今天有一个需求,要写一个DEMO,在WIN10下监听LWin+F20、LWin+F19、LWin+F18.三个快捷键。
之前写了个DEMO,是通过修改注册表的方式,修改Iink工作区的启动对象,这种方法缺点是用户可以自己通过ink工作区修改启动对象,其次在执行修改注册表时需要管理员权限。
老规矩,先贴查询过的资料
HID API 相关
http://blog.youkuaiyun.com/u010875635/article/details/73321066
RawInput 相关
https://msdn.microsoft.com/en-us/library/windows/desktop/ms645546(v=vs.85).aspx
RegisterHotKey百度去搜,连接太多了。被审核了。去掉了这个
Virtual-KeyCodes 键值码相关 连接太多,呗审核了,去掉这个
今天首先使用RawInput实现了监听,但是发现需要和设备建立连接后,通过 Win32.GetRawInputData,从连接的设备中获取数据包;而后找到自己要的内容,通过事件返回给UI。RawInput在使用时主要遇到的问题是找到那个设备是你要建立连接的。而后做数据包的拆分,在拆分数据包时对数据包每一位是干啥的不知道。所以比如在LWin+F20,只抓到了返回结果Keys为91.而F20键值为131.所以发数据包的时候只抓到了LWin的包。没抓到F20的包,当时很纠结问题在哪。因为当时没考虑到是组合键。就以为自己解析数据包时不对。而后放弃了RawInput的写法。(后面找到了问题。)
同时也去查找了一下RegisterHotKey注册热键失败的问题到底是什么。
RegisterHotKey在MSDN的备注说明上给了以下几个解释
This function cannot associate a hot keywith a window created by another thread.
RegisterHotKey fails if the keystrokesspecified for the hot key have already been registered by another hot key.
If a hot key already exists with thesame hWnd and id parameters, it is maintained along with the new hot key. Theapplication must explicitly callUnregisterHotKeyto unregister the old hot key.
The F12 key isreserved for use by the debugger at all times, so it should not be registeredas a hot key. Even when you are not debugging an application, F12 is reservedin case a kernel-mode debugger or a just-in-time debugger is resident.
这也就是为什么我通过RegisterHotKey注册LWin+F20和LWin+F19LWin+F18失败的原因。
今天主要说的是Hook。因为在遇到RawInput问题时,为了快速的完成任务。我改用了之前比较熟悉的Hook。
先解释一下什么是Hook,解释之前先看一下Windows的说明:
Windows平台是基于事件驱动机制的,整个系统都是通过消息的传递来实现的。当进程有响应时(包括响应鼠标