KeyDown form 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 be checked when the control key is pressed. But that will not work. Why?

+6
visual-studio winforms
source share
3 answers

Now I’m not near the computer, so I can’t check it, but when I wanted to receive key events in the form earlier, I would set Form1.KeyPreview to True (or something like that).

+13
source share

It works great. I assume that you have other controls in your form. One of them will receive focus, not form. The keyboard input is only for focus control.

You can set the KeyPreview form property to True. Winforms method is to override the ProcessCmdKey () method.

+1
source share

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 
0
source share

All Articles