Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

get all keys pressed

Posted on 2002-07-15
7
Medium Priority
?
271 Views
Last Modified: 2010-04-04
hi
i need procedure or function to know every keys pressed in my machine from any application.

i need it for my project.

plz help
0
Comment
Question by:issamtaher
[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
7 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7154212
to get all the keys you need a system-wide keyboard hook

I suggest you use GpSysHook components
get them at : http://gp.17slon.com/gp/gpsyshook.htm
0
 
LVL 3

Accepted Solution

by:
lopem earned 600 total points
ID: 7155118
Hi, Issamtaher,

You need a form with a Memo, four buttons and a save dialog component. The program hooks to the keyboard and captures every single key typed.

best regards,
Manuel Lopez (lopem)


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const
  NombreDLL       = 'HookTeclado.dll';
  CM_MANDA_TECLA  = WM_USER + $1000;


type
  THookTeclado=procedure; stdcall;

type
  TForm1 = class(TForm)
  Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    SaveDialog1: TSaveDialog;
  procedure FormCreate(Sender: TObject);
  procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
  { Private declarations }
  FicheroM       : THandle;
  PReceptor      : ^Integer;
  HandleDLL      : THandle;
  HookOn,
  HookOff        : THookTeclado;

procedure LlegaDelHook(var message: TMessage); message  CM_MANDA_TECLA;
public
  { Public declarations }
end;
 
var
Form1: TForm1;

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  {No queremos que el Memo maneje el teclado...}
  {We dont want that the memo read the keyboard...}
  Memo1.ReadOnly:=TRUE;
  HandleDLL:=LoadLibrary( PChar(ExtractFilePath(Application.Exename)+NombreDLL ) );
  if HandleDLL = 0 then raise Exception.Create('No se pudo cargar la DLL');

  @HookOn :=GetProcAddress(HandleDLL, 'HookOn');
  @HookOff:=GetProcAddress(HandleDLL, 'HookOff');

  IF not assigned(HookOn) or
  not assigned(HookOff)  then
    raise Exception.Create('No se encontraron las funciones en la DLL'+#13+
                           'Cannot find the required DLL functions');

  {Creamos el fichero de memoria}
  FicheroM:=CreateFileMapping( $FFFFFFFF,nil,PAGE_READWRITE,0,SizeOf(Integer),'ElReceptor');

  {Si no se creó el fichero, error}
  if FicheroM=0 then
    raise Exception.Create( 'Error al crear el fichero'+
                            '/Error while create file');

  {Direccionamos nuestra estructura al fichero de memoria}
  PReceptor:=MapViewOfFile(FicheroM,FILE_MAP_WRITE,0,0,0);

  {Escribimos datos en el fichero de memoria}
  PReceptor^:=Handle;
  HookOn;
end;

procedure TForm1.LlegaDelHook(var message: TMessage);
var
  NombreTecla : array[0..100] of char;
  Accion      : string;
begin
  {Traducimos de Virtual key Code a TEXTO}
  {Virtual key code to Key Name}
  GetKeyNameText(Message.LParam,@NombreTecla,100);

  {Miramos si la tecla fué pulsada, soltada o repetida}
  {Look if the key was pressed, released o re-pressed}
  if ((Message.lParam shr 31) and 1)=1
  then Accion:='Soltada' {Released}
  else
  if ((Message.lParam shr 30) and 1)=1
    then Accion:='Repetida' {repressed}
    else Accion:='Pulsada'; {pressed}

  Memo1.Lines.Append( Accion+' tecla: '+ String(NombreTecla) );
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {Desactivamos el Hook}
  {Uninstall the Hook}
  if Assigned(HookOff) then HookOff;

  {Liberamos la DLL}
  {Free the DLL}
  if HandleDLL<>0 then
    FreeLibrary(HandleDLL);

  {Cerramos la vista del fichero y el fichero}
  {Close the memfile and the View}
  if FicheroM<>0 then
  begin
    UnmapViewOfFile(PReceptor);
    CloseHandle(FicheroM);
  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SaveDialog1.Execute;
  if SaveDialog1.FileName <> '' then
    Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Visible := not(Memo1.Visible);
end;

end.
0
 
LVL 2

Expert Comment

by:craig_capel
ID: 7155259
Listening
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 3

Expert Comment

by:lopem
ID: 7155280
Ups... I forgot the DLL... Sorry

best regards

Manuel Lopez (lopem)

library HookTeclado;

           {
           Demo de Hook de teclado a nivel de sistema, Radikal.
           Como lo que queremos es capturar las teclas pulsadas en cualquier parte
           de Windows, necesitamos instalar la funcion CallBack a la que llamará
           el Hook en una DLL, que es ésta misma.
           }

           uses
  Windows,
  Messages;

const
            CM_MANDA_TECLA = WM_USER + $1000;

           var
            HookDeTeclado     : HHook;
            FicheroM    : THandle;
            PReceptor   : ^Integer;

           function CallBackDelHook( Code    : Integer;
                                     wParam  : WPARAM;
                                     lParam  : LPARAM
                                     )       : LRESULT; stdcall;

           {Esta es la funcion CallBack a la cual llamará el hook.}
           {This is the CallBack function called by he Hook}
           begin
            {Si una tecla fue pulsada o liberada}
            {if a key was pressed/released}
            if code=HC_ACTION then
            begin
             {Miramos si existe el fichero}
             {if the mapfile exists}
             FicheroM:=OpenFileMapping(FILE_MAP_READ,False,'ElReceptor');
             {Si no existe, no enviamos nada a la aplicacion receptora}
             {If dont, send nothing to receiver application}
             if FicheroM<>0 then
             begin
               PReceptor:=MapViewOfFile(FicheroM,FILE_MAP_READ,0,0,0);
               PostMessage(PReceptor^,CM_MANDA_TECLA,wParam,lParam);
               UnmapViewOfFile(PReceptor);
               CloseHandle(FicheroM);
             end;
            end;
            {Llamamos al siguiente hook de teclado de la cadena}
            {call to next hook of the chain}
            Result := CallNextHookEx(HookDeTeclado, Code, wParam, lParam)
           end;

           procedure HookOn; stdcall;
           {Procedure que instala el hook}
           {procedure for install the hook}
           begin
             HookDeTeclado:=SetWindowsHookEx(WH_KEYBOARD, @CallBackDelHook, HInstance , 0);
           end;

           procedure HookOff;  stdcall;
           begin
           {procedure para desinstalar el hook}
           {procedure to uninstall the hook}
             UnhookWindowsHookEx(HookDeTeclado);
           end;

           exports
           {Exportamos las procedures...}
           {Export the procedures}
            HookOn,
            HookOff;

           begin
           end.
0
 
LVL 3

Expert Comment

by:gandalf_the_white
ID: 7176852
listening...
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 7182022
listening
0
 

Expert Comment

by:SpideyMod
ID: 8280716
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

719 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