Solved

get all keys pressed

Posted on 2002-07-15
7
256 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
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 200 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

14 Experts available now in Live!

Get 1:1 Help Now