Why does my keyboard hook receive the same key-up and key-down events multiple times?

为君一笑 提交于 2019-12-01 17:53:00

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 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;

With this

   if (Code < 0) or (Code = HC_NOREMOVE ) then
   begin
      Result := CallNextHookEx(KBHook, Code, wparam, lparam);
      Exit;
   end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!