Solved

Problem in keyboard hooking

Posted on 2004-09-10
2
155 Views
Last Modified: 2010-04-05
Hi,

My keyboard program reports in log file three 'AAA' when I write only one 'A' in Winword, what is not the case when I write something in Notepad.

When I turn on another keyboard program (not my) so my program does not report anything when I write everywhere.

Please explain me?

Thank you,
nvs28111962
0
Comment
Question by:nvs28111962
[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
  • 2
2 Comments
 
LVL 12

Expert Comment

by:esoftbg
ID: 12033434
Hi nvs28111962,
Try my Keyboard-hook application .... It works fine with MS Word and Notepad, and Internet Explorer ....

unit Unit_Q_21127093;

interface

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

type
  TForm1 = class(TForm)
    btnStartHook: TSpeedButton;
    btnStopHook: TSpeedButton;
    ListView1: TListView;
    btnClear: TSpeedButton;
    EditHook: TEdit;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure btnStartHookClick(Sender: TObject);
    procedure btnStopHookClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private   { Private declarations }
  public    { Public declarations }
    procedure VisualizeKey(Key: Word);
  end;

var
  Form1: TForm1;
  Hook:  HHOOK;

implementation

{$R *.DFM}

function  HookProc(Code: Integer; wParam: DWORD; lParam: DWORD): Longint; stdcall;
var
  EvtMsg: TEventMsg;
begin
  Result := -1;
  try
    if (Code = HC_ACTION) then
    begin
      EvtMsg:=PEventmsg(lParam)^;
      if EvtMsg.message = WM_KEYDOWN then
      begin
        Form1.VisualizeKey(LOBYTE(LOWORD(EvtMsg.ParamL)));
      end;
    end;
    if (Code<0) then
    begin
      Result := CallNextHookEx(Hook, Code, wParam, lParam);
    end;
  finally
    Form1.EditHook.Text := 'On';
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Hook := 0;
  ListView1.ViewStyle := vsReport;
  ListView1.GridLines := True;
  ListView1.Font.Size := 8;
  ListView1.ReadOnly := True;
  Hook := SetWindowsHookEx(WH_JOURNALRECORD, @HookProc, hInstance, 0);
end;

procedure TForm1.VisualizeKey(Key: Word);
var
  ListItm:TListItem;
  S:      string;
  T:      string;
  R:      string;
begin
  S := IntToStr(Key);
  case Key of
    3:
    begin
      T := 'Ctrl + Pause';
      R := 'VK_Cancel';
    end;
    8:
    begin
      T := 'Backspace';
      R := 'VK_Back';
    end;
    9:
    begin
      T := 'Tab';
      R := 'VK_Tab';
    end;
    13:
    begin
      T := 'Return';
      R := 'VK_Return';
    end;
    16:
    begin
      T := 'Shift';
      R := 'VK_Shift';
    end;
    17:
    begin
      T := 'Control';
      R := 'VK_Control';
    end;
    18:
    begin
      T := 'Alt';
      R := 'VK_Menu';
    end;
    19:
    begin
      T := 'Pause';
      R := 'VK_Pause';
    end;
    20:
    begin
      T := 'Caps Lock';
      R := 'VK_Capital';
    end;
    27:
    begin
      T := 'Escape (ESC)';
      R := 'VK_Escape';
    end;
    32:
    begin
      T := 'Space';
      R := 'VK_Space';
    end;
    33:
    begin
      T := 'Prior';
      R := 'VK_Prior';
    end;
    34:
    begin
      T := 'Next';
      R := 'VK_Next';
    end;
    35:
    begin
      T := 'End';
      R := 'VK_End';
    end;
    36:
    begin
      T := 'Home';
      R := 'VK_Home';
    end;
    37:
    begin
      T := 'Cursor Left';
      R := 'VK_Left';
    end;
    38:
    begin
      T := 'Cursor Up';
      R := 'VK_Up';
    end;
    39:
    begin
      T := 'Cursor Right';
      R := 'VK_Right';
    end;
    40:
    begin
      T := 'Cursor Down';
      R := 'VK_Down';
    end;
    44:
    begin
      T := 'Print';
      R := 'VK_SnapShot';
    end;
    45:
    begin
      T := 'Ins';
      R := 'VK_Insert';
    end;
    46:
    begin
      T := 'Del';
      R := 'VK_Delete';
    end;
    48:
    begin
      T := '0';
      R := 'VK_0';
    end;
    49:
    begin
      T := '1';
      R := 'VK_1';
    end;
    50:
    begin
      T := '2';
      R := 'VK_2';
    end;
    51:
    begin
      T := '3';
      R := 'VK_3';
    end;
    52:
    begin
      T := '4';
      R := 'VK_4';
    end;
    53:
    begin
      T := '5';
      R := 'VK_5';
    end;
    54:
    begin
      T := '6';
      R := 'VK_6';
    end;
    55:
    begin
      T := '7';
      R := 'VK_7';
    end;
    56:
    begin
      T := '8';
      R := 'VK_8';
    end;
    57:
    begin
      T := '9';
      R := 'VK_9';
    end;
    65:
    begin
      T := 'A';
      R := 'VK_A';
    end;
    66:
    begin
      T := 'B';
      R := 'VK_B';
    end;
    67:
    begin
      T := 'C';
      R := 'VK_C';
    end;
    68:
    begin
      T := 'D';
      R := 'VK_D';
    end;
    69:
    begin
      T := 'E';
      R := 'VK_E';
    end;
    70:
    begin
      T := 'F';
      R := 'VK_F';
    end;
    71:
    begin
      T := 'G';
      R := 'VK_G';
    end;
    72:
    begin
      T := 'H';
      R := 'VK_H';
    end;
    73:
    begin
      T := 'I';
      R := 'VK_I';
    end;
    74:
    begin
      T := 'J';
      R := 'VK_J';
    end;
    75:
    begin
      T := 'K';
      R := 'VK_K';
    end;
    76:
    begin
      T := 'L';
      R := 'VK_L';
    end;
    77:
    begin
      T := 'M';
      R := 'VK_M';
    end;
    78:
    begin
      T := 'N';
      R := 'VK_N';
    end;
    79:
    begin
      T := 'O';
      R := 'VK_O';
    end;
    80:
    begin
      T := 'P';
      R := 'VK_P';
    end;
    81:
    begin
      T := 'Q';
      R := 'VK_Q';
    end;
    82:
    begin
      T := 'R';
      R := 'VK_R';
    end;
    83:
    begin
      T := 'S';
      R := 'VK_S';
    end;
    84:
    begin
      T := 'T';
      R := 'VK_T';
    end;
    85:
    begin
      T := 'U';
      R := 'VK_U';
    end;
    86:
    begin
      T := 'V';
      R := 'VK_V';
    end;
    87:
    begin
      T := 'W';
      R := 'VK_W';
    end;
    88:
    begin
      T := 'X';
      R := 'VK_X';
    end;
    89:
    begin
      T := 'Y';
      R := 'VK_Y';
    end;
    90:
    begin
      T := 'Z';
      R := 'VK_Z';
    end;
    96:
    begin
      T := '0';
      R := 'VK_Numpad0';
    end;
    97:
    begin
      T := '1';
      R := 'VK_Numpad1';
    end;
    98:
    begin
      T := '2';
      R := 'VK_Numpad2';
    end;
    99:
    begin
      T := '3';
      R := 'VK_Numpad3';
    end;
    100:
    begin
      T := '4';
      R := 'VK_Numpad4';
    end;
    101:
    begin
      T := '5';
      R := 'VK_Numpad5';
    end;
    102:
    begin
      T := '6';
      R := 'VK_Numpad6';
    end;
    103:
    begin
      T := '7';
      R := 'VK_Numpad7';
    end;
    104:
    begin
      T := '8';
      R := 'VK_Numpad8';
    end;
    105:
    begin
      T := '9';
      R := 'VK_Numpad9';
    end;
    106:
    begin
      T := '*';
      R := 'VK_Multiply';
    end;
    107:
    begin
      T := '+';
      R := 'VK_Add';
    end;
    109:
    begin
      T := '-';
      R := 'VK_Subtract';
    end;
    110:
    begin
      T := '.';
      R := '';
    end;
    111:
    begin
      T := '/';
      R := 'VK_Divide';
    end;
    112:
    begin
      T := 'F1';
      R := 'VK_F1';
    end;
    113:
    begin
      T := 'F2';
      R := 'VK_F2';
    end;
    114:
    begin
      T := 'F3';
      R := 'VK_F3';
    end;
    115:
    begin
      T := 'F4';
      R := 'VK_F4';
    end;
    116:
    begin
      T := 'F5';
      R := 'VK_F5';
    end;
    117:
    begin
      T := 'F6';
      R := 'VK_F6';
    end;
    118:
    begin
      T := 'F7';
      R := 'VK_F7';
    end;
    119:
    begin
      T := 'F8';
      R := 'VK_F8';
    end;
    120:
    begin
      T := 'F9';
      R := 'VK_F9';
    end;
    121:
    begin
      T := 'F10';
      R := 'VK_F10';
    end;
    122:
    begin
      T := 'F11';
      R := 'VK_F11';
    end;
    123:
    begin
      T := 'F12';
      R := 'VK_F12';
    end;
    144:
    begin
      T := 'Num';
      R := 'VK_Numlock';
    end;
    145:
    begin
      T := 'Scroll';
      R := 'VK_Scroll';
    end;
    186:
    begin
      T := ';';
      R := '';
    end;
    187:
    begin
      T := '=';
      R := '';
    end;
    188:
    begin
      T := ',';
      R := '';
    end;
    189:
    begin
      T := '-';
      R := '';
    end;
    190:
    begin
      T := '.';
      R := '';
    end;
    191:
    begin
      T := '/';
      R := '';
    end;
    192:
    begin
      T := '`';
      R := '';
    end;
    219:
    begin
      T := '[';
      R := '';
    end;
    220:
    begin
      T := '\';
      R := '';
    end;
    221:
    begin
      T := ']';
      R := '';
    end;
    222:
    begin
      T := '''';
      R := '';
    end;
  end;
  with ListView1 do
  begin
    ListItm := Items.Add;
    ListItm.Caption := S;
    ListItm.SubItems.Add(T);
    ListItm.SubItems.Add(R);
    ListItm.SubItems.Add(FormatDateTime('HH:mm:ss', Time));
  end;
end;

procedure TForm1.btnStartHookClick(Sender: TObject); // start the hook
begin
  if (Hook=0) then
    Hook := SetWindowsHookEx(WH_JOURNALRECORD, @HookProc, hInstance, 0);
end;

procedure TForm1.btnStopHookClick(Sender: TObject); // stop the hook
begin
  if (Hook<>0) then
  begin
    if UnhookWindowsHookEx(Hook) then
    begin
      Hook := 0;
      EditHook.Text := 'Off';
    end;
  end;
end;

procedure TForm1.btnClearClick(Sender: TObject);
begin
  ListView1.Items.Clear;
  ListView1.Clear;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  btnStopHookClick(Self);
end;

end.
0
 
LVL 12

Accepted Solution

by:
esoftbg earned 125 total points
ID: 12033453
you could download full code of the above example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21127093.zip
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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