How to consume keystrokes in keyboard handler (similar to keylogger)
Posted on 2003-10-29
I have an application that hooks the keyboard handler and examines each keystroke. It looks for a particular sequence that would only be generated by a barcode scan, and if detected, launches another program with the scanned number as an argument. This works fine.
I'd like to prevent the scanned number from entering the keyboard stream.
Currently, if the user had a text editor open (for example) and scanned the barcode, the barcoded number would appear in the text editor, AND the other application would launch correctly.
Ideally, I want the number not to appear in the text editor, just launch the other application.
The keyboard hook routine uses a state table to see if the barcode was scanned. I would like to discard the keystroke if the current state indicates I am accumulating a barcode scan. I tried to do this by return-ing rather than calling the next hooked routine. It did not appear to work.
Must I explicitly remove the keystroke from the keyboard buffer or can I tell the next hooked to ignore the keystroke somehow?
Here's the routine:
LRESULT WINAPI CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
case 0 :
if((wParam==145) && (pwParam==145)) // we've seen 145+145
state = 1;
case 1 :
if(wParam==189) // we've seen 145+145+189 (id badge leadin)
case 2 :
_spawnl( _P_NOWAIT, path, path, argv, NULL );
logger("Accumulating barcode data...\n");
scan = 0;
ch = char(w);
pwParam = wParam;
return CallNextHookEx( hkb, nCode, wParam, lParam );
return CallNextHookEx ...