Thread View: comp.lang.pascal.delphi.misc
4 messages
4 total messages
Started by jklimek
Wed, 12 Jan 2005 19:21
How can I change/remap keys from inside my keyboard hook? (I've been searching all day...)
Author: 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;
Re: How can I change/remap keys from inside my keyboard hook? (I've been searching all day...)
Author: Ante Smolcic
Date: Thu, 13 Jan 2005 09:39
Date: Thu, 13 Jan 2005 09:39
46 lines
3020 bytes
3020 bytes
In article <1105557699.667151.276930@z14g2000cwz.googlegroups.com>, jklimek@gmail.com says... > 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... Hi, I haven't had much experience with hooks. My first and only project had problems with freezing the system after a while. Few years later I discovered the problem was in Delphi's handling of dll functions that use the FPU. Anyway, I didn't use keyboard hook but a global message hook and just changed the key value in the message record. I never had the chance to look into it further. Anyway, I had this link http://www.gajits.com/p_delphihooks.html but it doesn't work anymore, fortunately, I have it dowloaded on my disk, so if you want it, send me a mail to: supersmola - inet - hr (replace dashes with @ and .) As far as the infinite loop, it obviously has something to do with this statement, right: if ((fromKeybdEvt and $FF00) = $C100) then Aparently it returns false. You may try logging the fromKeybdEvt value and see what it is. Another hint - I think I saw a KeyboardHoook demo in the JCL od JCL-VCL package. And what is HookInformation? It's not declared anywhare. -- Volume six, ha, ha, ha.
Re: How can I change/remap keys from inside my keyboard hook? (I've been searching all day...)
Author: Thaddy de Koning
Date: Fri, 14 Jan 2005 09:18
Date: Fri, 14 Jan 2005 09:18
7 lines
1960 bytes
1960 bytes
You can not use messages, because you would fall back into the hook thus creating an endless loop. You should map the whole keyboard and xlat them into YOUR keyboad lay out ( do it just once when the hook is installed) and look up from the hook. The thins you need are the win32 api GetKeyboardstate and SetKeyboardstate. These use very easy to handle arrays so it isnt difficult at all
Re: How can I change/remap keys from inside my keyboard hook? (I've been searching all day...)
Author: Thaddy de Koning
Date: Fri, 14 Jan 2005 09:21
Date: Fri, 14 Jan 2005 09:21
10 lines
2134 bytes
2134 bytes
Thaddy de Koning wrote: > You can not use messages, because you would fall back into the hook thus > creating an endless loop. > You should map the whole keyboard and xlat them into YOUR keyboad lay > out ( do it just once when the hook is installed) and look up from the > hook. > The thins you need are the win32 api GetKeyboardstate and > SetKeyboardstate. These use very easy to handle arrays so it isnt > difficult at all And MapVirtualKey do do the mapping! sorry missed that part before posting
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads