Solved

systemwide

Posted on 2004-03-24
6
171 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

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

Expert Comment

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

Author Comment

by:lapijn
Comment Utility
ok.thanks.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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 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.

771 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

16 Experts available now in Live!

Get 1:1 Help Now