2

I have a Form with several special controls on it that is the main window of my application. I would like to capture all key presses while my application is running, at the form level, rather than on the focused control or anything like that. What is the standard way to do this in WinForms?

captncraig
  • 22,118
  • 17
  • 108
  • 151
  • Check this out: http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-winforms – o.k.w Oct 24 '09 at 02:52

2 Answers2

3

set Form.PreviewKeys = true, then you get all keyboard event.

Benny
  • 8,547
  • 9
  • 60
  • 93
  • This works fine. I just overrode the ProcessCmdKey event and it seems to work. – captncraig Oct 24 '09 at 03:00
  • Works as long as there is no other control in the form listening for the same key. For example if you are trying to catch F2 at form level but also defined F2 as shortcut to a ToolStripMenuItem then neither will get them. – Lord of Scripts Jul 21 '17 at 17:55
1

Set the Form's KeyPreview property to true and then handle one of the keypress events.

Michael Todd
  • 16,679
  • 4
  • 49
  • 69