Solved

systemwide

Posted on 2004-03-24
6
175 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
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!

 

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

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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.
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.

763 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