0

I am trying to get a global key press for my windows form without overwriting it globally. I've tried setting Keypress = true but that only gets the keys pressed if the form is active, I've also tried to hook the key globally using

[DllImport("user32.dll")]
        private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll")]
        private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

but that overwrites the key which I don't want to do. Is there a happy medium somewhere where I can detect the key press globally without overwriting it?

Thanks

Robin Mackenzie
  • 18,801
  • 7
  • 38
  • 56
  • http://stackoverflow.com/questions/604410/global-keyboard-capture-in-c-sharp-application http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C – Warty Jul 16 '16 at 16:26

1 Answers1

0

You can try using ProcessCmdKey on your form.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
{       
    MessageBox.Show(keyData.ToString());        

    return base.ProcessCmdKey(ref msg, keyData);
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Sadly when I add that to my form class It only detects keypresses when my form is active but not when it is inactive. Is there something else I need to add to 'ProcessCmdKey' to get the key globally? – Patrick Nelson Jul 16 '16 at 16:10
  • Not that I know of but how are you clicking on an inactive form? Doesn't clicking on it make it active? – keyboardP Jul 16 '16 at 16:12
  • By inactive form , say I click on google chrome and click a key it won't activate the key pressed on the form, Basically I want to to be able to detect the key pressed if the form is not currently the main window active. – Patrick Nelson Jul 16 '16 at 16:41
  • Ah I see, it seems like a global hook is required which would detect any key press at any time. See my answer here if that's the case http://stackoverflow.com/questions/17579658/how-to-intercept-all-the-keyboard-events-and-prevent-losing-focus-in-a-winforms – keyboardP Jul 16 '16 at 16:43
  • Hmm this might work, I just don't know how to implement it into my windows form haha. I'm still new to this windows form stuff. How would I get the specific key pressed from this HookCallback? and output it in a messagebox for example. – Patrick Nelson Jul 16 '16 at 17:07
  • In that answer, look in the `HookCallback` section. It has this line `int vkCode = Marshal.ReadInt32(lParam);`. You can then cast `vkCode` with `Keys` as shown in the line after. You can use that to get the key that was pressed. – keyboardP Jul 16 '16 at 17:51
  • Thanks for the help so far, but I still can't seem to get this new code to work. I tried adding it to my Program.cs and tried adding it to my Form1.cs but It doesn't seem to trigger. I've tried [link](http://pastebin.com/pdBkC9kB) But nothing happens sadly. – Patrick Nelson Jul 16 '16 at 18:42
  • Make sure to call the `SetHook` method from somewhere in your app. You can look at my open source SteamKeyJ project to see an example (in particular, `KeyRecogniser.cs` and notice how it's called from `SteamKeyJ.cs`). Feel free to use the KeyRecogniser.cs class in your own project if it helps, I've put it under the MIT license https://github.com/keyboardP/SteamKeyJ – keyboardP Jul 16 '16 at 18:47
  • Thanks so much now I see how to use it! – Patrick Nelson Jul 16 '16 at 19:04