Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

861 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