为什么我的键盘挂钩多次接收相同的键盘和键盘事件? [英] Why does my keyboard hook receive the same key-up and key-down events multiple times?

查看:134
本文介绍了为什么我的键盘挂钩多次接收相同的键盘和键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前的问题中,我报告说键盘挂钩报告了两次扫描条形码时。

In my previous question, I reported that a keyboard hook was reporting everything twice when scanning a barcode.

我把它放下来,重要事件,并收到良好的建议。

I put that down to key down & key events and received good advice.

仔细观察之后,我发现每个数字实际上正在报告四次!

Having looked at it more closely I find that each digit is actually being report FOUR times!

这是一个粗糙的打印调试。有人可以建议我可能做错了什么吗?你需要更多的信息吗?我可以只是忽略每一秒的输入,但是... yeuck!我宁愿明白发生了什么。

Here's a crude "debug by print". Can anyone suggest what I might be doing wrong? Do you need more info? I could just ignore every second input, but ... yeuck! I would rather understand what is happening.

这是我得到的一个数字2

Here's what I got for a single digit 2

---------
LongParam = 196609 |  Word = 50 | 2
LongParam and $80000000 = 0
LongParam and $40000000 = 0
---------
LongParam = 196609 |  Word = 50 | 2
LongParam and $80000000 = 0
LongParam and $40000000 = 0
---------
LongParam = -1073545215 |  Word = 50 | 2
LongParam and $80000000 = 2147483648
LongParam and $40000000 = 1073741824
---------
LongParam = -1073545215 |  Word = 50 | 2
LongParam and $80000000 = 2147483648
LongParam and $40000000 = 1073741824






更新:这是我的代码


Update: here's my code

function KeyboardHookProc(Code: Integer; WordParam: Word; LongParam: LongInt): LongInt; stdcall;
begin
   if Code < 0 then  // http://msdn.microsoft.com/enus/library/windows/desktop/ms644984%28v=vs.85%29.aspx
   begin
      Result := CallNextHookEx(KBHook, Code, WordParam, LongParam);
      Exit;
   end;

MainForm.Memo1.Lines.Add('---------');
MainForm.Memo1.Lines.Add('LongParam = ' + IntToStr(LongParam) +  ' |  Word = ' +         IntToStr(Ord(WordParam)) + ' | ' + Char(WordParam));
MainForm.Memo1.Lines.Add('LongParam and $80000000 = ' + IntToStr(LongParam and $80000000));
MainForm.Memo1.Lines.Add('LongParam and $40000000 = ' + IntToStr(LongParam and $40000000));

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

   if MainForm.ScanningChemical = False then
   begin
      Result := CallNextHookEx(KBHook, Code, WordParam, LongParam);
      Exit;
   end;

此时我有一个条码数字。但是在这里之前添加了这些备忘录。

At this point I have a bar code digit. But those memo lines were added before here.

推荐答案

您的问题与您如何评估代码 param。
有关 <$ c $的文档c> KeyboardProc回调函数 指出:

Your issue is related to the way how you are evaluating the value of the Code param. The documentation about the KeyboardProc callback function states :


HC_NOREMOVE
wParam和lParam参数包含有关击键消息的信息,并且击键消息尚未从消息队列中删除
。 (一个名为PeekMessage
函数的应用程序,指定PM_NOREMOVE标志。)

HC_NOREMOVE The wParam and lParam parameters contain information about a keystroke message, and the keystroke message has not been removed from the message queue. (An application called the PeekMessage function, specifying the PM_NOREMOVE flag.)

要解决问题,只需将此代码

To fix the problem just replace this code

   if Code < 0 then  
   begin
      Result := CallNextHookEx(KBHook, Code, WordParam, LongParam);
      Exit;
   end;

有了这个

   if (Code < 0) or (Code = HC_NOREMOVE ) then
   begin
      Result := CallNextHookEx(KBHook, Code, wparam, lparam);
      Exit;
   end;

这篇关于为什么我的键盘挂钩多次接收相同的键盘和键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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