Solved

get all keys pressed

Posted on 2002-07-15
7
263 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Title # Comments Views Activity
Strange code, can use it, but i cant figure out what it does. 3 65
Simple Delphi Question 9 95
Internet Explorer View Settings Question 15 116
Delphi Form ownership 4 107
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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

792 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