Solved

Hook keyboard.

Posted on 2001-08-16
6
398 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now