0

I am currently working on a program, where I need to get Event's if a Key is pressed. (Even if my program is not focused / minimized.) For that I want to use the keybd_event. I also found some things in the internet, but nothing helpfull for me. (I also looked in the MSDN, but I didn't find it something helpfull.)

What do I need implement to get this working?

Thank you for your answers

Marcel

  • possible duplicate of [C# : Keyboard Hook](http://stackoverflow.com/questions/10391025/c-sharp-keyboard-hook) – G J Sep 29 '14 at 10:53

1 Answers1

0

The keybd_event documentation states that the function should be used for generating keystrokes and not for detecting them:

Synthesizes a keystroke. The system can use such a synthesized keystroke to generate a WM_KEYUP or WM_KEYDOWN message.

The documentation also states that the method is deprecated (which could explain the lack of resources):

Note This function has been superseded. Use SendInput instead.

... It looks as though you need a different function.

To detect a keypress irrespective of whether the window is in focus or not, you have a few options:

  • Use the RegisterHotkey function to detect the pressing of a hotkey.
  • Use the SetWindowsHookEx function to hook the keyboard and to receive notifications whenever any key is pressed.
  • Use the GetKeyState function to poll the state of a key.

Each of these functions have a copious number of tutorials that are only a Google search away.

User 12345678
  • 7,714
  • 2
  • 28
  • 46