Solved

systemwide

Posted on 2004-03-24
6
173 Views
Last Modified: 2010-04-01
I made a simple program with a systemwide keyboardhook using SetWindowsHookEx with LowLevelKeyboardProc.
here's the code:
****************
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)

{

  BOOL fHandled = FALSE;
  if (nCode == HC_ACTION)
  {
      KBDLLHOOKSTRUCT *pkbdllhook = (KBDLLHOOKSTRUCT *)lParam;
          switch (pkbdllhook->vkCode)
      {
          case VK_F12:

                      CTraa_004Dlg* maind = (CTraa_004Dlg*)theApp.GetMainWnd();
                      MessageBox(0,"ef","12",0);        
                      fHandled = TRUE;
                            break;
          }
  }

  return (fHandled ? TRUE : CallNextHookEx(ghhookKB, nCode, wParam, lParam));
}
*******************
now, my questions are:
1/I was told that something which is systemwide has to be in a dll, but it works, and it is an exe.
2/It works, but the MessageBox gets called twice  (when I press F12) I don't understand why...

thanks
0
Comment
Question by:lapijn
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10666959
1) Global keyboard hook with WH_KEYBOARD parameter must use Dll. WH_KEYBOARD_LL hook which can be used in WinNT or later works without Dll.
2) It is called twice - first time for WM_KEYDOWN message, second time for WM_KEYUP message. Test wParam for this.
0
 

Author Comment

by:lapijn
ID: 10672596
thanks.
But how should I set WM_KEYDOWN ?? I tried to set it as a default parameter, but apparently this doesn't does the job...
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 10674896
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)

{

  BOOL fHandled = FALSE;
  if (nCode == HC_ACTION)
  {
      if ( wParam == WM_KEYDOWN )
      {
       KBDLLHOOKSTRUCT *pkbdllhook = (KBDLLHOOKSTRUCT *)lParam;
           switch (pkbdllhook->vkCode)
       {
           case VK_F12:

                       CTraa_004Dlg* maind = (CTraa_004Dlg*)theApp.GetMainWnd();
                       MessageBox(0,"ef","12",0);        
                       fHandled = TRUE;
                             break;
       }
   }
  }

  return (fHandled ? TRUE : CallNextHookEx(ghhookKB, nCode, wParam, lParam));
}

Now MessageBox is shown once.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:lapijn
ID: 10676233
thanks. It works.
(However, I can only press the OK button after 5 seconds).
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10676310
Never use MessageBox and any other interaction functions inside of hook function. For testing purposes use TRACE.
0
 

Author Comment

by:lapijn
ID: 10676930
ok.thanks.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

786 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