Solved

systemwide

Posted on 2004-03-24
6
176 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
[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
  • 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
Independent Software Vendors: 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!

 

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

Independent Software Vendors: 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

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

738 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