How to read keyboard input in Winform?

I tried using the KeyUp and KeyDown events to read keyboard input, but as soon as I put other controls in Winform, the keys are not read. How can I make sure the keys are read?

+5
source share
4 answers

You can set KeyPreview = truein your form to catch keyboard events.

EDITED so you understand:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.A) 
        e.SuppressKeyPress = true;
}

A silly pattern that receives keyboard events and discards if A. was pressed.
If the focus is in the text field, you will see that the text is written, but not A !!

: VB.NET.
"Keypress" " ".
:

'Declare the event
Event KeyPress(KeyAscii As Integer) 

Private Sub Text1_KeyPress(KeyAscii As Integer)
    RaiseEvent KeyPress(KeyAscii)
End Sub
+4

marco , KeyPreview true , , .

KeyPress... KeyUp/Down , . KeyDown , ... .. , KeyPress - , .

, , KeyPressEventArgs.Handled = true.

http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=VS.90).aspx

?

MyForm.KeyDown += MyHandler;

... ...

+2

, . . .NET, DevX.com(, , ) , , . , "Keystroke Sandbox" , , .

, :

enter image description here

0
source

All Articles