Solved

Hook keyboard.

Posted on 2001-08-16
6
396 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

22 Experts available now in Live!

Get 1:1 Help Now