0

Using a Global Keyboard Hook (as here: Global keyboard capture in C# application), how would one detect which keyboard has been pressed from the event fired?

Pavel_K
  • 10,748
  • 13
  • 73
  • 186
Paul
  • 9,409
  • 13
  • 64
  • 113
  • 1
    Just so we're clear, you really mean "keyboard"? As in, you have multiple keyboards, you need to know if the user pressed the A button on the left or the right keyboard (as an example)? – Lasse V. Karlsen Nov 21 '17 at 11:05
  • Exactly that yes – Paul Nov 21 '17 at 11:50
  • AFAIK, you can't do that with global hooks. If you need to know which device was used, use DirectInput. It is much more low-level and MS considers it outdated, but it does its job and also it has an additional advantage of being much more robust then global hooks as it does not involve any dll injections or other dark practices. – rs232 Nov 21 '17 at 15:26
  • Thanks - I've started on a direction like that – Paul Nov 22 '17 at 15:51

1 Answers1

-1

here is an example:

keyBoardHook = new KeyboardHook(true);
keyBoardHook.KeyDown += KeyBoardHook_KeyDown;
keyBoardHook.KeyUp += KeyBoardHook_KeyUp;

private void KeyBoardHook_KeyUp(System.Windows.Forms.Keys key, bool Shift, bool Ctrl, bool Alt)
    {
        if (key == System.Windows.Forms.Keys.F1)
        {
            //do something
        }
    }

    private void KeyBoardHook_KeyDown(System.Windows.Forms.Keys key, bool Shift, bool Ctrl, bool Alt)
    {
        if (key == System.Windows.Forms.Keys.F1)
        {
            //do something
        }
    }
  • 1
    How does this answer the question? Additionally, every answer should contain some explanation, not just an unexplained snippet of code. – Flater Nov 21 '17 at 15:17
  • sorry, i misunderstood the question. i thought he was asking how to detect which key pressed – StackOverflowUser Nov 22 '17 at 09:35