Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch a key press on a C# .NET form

Tags:

c#

.net

winforms

I have a parent form that contains a lot of controls. What I am trying to do is filter all of the key presses for that form. The trouble is that if the focus is on one of the controls on the form then the parent form is not getting the key press event, so how do I capture the key down event?

like image 590
flavour404 Avatar asked Jun 08 '10 20:06

flavour404


People also ask

How do you check if a key is pressed in C?

kbhit() is present in conio. h and used to determine if a key has been pressed or not. To use kbhit function in your program you should include the header file “conio. h”.

How do I find key presses?

The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.

How do you make a program press the key?

You'll need to start by downloading and installing AutoHotkey, which is a simple scripting language that allows you to create easy scripts. Once you do that, right-click anywhere and choose New –> AutoHotkey Script. This simple script will wait every 30 minutes and press the Spacebar.


1 Answers

Set KeyPreview to true on your form and you will catch them: MSDN

like image 192
RvdK Avatar answered Sep 30 '22 02:09

RvdK