1

I am using this Global Keyboard Hook as found here: Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C#

It works great, and I use a little piece of code to handle the keydown event.

private void Klistener_KeyDown(object sender, RawKeyEventArgs e)
{
    if (e.Key == Key.Snapshot)
    {
        MessageBox.Show("Key Pressed!");
    }
}

However, any other handlers tied to Key.Snapshot will still work. For instance Windows default for the key will print screen and save it into the clipboard, what if I didn't want any other actions happening after my handler?

I'm not entirely fluent in the Keyboard Hook I am using, but I'm sure there must be a way to implement a e.Handled property or something similar as can be found in KeyEventArgs.

Any ideas how I would go about doing this? Thanks.

Community
  • 1
  • 1
  • It may help if you knew I'm basically creating an application that'll take advantage of the Print Screen key, and I'm going to use that as the global hotkey for my application. The idea is that no other application can be able to use the same hotkey. The default Windows use for the Print Screen hotkey is that it'll place an image inside the clipboard and of course I want to disable all that completely and using my own application with it... if that makes sense. –  Jul 03 '13 at 10:36

1 Answers1

1

The MSDN says :

If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.

So you would need to just return 1 in LowLevelKeyboardProc if the desired key was pressed. Otherwise you return the call to CallNextHookEx

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • So would I change void KeyboardListener_KeyboardCallbackAsync to int KeyboardListener_KeyboardCallbackAsync Then return 1; ? –  Jul 03 '13 at 09:13
  • From the MSDN article I think that would do the trick. But it's not that well documented. Please try it and gie us a feedback. – Romano Zumbé Jul 03 '13 at 09:17
  • But you only need to return 1 when the desired key was pressed. Otherwise you need to return zero or all keys will be disabled. Perhaps you should try it also with a nullable int ( `int?` ) if a standard int doesn't work. – Romano Zumbé Jul 03 '13 at 09:20
  • Just tried it and it now has an error. 'int ScreenCapture.Keyboard.KeyboardListener.KeyboardListener_KeyboardCallbackAsync(ScreenCapture.Keyboard.InterceptKeys.KeyEvent, int)' has the wrong return type. Line 28. Line 28 is... hookedKeyboardCallbackAsync = new KeyboardCallbackAsync(KeyboardListener_KeyboardCallbackAsync); I modified the RawKeyEventArgs class and added in a Handled property. Inside KeyboardListener_KeyboardCallbackAsync I check if handled is set to true and return 1 otherwise return 0. Would paste code but it wouldn't display nicely in a comment. –  Jul 03 '13 at 09:21
  • Sorry, I told you the wrong method. You can see a good example on this site http://www.programmersheaven.com/mb/beginnercpp/362851/362903/using-windows-registry-how-do-i-disable-the-windows-key/. What you need to change is the method `LowLevelKeyboardProc` where you return the call to `CallNextHookEx`. There you return a 1 in case the key (you want to disable) was pressed and other wise you return the call to `CallNextHookEx`. Answer is edited – Romano Zumbé Jul 03 '13 at 09:25
  • I understand what you're saying however I don't want to disable a key, I just want to prevent any default actions after it (for instance another program may use the same key which I would rather it was all overwritten with my handler). If what you said would do that, then I'm confused... –  Jul 03 '13 at 09:34
  • The method I described disables the key systemwide as long as the application is running. Do you just want to disable the windows key handling and keep the handling by other programs? – Romano Zumbé Jul 03 '13 at 09:36
  • I want to use the key in my own application and not allow it to be used by other applications (unless my application was not open). So in a way it'd be a system wide disable with the exception of my application if that makes sense? –  Jul 03 '13 at 09:38
  • That is exactly what the described approach does. – Romano Zumbé Jul 03 '13 at 11:12