简体   繁体   English

防止全局键盘挂钩上的默认设置

[英]Preventing default on a Global Keyboard Hook

I am using this Global Keyboard Hook as found here: Using global keyboard hook (WH_KEYBOARD_LL) in WPF / C# 我正在使用此全局键盘挂钩,如下所示: 在WPF / C#中使用全局键盘挂钩(WH_KEYBOARD_LL)

It works great, and I use a little piece of code to handle the keydown event. 效果很好,我使用了一些代码来处理keydown事件。

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. 但是,与Key.Snapshot绑定的任何其他处理程序仍然可以使用。 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? 例如,默认的Windows键将打印屏幕并将其保存到剪贴板,如果我不希望在处理程序之后发生任何其他操作怎么办?

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. 我使用的Keyboard Hook并不完全流利,但是我敢肯定必须有一种方法来实现e.Handled属性或类似KeyEventArgs的东西。

Any ideas how I would go about doing this? 有什么想法我将如何去做吗? Thanks. 谢谢。

The MSDN says : MSDN说:

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. 因此,如果按下了所需的键,则只需在LowLevelKeyboardProc返回1。 Otherwise you return the call to CallNextHookEx 否则,将呼叫返回给CallNextHookEx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM