?
Solved

Keyboard hook change key

Posted on 2003-03-29
6
Medium Priority
?
719 Views
Last Modified: 2010-04-04
I have WndProc(var Msg: TMessage) procedure
Msg.Msg contains Windows message (WM_CHAR)
Msg.WParam contains the ASCII Character code of the key pressed on the keyboard
I intercepted the key message but how do I prevent windows from procesing the key or
change the key to another key?
0
Comment
Question by:mr_nix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 34

Expert Comment

by:Slick812
ID: 8232906
hello mr_nix, the WM_CHAR messages in the WndProc are not used by that control until Inherited is called by that function, so you can Exit and Not call Inherited  OR  Replace the Charater with  #0  so nothing will happen -


procedure TForm1.WndProc(var Message: TMessage);
begin
case Message.Msg of
  WM_CHAR: if Message.wParam = 32 then Exit // No Space char
           else
           if Message.wParam = 65 then Message.wParam := 0 // no A char
           else
           if Message.wParam = 97 then Message.wParam := 65; // a becomes A

  end;
inherited;
end;
0
 

Author Comment

by:mr_nix
ID: 8248178
Code works fine in Application.OnMesseage but WndProc needs to be global. Like when you press a key in Notepad I can intercept and change before Notepad gets the message.
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 105 total points
ID: 8249033
here is a page with some code for a KeyBoard Hook -
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20347433.html

this Hook does not change or block the Key messages, if you do NOT call
Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
then the Keystrok will be blocked, if you change the
KeyStroke
parameter then the Key will be changed

there is a web page here -

http://delphi.about.com/library/bluc/text/uc063001a.htm

with some info on making hooks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mr_nix
ID: 8264746
I see in your source
Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
{I moved the CallNextHookEx up here but if you want to block or change any keys then move it back down}

where do I move it down to?
0
 
LVL 34

Expert Comment

by:Slick812
ID: 8266310
it looks like you have no Idea what's going on in that fubction. . .

function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall;
var
KeyState1: TKeyBoardState;
AryChar: Array[0..1] of Char;
Count: Integer;
begin
Result := 0;
if Code = HC_NOREMOVE then Exit;

if Code < 0 then
Exit;

if Code = HC_ACTION then
 begin
 if ((KeyStroke and (1 shl 30)) <> 0) then
 if not IsWindow(hMemo) then
 begin
{I moved the OpenFileMapping up here so it would not be opened
unless the app the DLL is attatched to gets some Key messages}
 hMemFile := OpenFileMapping(FILE_MAP_WRITE, False, 'Global7v9k');
 PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
   if PHookRec1 <> nil then
     begin
     hMemo := PHookRec1.MemoHnd;
     hApp := PHookRec1.AppHnd;
     end;
 end;
 if ((KeyStroke and (1 shl 30)) <> 0) then
   begin
   GetKeyboardState(KeyState1);
   Count := ToAscii(VirtualKey,KeyStroke, KeyState1, AryChar, 0);
   if Count = 1 then
     begin
     if AryChar[0] = '?' then Exit;
{Exit so CallNextHookEx will not be called}
     end;
   end;
 end;
Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
end;
0
 

Author Comment

by:mr_nix
ID: 8266397
thanks for your prompt answers. I understand now.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question