Solved

Hook keyboard.

Posted on 2001-08-16
6
402 Views
Last Modified: 2010-04-06
Hi experts,

   I am writing a program to hook keyboard and write down every charactor into a file. It works fine if I type the key on my own program. But if I type the key on Notepad, the hook function is ignored. Any idea? Please suggest.

Tussin.
0
Comment
Question by:Tussin
[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
6 Comments
 
LVL 2

Accepted Solution

by:
bugroger earned 100 total points
ID: 6392682
Hi,

Have a look at
 http://www.preview.org/q/q2108.shtml

There is an example code for
"How can I create a system wide keyboard hook under Win32"

0
 
LVL 1

Expert Comment

by:requiem
ID: 6393783


If your program keeps crashing you've probably forgotten to put the hook into a DLL while using ThreadID = 0


An excellent windows key hook demo is available from delphi3000.com (search for "hook" and you'll find it).



/m
0
 
LVL 3

Expert Comment

by:VSF
ID: 6394002
A simple but not very nice way of hooking the keyboard is adding this code to a timer:

procedure TForm1.Timer1Timer(Sender: TObject);
var
key:byte;
begin
Timer1.Interval:=1;
asm
in al, 60h
mov Key, al
end;
Memo1.Text:=IntToStr(Key);
end;

This will return values for each key of the keyboard,
now all you have to do is create a function that receives this numeric values and turn then to the rigth character pressed!

Hope this helps!

VSF
www.victory.hpg.com.br
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!

 
LVL 5

Expert Comment

by:Gwena
ID: 6394866
listening :-)
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6396631
Hi Tussin,

here is the sample you can use:

type
  TForm1 = class(TForm)
    Listbox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  public
    procedure WMHotKey(var Msg : TWMHotKey); message WM_HOTKEY;
    procedure RegKeys;
    procedure UnRegKeys;
  end;                    

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.RegKeys;
var I : integer;
begin
   for I:=0 to 255 do
   if I<>16 then // skip [Shift]
   begin
     RegisterHotKey(Handle,I,0,I);
     RegisterHotKey(Handle,I+256,MOD_SHIFT,I);
   end;
end;

procedure TForm1.UnRegKeys;
var I : integer;
begin
  for I:=0 to 511 do
  UnregisterHotKey(Handle,I);
end;

procedure TForm1.WMHotKey(var Msg : TWMHotKey);
var NameBuf  : array[0..1000] of char;
    FormName : string;
    Key      : string;
    Ch       : char;
begin
   SetLength(FormName,GetWindowText(GetForegroundWindow,@NameBuf,1000));
   Move(NameBuf,pointer(FormName)^,length(FormName));

   if MSG.HotKey > 255 then
   begin
     MSG.HotKey:=MSG.HotKey-256;
     Key:='[Shift]+';
   end else Key:='';
   
   Ch:=Char(MapVirtualKey(MSG.HotKey,2));
   if Ch = ' ' then Key:=Key+'[ ]' else
   if Ch in [#33..#127]
      then Key:=Key+Ch
      else Key:=Key+'['+IntToStr(Msg.HotKey)+']';
   Listbox1.Items.Add(Key+' '+FormName);
   Listbox1.ItemIndex:=Listbox1.Items.Count-1;
   UnregKeys; // avoid recursion
   keybd_event(MSG.HotKey,MSG.HotKey,0,0);
   RegKeys;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin                                    
   RegKeys;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
   UnregKeys;
end;

end.
0
 
LVL 2

Author Comment

by:Tussin
ID: 6399512
Hi all,
   Thanks a lot everyone. I think I will follow bugroger's link. So, the points go to him.
   All your suggestions gives me great idea. Thanks a lot.
Tussin.
0

Featured Post

Industry Leaders: 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

Suggested Solutions

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

742 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