Article View: borland.public.cppbuilder.nativeapi
Article #2987Establishing a Keyboard Hook.........
From: qyte
Date: Mon, 04 Sep 2006 21:48
Date: Mon, 04 Sep 2006 21:48
70 lines
2530 bytes
2530 bytes
I have built the following DLL...
#include <vcl.h>
#include <windows.h>
#pragma hdrstop
#pragma argsused
#define UM_KEYB (WM_USER+11069)
static HHOOK hkb=NULL;
HWND Handle;
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
return 1;
}
//---------------------------------------------------------------------------
__declspec(dllexport) LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
{
PostMessage(Handle,UM_KEYB,wParam,lParam);
return CallNextHookEx( hkb, nCode, wParam, lParam );
}
__declspec(dllexport) bool installhook(HWND hwnd)
{
hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,HInstance,0);
Handle = hwnd;
return hkb;
}
__declspec(dllexport) bool UnHook()
{
return UnhookWindowsHookEx(hkb);
}
and on the App now...
int y=0;
__declspec(dllimport) LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam);
__declspec(dllimport) bool installhook(HWND hwnd);
__declspec(dllimport) bool UnHook();
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
installhook(Handle);
}
void __fastcall TForm1::WndProc(TMessage &Message)
{
if(Message.Msg==UM_KEYB)
{
Caption = y++;
Message.Result=true;
}
else
TForm::WndProc(Message);
}
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
UnHook();
}
Why is this not working? What on earth am i doing wrong?
It only captures keystrokes from within my application...
I Want a system-wide hook...
Technical Details
Message-ID:
<44fc9f3a@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,25dce3f6aa4fede9,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 05 Sep 2006 00:48:45 +0300 From: qyte <gyte@vivodinet.gr> User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Establishing a Keyboard Hook......... Content-Type: text/plain; charset=ISO-8859-7; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 88.218.20.114 Message-ID: <44fc9f3a@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1157406522 88.218.20.114 (4 Sep 2006 14:48:42 -0700) Lines: 70 Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nwr.nac.net!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail