Keydown event vb.net fails to respond? - vb.net

I have the following code snipet which I have in my vb.net program but it dosnt work???
it has absolutly no response, perhaps someoneout there knows why?
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If (e.KeyCode = Keys.Q AndAlso e.Modifiers = Keys.Control) Then
MsgBox("CTRL + Q Pressed !")
End If
End Sub
I'm using VB.net

Set KeyPreviewproperty of the form to true. The form will then get to know about key events even when a control has focus.

Related

VB.NET 2010 - Windows Ding.wav playing when pressing Enter inside a TextBox

I have a program where you type something into a TextBox and press Enter, then it will add the text into a ListBox and erase the TextBox.
It does work!
However, when Enter is pressed, the C:\Windows\Media\Windows Ding.wav audio plays.
It's honestly just annoying...
Anyone know how to stop that?
Thanks!
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
End If
End Sub
Do you want to disable the beep after Enter pressed? You can try to set property SuppressKeyPress to True.
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
ListBox1.Items.Add(TextBox1.Text)
TextBox1.Text = ""
e.SuppressKeyPress = True
End If
End Sub

AcceptButton or CancelButton with UserControls VB

I have a Form with a FlowLayoutpanel which adds several UserControls(TextBox+Label) in it. My Buttons(Accept/Close) are UserControls aswell.
How do I say my UserControl-Buttons that they're Buttons so that I can choose them in the Forms Properties as Accept-/CancelButton? Which property do I need for this?
Or how do I use the KeyPressEvent?
Private Sub OnKeyDownHandler(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
MessageBox.Show("Enter key pressed")
End If
End Sub
and
Private Sub Me_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If (Control.ModifierKeys And Keys.Enter) = Keys.Enter Then
MsgBox("enter key pressed")
End If
End Sub
aren't triggered when I press Enter in this Form...
thanks in advance :)
This question is ages old, but in case someone comes across this, the proper way to create a UseControl that should act like a button is to implement the System.Windows.Forms.IButtonControl interface. Once you've done that, you can select it as the Cancel/AcceptButton of the form.

keyboard shortcuts not responding in vb.net2010

This does not work on my application. Funny thing is, it does not give off an error either.
Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.A Then
MsgBox("A pressed")
End If
End Sub
I have seen where people use this in videos and it worked for them. I do not know why mine does not work.
You have to set the KeyPreview property of the form to true.
valter

How to find out which key is pressed in keyDown event in visual basic?

I'm writing my first visual basic program, And I want to do something when for example the 'k' button is pressed, I know that I should write the code in "KeyDown" event, but I don't know how to find out that 'k' button is pressed or not
If you are using a Windows Forms Application, you have to put the KeyPreview property of the form to True so the form will monitorize key events.
Then:
Private Sub Form1_KeyPress(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = "k" OrElse e.KeyChar = "K" Then
MessageBox.Show("Pressed!")
End If
End Sub
If you prefer, you could use other event:
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.K Then
MessageBox.Show("Pressed!")
End If
End Sub

Form keyPress help

first of all, I needed a way to know when the control key was down, and here's the link: Form keyDown not working?
Thanks to them I got it working. But I noticed that was not my ultimate objective!
Instead of checking for the control key on keyDown, I want to check for it on keyPress. But apparently I can't use
If e.Control Then
End If
On the
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
End Sub
Method.
Any ideas? I also want to check for when the key is no longer pressed...
New problem:
Private Sub controlTick(ByVal sender As Object, ByVal e As EventArgs)
If Control.ModifierKeys = Keys.Control Then
controlActivated = True
PictureBox2.Invalidate()
End If
If Control.ModifierKeys <> Keys.Control Then
controlActivated = False
PictureBox2.Invalidate()
End If
Label1.Text = controlActivated
End Sub
That is inside a timer. For some reason it is always "False" unless I click somewhere with the control key pressed...
By the time you get the KeyPress event, which you won't when the form has any controls, the Control key state is already applied to the pressed key. So you'll get, say, Ctrl+V:
Private Sub Form1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = ChrW(22) Then
MessageBox.Show("Ctrl+V pressed")
End If
End Sub
Ctrl+A = 1, etcetera, through Ctrl+Z = 26. This is very likely what you want to do, you could also use the Control.ModifierKeys property:
If Control.ModifierKeys = Keys.Control Then
MessageBox.Show("Control key pressed")
End If
Beware that many keys don't generate a KeyPressed event, like Ctrl+F1. KeyDown is required to test them.
You should use Control.ModifierKeys in whatever operation that should be affected by whether or not the control key is down. This timer's Tick event handler works fine:
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Dim controlActivated As Boolean
If (Control.ModifierKeys And Keys.Control) = Keys.Control Then
controlActivated = True
End If
Label1.Text = controlActivated.ToString()
End Sub