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.
My code is adapted slightly from this link.
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;
[Further update] Five years after, and this still doesn't help, but I find that my original follow-up question (q.v) does.
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.
For example: