Article View: comp.lang.pascal.delphi.misc
Article #476How can I change/remap keys from inside my keyboard hook? (I've been searching all day...)
From: jklimek
Date: Wed, 12 Jan 2005 19:21
Date: Wed, 12 Jan 2005 19:21
59 lines
3254 bytes
3254 bytes
I've written a global keyboard hook (which works perfectly fine) but
I'm trying to remap keys as they are entered...
For example, if somebody types the letter 'G', I want to change that
'G' into a 'K' (etc, etc)...
I've been searching all day long and I've read about using keybd_event
and I've tried the following code, but I enter an infinite loop of some
sort. Please help...
function ProcessKeyBoardHook(code: integer; wParam: word; lParam:
longword): longword; stdcall;
var
KeybdState : TKeyboardState; // array[0..255] of byte
charcode : longword;
fromKeybdEvt: Word;
begin
if (code < 0) then begin //if code is <0 your keyboard hook should
always run CallNextHookEx instantly and
result := CallNextHookEx(HookInformation^.HookHandle, code, wParam,
lparam); //then return the value from it.
Exit;
end;
fromKeybdEvt := (lParam shr 16);
// Process the hook here:
// Only react to WM_KEYDOWN (check 32bit in lParam)
if (lParam and (1 shl 31)) = 0 then begin
GetKeyboardState(KeybdState);
if ToASCII(wParam, HiWord(lParam), KeybdState, @CharCode, 0) = 1
then begin
PostMessage(HookInformation^.AppHandle, WM_KEYPRESSED, 0,
CharCode);
writeDebug('Key pressed: ' + IntToStr(wparam), 'c:\log.txt');
// Send an 'E' key to the keyboard buffer
keybd_event(ord('E'), 0, 0, 0);
keybd_event(ord('E'), 0, KEYEVENTF_KEYUP, 0);
end;
end;
// PostMessage(HookInformation^.AppHandle, WM_KEYPRESSED, ch,
lParam);
CallNextHookEx(HookInformation^.HookHandle, code, wParam, lparam);
//call the next hook proc if there is one
// I found this code on google groups-- it's supposed to prevent the
// infinite loop by allowing keybd_events to pass and everything else
// to be trapped.
if ((fromKeybdEvt and $FF00) = $C100) then
result := 0 // let it pass, it's from keybd_event
else
result := 1; // trap/erase this keypress
exit;
end;
Technical Details
Message-ID:
<1105557699.667151.276930@z14g2000cwz.googlegroups.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!z14g2000cwz.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10baee,797761edfabd18bb,start X-Google-Thread: 111659,797761edfabd18bb,start X-Google-Thread: 10261c,797761edfabd18bb,start X-Google-Thread: 1091e5,797761edfabd18bb,start X-Google-Thread: 113dfb,797761edfabd18bb,start X-Google-Attributes: gid10baee,gid111659,gid10261c,gid1091e5,gid113dfb,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!postnews.google.com!z14g2000cwz.googlegroups.com!not-for-mail From: jklimek@gmail.com Newsgroups: comp.lang.pascal.delphi.misc,comp.lang.pascal.borland,comp.lang.pascal.misc,borland.public.delphi.winapi,comp.os.ms-windows.programmer.win32 Subject: How can I change/remap keys from inside my keyboard hook? (I've been searching all day...) Date: 12 Jan 2005 11:21:39 -0800 Organization: http://groups.google.com Lines: 60 Message-ID: <1105557699.667151.276930@z14g2000cwz.googlegroups.com> NNTP-Posting-Host: 199.224.104.66 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1105557703 30899 127.0.0.1 (12 Jan 2005 19:21:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 12 Jan 2005 19:21:43 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: z14g2000cwz.googlegroups.com; posting-host=199.224.104.66; posting-account=7tqE2w0AAACjSCqR-RvAtrIN-mCQw6QI