VB2010 - KeyDown Events Not Executing - vb.net

I have a pong type of game and I want the spacebar to pause the game... Here is my code to detect when the space bar is pressed.
Private Sub PongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyValue = Keys.Space Then
Application.Exit()
End If
End Sub
For some reason the application is not closing... IS there something wrong with this code that I am not seeing?
Thanks

Make sure to check your form's KeyPreview property.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx

Related

VB:mouse click not work

My codes:
Me.KeyPreview = True
...
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Try to capture mouse right click, but not work.
Any suggestions welcomed. Thanks
As other mention in comments your code seems right, but will only work on plain form. To overcome that you can join events.
Private Sub Form_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
MsgBox("Right Mouse clicked.")
End If
End Sub
Please replace Control names you want your event to fire on in this code:
Handles MyBase.MouseClick, Button1.MouseClick, Control1.MouseClick, AnyOtherControl.MouseClick
I'm guessing you are using any of container controls which fill up most of your form. If you want your event to work with them you need to add them to your event.
Finally there is also matter of DoubleClick which won't fire above event. To overcome it all you need to do is change MouseClick to MouseDown

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.

Enter key in KeyDown

I just started learning VB recently and I'm trying to get familiar with the methods. Here I'm trying to write the key code to the console each time I press a key.
Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Console.WriteLine(e.KeyCode)
End Sub
This work for most of the keys, but strangely I can't get Enter to work. One thing I noticed is that whenever I press Enter I also "clicked" one of the buttons I have in the form.
What is happening here?
Keys like Shift, Enter, Tab, Escape are special keys. They need a little different commands.
Private Sub Form2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
MsgBox("Success!")
End If
End Sub
And that button which you are talking about has the focus. Set the focus to the form using SetFocus Control
That should work perfectly!
User Your Controls Key Down Event and Check for Keys to be Enter
Private Sub kd(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
IF e.key=key.Enter Then
<do your code>
Endif
End Sub

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

Form keyDown not working?

Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.Control Then
MessageBox.Show("aaaa")
End If
End Sub
As you can see, my form will check for when the control key is pressed down.
But it doesn't work. Why?
I am not near a computer now so I can't test this, but when I have wanted to get key events on a form before, I would set Form1.KeyPreview to True (or something similar).
That works fine. I assume you have other controls in your form. One of them will get the focus, never the form. Keyboard input only goes to the control with the focus.
You can set the form's KeyPreview property to True. The Winforms' way is to override the ProcessCmdKey() method.
You need to set KeyPreview to true when loading the form, it should work then
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.KeyPreview = True
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
MsgBox(e.KeyChar)
End Sub