Search code examples
c#keyboardkeypress

distinguish between keyboard's Real and Virtual key presses


I'm writing a program in C#, that have to simulate keyboard's key press commands.

The problem is - I need to simulate "realistic" keyboard button clicks, not the fake ones. For example, as far as I understand:

  • when user presses any button on the keyboard - the signal is sent via USB, and then proceed via keyboard's driver.

  • when API's are used (SendInput, SendKeys, KeyBoardEvent, or whatever) - we bypass USB and driver, so basically these kind of presses can be tracked via global hook method..

How do you simulate Realistic key presses?

  • I don't have knowledge in C++ so writing a custom driver is not an option.

  • I don't mind using microcontrollers or any additional hardware "solutions", as long as it's cheap..

  • maybe it's possible to connect two PC's via USB cable for the purpose of sending realistic keyboard button press signals from PC2 to PC1?

so what's the method?


Solution

  • You can identify keyboard event sent with SendInput() with global keyboard hook(WH_KEYBOARD_LL). It's callback method LowLevelKeyboardProc recieves KBDLLHOOKSTRUCT as a parameter.

    If LLKHF_INJECTED flag is set in KBDLLHOOKSTRUCT.flags keyboard event was injected with SendInput().