Link to home
Start Free TrialLog in
Avatar of namedpipes
namedpipes

asked on

How to consume keystrokes in keyboard handler (similar to keylogger)

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)
{
      char ch;

      if (((DWORD)lParam & 0x40000000) &&(HC_ACTION==nCode))
      {
            if ((wParam==VK_SPACE)||(wParam==VK_RETURN)||(wParam>=0x2f ) &&(wParam<=0x100))
            {
                  switch (state)
                  {
                  case 0 :
                        if((wParam==145) && (pwParam==145)) // we've seen 145+145
                              state = 1;
                        break;
                  case 1 :
                        if(wParam==189) // we've seen 145+145+189 (id badge leadin)
                              state=2;
                        else
                              state=0;
                        break;
                  case 2 :
                        if(wParam==189)
                        {
                              _spawnl( _P_NOWAIT, path, path, argv, NULL );
                              state=0;
                              strcpy(argv,"");
                        }
                        else
                        {
                              logger("Accumulating barcode data...\n");
                              BYTE ks[256];
                              GetKeyboardState(ks);
                              WORD w;
                              UINT scan;
                              scan = 0;
                              ToAscii(wParam,scan,ks,&w,0);
                              ch = char(w);
                              sprintf(argv,"%s%c",argv,ch);
                        }
                        break;
                  default :
                        state=0;
                        break;
                  }

                  pwParam = wParam;
            }
      }

      return CallNextHookEx( hkb, nCode, wParam, lParam );
}


I tried:

if(state==0)
  return CallNextHookEx ...
else
  return 0;

Any thoughts?
ASKER CERTIFIED SOLUTION
Avatar of _TAD_
_TAD_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial