Solved

Hook keyboard.

Posted on 2001-08-16
6
403 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
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!

 
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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses
Course of the Month5 days, 8 hours left to enroll

626 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