?
Solved

Keyboard hook change key

Posted on 2003-03-29
6
Medium Priority
?
749 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
  • 3
  • 3
6 Comments
 
LVL 35

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 35

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
Technology Partners: 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 35

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

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

616 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