Solved

Keyboard Hook

Posted on 2000-02-29
6
522 Views
Last Modified: 2008-02-01
Please anybody can tell me (or better yet, show me where I can get a sample source code in Visual C++ V6) how to write a keyboard hook that intercepts keyboard data from all threads (global hook). I got the example from MSDN (Hooks32) but I could not compile in VC++ V6, apparently that was written under either VC++ V2 or V4.
0
Comment
Question by:Volga
[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
  • 2
6 Comments
 
LVL 8

Expert Comment

by:gelbert
ID: 2570633
You can use SetWindowsHookEx with WH_KEYBOARD.
0
 
LVL 8

Expert Comment

by:gelbert
ID: 2570646
There is a sample by Paul DiLascia
in MSDN. Just look for "SetWindowsHookEx" keyword and scroll results until you see "C++ Q & A" for "Periodicals 1997". He is using this function in his program.
Also there are other samples for the same thing in MSDN
0
 
LVL 8

Expert Comment

by:gelbert
ID: 2570656
Another sample (if you do not have MSDN CD) is source code to "Visual C++ Windows Shell Programming" by Dino Esposito at www.wrox.com. Look around website for book page and there reference to download source code. Search source code for "SetWindowsHook"
0
Technology Partners: 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!

 
LVL 5

Accepted Solution

by:
Wyn earned 100 total points
ID: 2571361
Here is a easy sample I wrote:
//for *.dll:

#include <windows.h>

#pragma data_seg(".shared")
HHOOK hhook=NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.shared,RWS")

HINSTANCE hdll;

LRESULT CALLBACK ClockClass::KeyBoardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
//do whatever you want here
return CallNextHookEx(hhook,nCode,wParam,lParam);
}
   

int APIENTRY DllMain (HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
{ if ( dwReason == DLL_PROCESS_ATTACH)
hdll = hInstance;
return (int)(DisableThreadLibraryCalls (hdll));
}



_declspec(dllexport) void SetTheHook()
{
hhook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC),KeyBoardProc,hdll,NULL);
);
   
}

_declspec(dllexport) void RemoveTheHook()
{
   UnhookWindowsHookEx(hhook);
}

Now,you can write a .exe and call SetTheHook() and later you call RemoveTheHook.You'v got to link with .lib of that .dll .
Regards
W.Yinan

0
 

Author Comment

by:Volga
ID: 2577429
gelbert,
thanks for your suggestions but I had a hard time finding the right sample at the location.

wyn,
you are the closest who can give me some kind of sample code, even though I had to clean out some syntax error before being able to compile it. You have an extra ');' the line after SetWindowsHookEx.
I wrote an application that call SetTheHook in that dll but it looks like nothing happens. Do I need another hook (like WH_GETMESSAGE) to make it work?

Volga


0
 

Author Comment

by:Volga
ID: 2605697
wyn,

I have to accept your question, since I was able to compile the dll. But I'm still unable to write a program to translate the keyboard.
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

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

724 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