Solved

get all keys pressed

Posted on 2002-07-15
7
264 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 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
Independent Software Vendors: 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

Independent Software Vendors: 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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 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