Solved

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

Posted on 2003-10-29
1
436 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to gracefully close the c++ 11 thread? 3 80
I could not build boost code, 10 78
Best book to learn C++ 4 70
object oriented programming comparison 5 52
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 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

17 Experts available now in Live!

Get 1:1 Help Now