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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

get all keys pressed

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
issamtaher
Asked:
issamtaher
1 Solution
 
Lee_NoverCommented:
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
 
lopemCommented:
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
 
craig_capelCommented:
Listening
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
lopemCommented:
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
 
gandalf_the_whiteCommented:
listening...
0
 
sudhakar_koundinyaCommented:
listening
0
 
SpideyModCommented:
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now