atomic80
asked on
KeyBoard Hook with Borland C++ Builder
Hello,
sorry for my bad english i am from germany.
Now my question.
Can anybody say me how i can write a simple program in "Borland C++ Builder" that count the key strokes in the system ?
Thanks
sorry for my bad english i am from germany.
Now my question.
Can anybody say me how i can write a simple program in "Borland C++ Builder" that count the key strokes in the system ?
Thanks
ASKER
Yes, it works in C++ Builder!
I have tested so many other samples but this works ;-)
Thanks
I have tested so many other samples but this works ;-)
Thanks
It's good to hear you can make it work. If you don't have additional question you can close this question by accepting my comments as answer. In the future I hope to help you with any additional question.
ASKER
I have one question more ;-)
This is my source code:
#pragma data_seg(".shared")
HHOOK hHook = NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:. shared,RWS ")
HWND g_hWnd = NULL;
HINSTANCE g_hDll = NULL;
//######################## ########## ########## ########## ########## ########## #
LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0) CallNextHookEx(hHook, nCode, wParam, lParam);
if(nCode == HSHELL_WINDOWCREATED)
{
SendMessage(g_hWnd, WM_USER, 0, 0);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//------------------------ ---------- ---------- ---------- ---------- ---------- -
//######################## ########## ########## ########## ########## ########## #
bool _export SetHooks(HWND hwnd)
{
if(hHook == NULL)
{
g_hWnd = hwnd;
hHook = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDll, 0);
if(hHook == NULL) return false;
}
return true;
}
//------------------------ ---------- ---------- ---------- ---------- ---------- -
//######################## ########## ########## ########## ########## ########## #
void _export RemoveHooks()
{
if(hHook != NULL)
{
UnhookWindowsHookEx(hHook) ;
hHook = NULL;
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- -
//######################## ########## ########## ########## ########## ########## #
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
g_hDll = hinst;
return DisableThreadLibraryCalls( g_hDll);
case DLL_PROCESS_DETACH:
RemoveHooks();
default:
return TRUE;
}
}
//------------------------ ---------- ---------- ---------- ---------- ---------- -
It all works but i have a problem with SendMessage.
I will send a Message to my program when a event occurse but it do not work.
This is my source code:
#pragma data_seg(".shared")
HHOOK hHook = NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.
HWND g_hWnd = NULL;
HINSTANCE g_hDll = NULL;
//########################
LRESULT CALLBACK ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode < 0) CallNextHookEx(hHook, nCode, wParam, lParam);
if(nCode == HSHELL_WINDOWCREATED)
{
SendMessage(g_hWnd, WM_USER, 0, 0);
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
//------------------------
//########################
bool _export SetHooks(HWND hwnd)
{
if(hHook == NULL)
{
g_hWnd = hwnd;
hHook = SetWindowsHookEx(WH_SHELL,
if(hHook == NULL) return false;
}
return true;
}
//------------------------
//########################
void _export RemoveHooks()
{
if(hHook != NULL)
{
UnhookWindowsHookEx(hHook)
hHook = NULL;
}
}
//------------------------
//########################
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
switch(reason)
{
case DLL_PROCESS_ATTACH:
g_hDll = hinst;
return DisableThreadLibraryCalls(
case DLL_PROCESS_DETACH:
RemoveHooks();
default:
return TRUE;
}
}
//------------------------
It all works but i have a problem with SendMessage.
I will send a Message to my program when a event occurse but it do not work.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello,
here the source from the sample:
#include "stdafx.h"
#include "keydll3.h"
#pragma data_seg(".HOOKDATA")//Sha red data among all instances.
HHOOK hook = NULL;
HWND hwnd = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.HOOKDATA,RWS")/ /linker directive
HINSTANCE hinstance = NULL;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
hinstance = (HINSTANCE)hModule;
hook = NULL;
return TRUE;
}
KEYDLL3_API void installhook(HWND h)
{
hook = NULL;
hwnd = h;
hook = SetWindowsHookEx(WH_KEYBOA RD,hookpro c,hinstanc e,NULL);
if(hook==NULL)
MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
}
KEYDLL3_API void removehook()
{
UnhookWindowsHookEx(hook);
}
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(ncode>=0)
{
if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
{
hwnd = FindWindow("#32770","Keylo gger Exe");//Find application window handle
PostMessage(hwnd,WM_USER+7 55,wparam, lparam);// Send info to app Window.
}
}
return ( CallNextHookEx(hook,ncode, wparam,lpa ram) );//pass control to next hook in the hook chain.
}
This sample is bad.
You give installhook the window - handle and the function save it but by any call of hookproc the handle is overwrited by FindWindow. ?????
Why i give the handle when the hookproc - function generates it self ?
here the source from the sample:
#include "stdafx.h"
#include "keydll3.h"
#pragma data_seg(".HOOKDATA")//Sha
HHOOK hook = NULL;
HWND hwnd = NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:.HOOKDATA,RWS")/
HINSTANCE hinstance = NULL;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
hinstance = (HINSTANCE)hModule;
hook = NULL;
return TRUE;
}
KEYDLL3_API void installhook(HWND h)
{
hook = NULL;
hwnd = h;
hook = SetWindowsHookEx(WH_KEYBOA
if(hook==NULL)
MessageBox(NULL,"Unable to install hook","Error!",MB_OK);
}
KEYDLL3_API void removehook()
{
UnhookWindowsHookEx(hook);
}
KEYDLL3_API LRESULT CALLBACK hookproc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(ncode>=0)
{
if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
{
hwnd = FindWindow("#32770","Keylo
PostMessage(hwnd,WM_USER+7
}
}
return ( CallNextHookEx(hook,ncode,
}
This sample is bad.
You give installhook the window - handle and the function save it but by any call of hookproc the handle is overwrited by FindWindow. ?????
Why i give the handle when the hookproc - function generates it self ?
ASKER
I found the error!
SendMessage don't work but PostMessage!
SendMessage don't work but PostMessage!
ASKER
And the FindWindow do not need. Bad!
Thanks
Thanks
https://www.experts-exchange.com/questions/20453601/Getting-keyboard-input-from-unfocussed-windows.html