Solved

Hooks for Detecting Global Mouse and Keyboard Movement

Posted on 1997-06-17
4
459 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

786 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