?
Solved

Hooks for Detecting Global Mouse and Keyboard Movement

Posted on 1997-06-17
4
Medium Priority
?
477 Views
Last Modified: 2008-03-17
I need to write a program which detects Mouse Movement and Keyboard Input.  Currently I'm trying to use HOOKS to accomplish this.  
As it stands my program only works within my program window.
For example, I have my program display a message box everytime I click the left mouse button.  However, when
I click the left mouse button outside the program window or anywhere on the desktop nothing happens.  No message box
is displayed even though I clicked on the mouse button.

I'm using      "if (wParam == WM_LBUTTONDOWN)"
to detect the left mouse button, but it's not global.  Meaning it
only works on one window.  
My question is how do I write a C++ program which detects
Mouse Movement and Keyboard Input anywhere, on any window on the desktop?  Thanks.
   
0
Comment
Question by:vjean
  • 2
  • 2
4 Comments
 

Author Comment

by:vjean
ID: 1163960
Edited text of question
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1163961
1. The system hook function must be in a DLL.
2. HOOKPROC hkprcSysMsg;
   static HINSTANCE hinstDLL;
   static HHOOK hhookSysMsg;

   ...

   hinstDLL = ::LoadLibrary("c:\\windows\\your.dll");
   hkprcSysMsg = (HOOKPROC)::GetProcAddress(hinstDLL, "MouseProc");
   hhookSysMsg = ::SetWindowsHookEx(WH_MOUSE,
    hkprcSysMsg, hinstDLL, 0);  // 0 means the hook procedure is associated with all existing threads.

For more information, see Win32 SDK documentation about Hooks.
0
 

Author Comment

by:vjean
ID: 1163962
What exactly do I put into "my.dll"?
The Mouse Procedure or something?

0
 
LVL 23

Expert Comment

by:chensu
ID: 1163963
Yes, the Mouse Hook Procedure and export it.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

589 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