Windows keyboardhook reports everything twice

I use Delphi and try to read from a barcode scanner via USB, so this is just another device for the human interface.

I understand the numbers correctly, but I get them twice. I guess this is the key and the key is up.

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

My code is a bit adapted from this link .

Is it possible to indicate that I only need key_up events when assigning a hook?

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

or somehow check the flag inside the hook function itself?


Update: I tried to create code for it, but it looks like I was wrong. Here is what I tried at the beginning 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) .

+5
1

KeyboardHookProc LongParam. , . , .

:

KeyUp:boolean;

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

All Articles