Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Hook keyboard.

Posted on 2001-08-16
6
Medium Priority
?
411 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 400 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
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!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline
Suggested Courses

885 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