?
Solved

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

Posted on 2003-10-29
1
Medium Priority
?
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 22

Accepted Solution

by:
_TAD_ earned 300 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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