🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8139

Re: Several ways to define 2 system-wide hotkeys, without to install keyboard-hook..

#8139
From: Oren Halvani
Date: Thu, 18 Nov 2004 23:43
93 lines
3658 bytes
Remy what do you think about this keyboard hook...

// in application unit:
/*******************************************************/

TfrmMainUnit *frmMainUnit;

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);

__fastcall TfrmMainUnit::TfrmMainUnit(TComponent* Owner) : TForm(Owner)
{
   SetHook();
}

void __fastcall TfrmMainUnit::FormDestroy(TObject *Sender)
{
   RemoveHook();
}

void __fastcall TfrmMainUnit::KeyHook(TMessage &Message)
{
  char Key[80];
  GetKeyNameText(Message.LParam, Key, 80);
  ListBox1->Items->Add(Key);
}

// in application header:

private:
          MESSAGE void __fastcall KeyHook(TMessage &Message);
          BEGIN_MESSAGE_MAP
             MESSAGE_HANDLER(WM_KEYHOOK, TMessage, KeyHook);
          END_MESSAGE_MAP(TForm);
/*******************************************************/


// in keyboard - dll unit:
/*******************************************************/
#include <vcl.h>
#include <windows.h>
#pragma hdrstop

#define WM_KEYHOOK WM_USER+100
HHOOK ghhookKB;
HINSTANCE ghInst;
#pragma argsused

int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
{
   ghInst = hinst;
   return (1);
}

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);

void __stdcall SetHook(void)
{
  HOOKPROC lpfnHookProc = NULL;
  lpfnHookProc = GetProcAddress(GetModuleHandle("keydll.dll"),"CheckKey");
  ghhookKB = SetWindowsHookEx(WH_KEYBOARD, lpfnHookProc, ghInst, NULL);
}

void __stdcall RemoveHook(void)
{
  UnhookWindowsHookEx(ghhookKB);
}

DWORD __stdcall CheckKey(int nCode, WORD wParam, LONG lParam)
{
  HWND ghAppWnd = FindWindow(0, "TKeyHookForm");
  if((nCode < 0) || nCode == HC_NOREMOVE)
    return CallNextHookEx(ghhookKB, nCode, wParam, lParam);

  // Skip if it's a repeat
  if(lParam & 0x40000000) return CallNextHookEx(ghhookKB, nCode, wParam,
lParam);

  // Send key information to the main window
  SendMessage(ghAppWnd, WM_KEYHOOK, 0, lParam);

  return CallNextHookEx(ghhookKB, nCode, wParam, lParam);
}
/*******************************************************/

it's just a simple hook to display the pressed keys..is there something
that could be optimized..? what do you think..?


Oren
Technical Details
Message-ID: <419d33b8$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!transit.nntp.hccnet.nl!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <419ceea1@newsgroups.borland.com> <419cf173$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
Reply-To: "Oren Halvani" <Spam@Me-Please.com>
From: "Oren Halvani" <Spam@Me-Please.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <419ceea1@newsgroups.borland.com> <419cf173$1@newsgroups.borland.com>
Subject: Re: Several ways to define 2 system-wide hotkeys, without to install keyboard-hook..
Date: Fri, 19 Nov 2004 00:43:03 +0100
Lines: 95
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 217.234.123.210
Message-ID: <419d33b8$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1100821432 217.234.123.210 (18 Nov 2004 15:43:52 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!transit.nntp.hccnet.nl!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail