Article View: borland.public.cppbuilder.nativeapi
Article #8141Re: Several ways to define 2 system-wide hotkeys, without to install keyboard-hook..
From: Remy Lebeau (TeamB)
Date: Fri, 19 Nov 2004 00:37
Date: Fri, 19 Nov 2004 00:37
55 lines
2842 bytes
2842 bytes
"Oren Halvani" <Spam@Me-Please.com> wrote in message
news:419d33b8$1@newsgroups.borland.com...
> extern "C" __declspec(dllexport) __stdcall void SetHook(void);
> extern "C" __declspec(dllexport) __stdcall void RemoveHook(void);
> extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
Those are supposed to be imports, not exports, when called from the main
application.
> void __fastcall TfrmMainUnit::FormDestroy(TObject *Sender)
Do not use the OnDestroy event. Use the destructor instead.
> #define WM_KEYHOOK WM_USER+100
> HHOOK ghhookKB;
> HINSTANCE ghInst;
Those should be stored in shared memory.
> extern "C" __declspec(dllexport) __stdcall DWORD CheckKey(int, WORD,LONG);
That declaration should look like this instead:
extern "C" __declspec(dllexport) LRESULT CALLBACK CheckKey(int,
WPARAM,LPARAM);
> HOOKPROC lpfnHookProc = NULL;
> lpfnHookProc = GetProcAddress(GetModuleHandle("keydll.dll"),"CheckKey");
There is no need to do that. The callback function is in the same unit as
SetHook() and RemoveHook, just pass it directly:
ghhookKB = SetWindowsHookEx(WH_KEYBOARD, CheckKey, ghInst, 0);
> DWORD __stdcall CheckKey(int nCode, WORD wParam, LONG lParam)
> {
> HWND ghAppWnd = FindWindow(0, "TKeyHookForm");
I would suggest calling that in SetHook() instead of CheckKey() .Then you
don't have to call it every time a new keystroke is typed.
> // Send key information to the main window
> SendMessage(ghAppWnd, WM_KEYHOOK, 0, lParam);
Be very careful when using SendMessage() in a hook. If the receiving
application is frozen or delayed, then the hooked application will be so as
well since SendMessage() does not return until the message is processed.
You should use PostMessage() instead when possible, or at least
SendMessageTimeout().
Gambit
Technical Details
Message-ID:
<419d3ff0@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<419ceea1@newsgroups.borland.com> <419cf173$1@newsgroups.borland.com> <419d33b8$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9a94ec9a8ef193c6 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <419ceea1@newsgroups.borland.com> <419cf173$1@newsgroups.borland.com> <419d33b8$1@newsgroups.borland.com> Subject: Re: Several ways to define 2 system-wide hotkeys, without to install keyboard-hook.. Date: Thu, 18 Nov 2004 16:37:55 -0800 Lines: 57 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <419d3ff0@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1100824560 68.69.17.41 (18 Nov 2004 16:36:00 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail