• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

systemwide

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
lapijn
Asked:
lapijn
  • 3
  • 3
1 Solution
 
AlexFMCommented:
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
 
lapijnAuthor Commented:
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
 
AlexFMCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
lapijnAuthor Commented:
thanks. It works.
(However, I can only press the OK button after 5 seconds).
0
 
AlexFMCommented:
Never use MessageBox and any other interaction functions inside of hook function. For testing purposes use TRACE.
0
 
lapijnAuthor Commented:
ok.thanks.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now