Windows键盘报告一次两次 [英] Windows keyboardhook reporting everything twice

查看:118
本文介绍了Windows键盘报告一次两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi,尝试通过USB从条形码扫描器读取,这只是另一个人机界面设备。

I am using Delphi and trying to read from a bar code scanner over USB, so that it is just another Human Interface Device.

我正确的取数字,但每次两次。我想象这是关键和关键。

I get the digits correctly, but get each twice. I imagine that this is key down and key up.

我可以用旗子把它弄脏,忽略非常第二次阅读,但宁愿这样做。

I could; kludge it with a flag and ignore very second read, but would rather do it propery.

我的代码略微改编自此链接

我可以指定在分配钩子时我只想要key_up事件吗?

Can I specify that I only want key_up events when assigning the hook?

KBHook := SetWindowsHookEx(WH_KEYBOARD,
                           @KeyboardHookProc,
                           HInstance,
                           GetCurrentThreadId()) ;

或者以某种方式检查钩子函数本身的标志?

or somehow check a flag within the hook function itself?

更新:我试图为它编码,但是看起来我错了。这是我在钩子功能开始时尝试的一个方法。

Update: I tried to code for it, but looks like I got it wrong. Here's what I tried at the start of my hook function

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms644984%28v=vs.85%29.aspx
if Code < 0 then  
begin
   Result := CallNextHookEx(KBHook, Code, WordParam, LongParam);
   Exit;
end;

if (((LongParam and $80000000) <> $80000000)    (* key is not being released *)
and ((LongParam and $40000000) <>  $40000000))  (* key was not previously down *) then
begin
   Result := CallNextHookEx(KBHook, Code, WordParam, LongParam);
   Exit;
end;






[更新]五年后,仍然没有帮助,但我发现我原来的后续问题(qv)。

推荐答案

在KeyboardHookProc中,您需要检查 LongParam 参数的高位。如果高位为零,则是按键。如果高位是一个,这是一个关键的版本。

In your KeyboardHookProc, you need to check the high bit of the LongParam argument. If the high bit is zero, it is a key press. If the high bit is one, it is a key release.

例如:

KeyUp:boolean;

KeyUp := ((LongParam and (1 shl 31)) <> 0);

这篇关于Windows键盘报告一次两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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