Solved

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

Posted on 2003-10-29
1
434 Views
Last Modified: 2008-03-17
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?
0
Comment
Question by:namedpipes
1 Comment
 
LVL 22

Accepted Solution

by:
_TAD_ earned 75 total points
ID: 9645300


Here is a WIN API site that talks about HOOKS and keyboard messaging (MSDN):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks.asp


I'll play around with some of the calls to see if I can actually provide some code that will alter a message.  You may be able to do it quiker than I depending on your familiarity with keyboard messaging and setting hooks.

But I think you may want to look closely at the GetMessage, PeekMessage and SetWindowHookEx calls.  I haven't written any code yet, but I'm guessing that if the first message is character 145, you can pause and wait for the next character (or peek at the next character), if that one is also 145 then disable/hide the return values from the user.  But like I said... I havn't tested it yet so I may just be causing more problems than I'm solving.



0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now