Hello there!
I need a little help with a key detection program, i only need to watch for 1 key just -like the method to start recording with fraps-.
I'm trying to use WH_KEYBOARD hook(not the WH_KEYBOARD_LL because i need the program to do stuff while "hooking").
In the dll i need to determine which key was pressed, but i have no idea ,how could i do that,so please give me an example.
Also ,if you would be so kind to check my code if i made any mistakes.
So here is "my"-mostly copied from other sites-code:
The Main program

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HOOKPROC hkprcSysMsg;
HINSTANCE hinstDLL;  
HHOOK hhookSysMsg; 
hinstDLL = LoadLibrary(TEXT("C:\\cppprojdll.dll")); 
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProc"); 
hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD,hkprcSysMsg,hinstDLL,0);
//here the program should execute the code from the dll , but i don't have any idea ,how to do that :(
UnhookWindowsHookEx(hhookSysMsg);
}

The Dll
DLL_Tutorial.h

#ifndef _DLL_TUTORIAL_H_
#define _DLL_TUTORIAL_H_
#include <iostream>
#include <windows.h>
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

extern "C"
{
   DECLDIR LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
}

#endif

DLLTutorial.cpp

#include <iostream>
#include "DLL_Tutorial.h"
#include <windows.h>
#define DLL_EXPORT

extern "C"
{
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) 
{   if (nCode < 0)
    return CallNextHookEx(NULL, nCode, wParam, lParam);
	///here the program should print "key e is pressed" ,when it's pressed
	return CallNextHookEx(NULL, nCode, wParam, lParam); 
} 

}

Well guys, i'm sorry for wasting your time, with all theese noob problems =/
But if you have a little time to explain this thing to me, please do so.
Thanks & Have a nice day!

Andrew

Hi Andrew,

Unfortunately I don't know the answer to your question. However, I urge you to make the titles of your threads as descriptive as possible. For example, I came to look at "A little problem", but quickly found out that it was related to the Windows API and I don't know anything about that. If you named it something like "Determine which key was pressed with WH_KEYBOARD hook", I would have known to leave it to the Windows guys :)

Good luck,

David

//here the program should execute the code from the dll , but i don't have any idea ,how to do that :(

In the dll i need to determine which key was pressed, but i have no idea ,how could i do that

Which one is your exact question?
From what I remember, I answered the latter Here.
If former, you just have to import the function, which compiler?

My bad. Apologies.

Hi Andrew,

Unfortunately I don't know the answer to your question. However, I urge you to make the titles of your threads as descriptive as possible. For example, I came to look at "A little problem", but quickly found out that it was related to the Windows API and I don't know anything about that. If you named it something like "Determine which key was pressed with WH_KEYBOARD hook", I would have known to leave it to the Windows guys :)

Good luck,

David

Okay, I'll do so the next time, thanks fr the idea :).

Which one is your exact question?
From what I remember, I answered the latter Here.
If former, you just have to import the function, which compiler?

Yep there is a former problem of mine, which you answered,but now i need a non-lowlevel hook so the hook is in a dll not in the main program...

You can import the CALLBACK function from the dll & it should be fine.

Okay , please take a look at my code,now:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
HOOKPROC hkprcSysMsg;
HINSTANCE hinstDLL;  
HHOOK hhookSysMsg;
hinstDLL = LoadLibrary(TEXT("C:\\cppprojdll.dll")); 
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProc"); 
hhookSysMsg = SetWindowsHookEx(WH_KEYBOARD,hkprcSysMsg,hinstDLL,0);
//Now i need here a code , which basically lets the dll to do it's work(don't let this program to exit...")
UnhookWindowsHookEx(hhookSysMsg);
}

You need to import the CALLBACK function in your main file.

You need to import the CALLBACK function in your main file.

I tougth i did it with this 2 line:

hinstDLL = LoadLibrary(TEXT("C:\\cppprojdll.dll")); 
hkprcSysMsg = (HOOKPROC)GetProcAddress(hinstDLL, "KeyboardProc");

So what's the issue your facing with? The DLL should be able to do it's job.

Well my problem is this: when i run the program , it just exits ^^, basically its not listening to keypresses, it just sets up the hook and unhook it.
I would like to make it somehow to listen to the keypresses 'till the user exits the program ...

Well my problem is this: when i run the program , it just exits

Pretty much, it's your code to blame. You do realize the fact that SetWindowsHookEx() performs an Asynchronous call. So when you Hook it, control immediately returns to the parent function.

I would like to make it somehow to listen to the keypresses 'till the user exits the program

Then Unhook when the user exits. Place the code in the EXIT part of your program or under the WM_QUIT Message.

Pretty much, it's your code to blame. You do realize the fact that SetWindowsHookEx() performs an Asynchronous call. So when you Hook it, control immediately returns to the parent function.


Then Unhook when the user exits. Place the code in the EXIT part of your program or under the WM_QUIT Message.

Well ,I'm sorry, i told you, i was verry new to c , and basically i started learning c++ 2 days ago.
It's so much more simple in c# where you can put the unhook function after the Application.Run() function,so when the application stops, the program unhooks the hook ^^.
I bet there is a similar structure in c++ too.
Anyway, please give me a working code of the main program, because imo. that would be the simpliest way, when i have it , i'll to google the parts and i'm sure i'll understand it, or if not i'll ask you :P.
Thanks!

basically i started learning c++ 2 days ago.

You should be creating a vintage Library Management Program rather a Windows Hooking Procedure. ;)

I bet there is a similar structure in c++ too.

Of course yes, you do have figured a way, for the user to Exit your program, haven't you? Put the Unhooking Procedure over there.

basically i started learning c++ 2 days ago.

You should be creating a vintage Library Management Program rather a Windows Hooking Procedure. ;)

Of course yes, you do have figured a way, for the user to Exit your program, haven't you? Put the Unhooking Procedure over there.

Well, thanks for the help, again. ^^
I tougth it'd be easier to change to c++ from c#...
I'll stick with rewriting my easier programs, atleast for a while :).
Have a nice day!
Andrew

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.