Page 1 of 1

System wide Keyboard Hook

Posted: Sun Jan 22, 2006 2:42 pm
by Yogi Yang
Has any one implemented system wide keyboard hook for remapping conventional keyboard?

I have got the hooking code in a Dll created using LCC C. I was wondering if such DLLs can be used in PB or not.

If yes how.

Thanks

Posted: Sun Jan 22, 2006 4:34 pm
by SFSxOI

Posted: Mon Jan 23, 2006 5:27 am
by Yogi Yang
SFSxOI wrote:Will this help:

viewtopic.php?t=16185&highlight=hook
I have already check that article.

It would not help me here as I do not want to install and monitor HotKeys. I want my program to hook into the Keyboard so that when every a key is pressed my software should know first about and sould change the content if required.

I have a good hooking mechanism in place I was wondering if I can use it easily from PureBasic. Currently I have a small utility for personal use programmed in Delphi but want to implement that same one in PureBasic for sake of learning and getting an ideas as to how easy, usefull and efficient PB is....

Thanks

Posted: Mon Jan 23, 2006 4:41 pm
by wcardoso
look at PBSOL library http://pbosl.purearea.net/ .
There is a library to manage keyboard hooking in very easy way.
Good luck :wink:

Posted: Tue Jan 24, 2006 8:30 am
by Yogi Yang
wcardoso wrote:look at PBSOL library http://pbosl.purearea.net/ .
There is a library to manage keyboard hooking in very easy way.
Good luck :wink:
Thanks for the tip.
I will go through it. What actuall I want to do is to use the Hooking mechanism that I have with me in PB.

Thanks again.

Posted: Wed Jan 25, 2006 1:00 pm
by SFSxOI
Yogi Yang,

What did you finally end up using?

Posted: Wed Jan 25, 2006 3:59 pm
by Yogi Yang
I have not implemented anything in PB. My original implementation is in Delphi 3.

I want to implement in PB but the mechanism that is implemented is using memory mapped files to map into target applications memory space.

The code is Delphi is something like this:

procedure TForm1.FormCreate(Sender: TObject);
begin
sharedFileHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, 256, 'YogiSharedFile');
sharedData := MapViewOfFile(sharedFileHandle, FILE_MAP_WRITE, 0, 0, 0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnmapViewOfFile(sharedData);
CloseHandle(sharedFileHandle);
end;
// --------- This routine is executed (called) when ever user presses a key in any windows based apps

procedure TForm1.main(var msg: TMessage);
begin
msg.Result := 0; // the dll should not touch this keypress
LabelHandle.Caption := IntToStr(sharedData[0]); // handle of the current window
GHwnd := sharedData[0];
if char(msg.wParam) = 'Q' then begin
msg.Result := 1; // tell the dll to kill the original keypress

// *** THIS IS LINE IS REQUIRED TO
// *** REMAP ONLY KEYDOWNS
// *** THE MEAINING OF THE BITS CAN
// *** BE FOUND IN THE WIN32SDK HELP
// *** UNDER KEYWORD: "KeyboardProc"

if (msg.LParam and $80000000) = 0 then
PostMessage(sharedData[0], WM_CHAR, ord('*'), 1);
end;

if char(msg.wParam) = 'Z' then begin
msg.Result := 1; // tell the dll to kill the original keypress
//Delete previously typed character and insert another one
if (msg.LParam and $80000000) = 0 then begin
SendMessage(GHwnd, WM_KEYDOWN, VK_BACK, 0);
PostMessage(GHwnd, WM_CHAR, VK_BACK, 1);

SendMessage(sharedData[0], WM_KEYDOWN, $1FFF0001, 0);
PostMessage(sharedData[0], WM_CHAR, ord('M'), 1);

end;
end;

If you observe the code the implemeted Hook passes all the messages regarding keyboard to the delphi form. How can such a thing be possible in case of PB where forms are created in a way that I am not aware of. :(

Posted: Mon Nov 13, 2006 3:19 pm
by mp303
wcardoso wrote:look at PBSOL library http://pbosl.purearea.net/ .
There is a library to manage keyboard hooking in very easy way.
Good luck :wink:
I searched through the libraries, but I can't find it - can you tell me where it is?

thanks!