低级键盘钩& rawinput的击键 [英] Low level keyboard hook & keystrokes from rawinput

查看:135
本文介绍了低级键盘钩& rawinput的击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在编写一个程序,以拦截来自特定键盘的按键(使用其HID过滤).因此,要了解特定设备发送了哪些按键,我使用了RawInput技术,该技术的灵感来自于本教程:

Currently, I'm making a program that intercept keystrokes from a specific keyboard (filtered using its HID). So to know which keystrokes have been sent by a specific device, I used the RawInput technic, inspired by this great tutorial:

http://www.codeproject.com/Articles/17123/使用-Raw-Input-from-C-to-handle-multiple键盘

现在,它的效果很好:我可以击键并知道是哪个键盘产生了它.

Now, it works great: I can get a keystroke and know which keyboard have generated it.

我的项目的难点是拦截和阻止来自此特定键盘的击键,以避免这些击键到达重点应用程序(重点"是指操作系统带来的前台窗口).

The difficult part of my project is to intercept and block keystrokes from this specific keyboard, to avoid these keystrokes to reach the focused application (focused mean the foreground window brought by the OS).

因此,自然的解决方案是对所有具有窗口句柄的当前线程进行低级全局挂钩.

So the natural solution was a low level global hook, on all current threads that have a window handle.

我使用并改编了此页中的代码来做到这一点:

I used and adapted the code from this page to do that:

http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx

我在Visual Studio中创建了一个新项目,以避免使工作混乱.经过研究,我可以通过在回调函数中返回值(-1)来阻止所有应用程序上的击键:

I created a new project in visual studio to avoid putting the mess in my work. After some research, I was able to block keystrokes on all applications, by returning the value (-1) in the callback function:

private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)  
{
//Check if we have a key to pass

if (
    nCode >= 0 && ( 
    (wParam == (IntPtr)WM_KEYDOWN) || (wParam == (IntPtr)WM_KEYUP) ) 
    )
{
    int vkCode = Marshal.ReadInt32(lParam);
    if ((Keys)vkCode == Form1.KeysToIgnore)
    {
        return (IntPtr)(-1);
    }
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

要整合所有内容(挂钩过程和按键检测),我在最终项目中创建了两个线程:

To put all together (the hook procedure, and the keystroke detection), I create two threads in the final project:

第一个:使用RawInput识别每个击键并将其附加到设备上

1st : using RawInput to identify and attach each keystroke to a device

第二个:用于挂接所有窗口并阻止某些按键

2nd : used to hook all windows and block certains keystrokes

线程1旨在将击键发送给线程2,该线程2读取发送到所有窗口应用程序的所有消息以及从特定键盘上清除垃圾桶的击键.我精确地说,这两个线程是同步的.

Thread 1 is designed to send keystroke to block to the thread 2, that read all messages sent to all window application and trash keystrokes from a specific keyboard. I precise that these two threads are synchronized.

问题在于,该挂钩似乎在执行Rawinput之前执行,因此我无法识别发送按键的键盘.我不知道该怎么做,也许不知道要改变钩子的类型(避免使用低级键盘钩子,而要使用用户空间级键盘钩子).

The problem is that the hook seems to be executed before the execution of Rawinput, so I can't identify the keyboard that sent the keystroke. I have no idea how to do that, maybe to change the type of hook (avoid using low level keyboard hook, but using a user-space level keyboard hook).

或者也许有人知道做我想要的聪明方法?

Or maybe someone know a clever way to do what I want?

我知道此请求确实很复杂,请随时询问更多细节.

I know this request is really complicated, don't hesitate to ask for more details.

推荐答案

好,Silent Dogood给了我一个很好的提示:

Ok, Silence Dogood give me a good clue:

Intercept库非常适合我使用.您可以在这里找到它:

The library Interception is perfect for my use. You can find it here:

https://github.com/oblitum/Interception

这篇关于低级键盘钩& rawinput的击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆