Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I capture keys on a WinForms application when the application is in focus?

Tags:

c#

.net

winforms

How can I capture keys on a WinForms application when the application is in focus?

I have tried using the Form_KeyDown and Form_KeyUp events but they don't work the way I want them to.

like image 675
Joan Venge Avatar asked Oct 17 '25 01:10

Joan Venge


1 Answers

Set KeyPreview to true

Here's a sample OnKeyDown override for your form that eats the keystroke and prevents it from being propagated to other events:

protected override void OnKeyDown(KeyEventArgs e)
{
    e.SuppressKeyPress = true; // do this to 'eat' the keystroke
    base.OnKeyDown(e);
}
like image 106
jjxtra Avatar answered Oct 18 '25 17:10

jjxtra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!