Thema: Keyboard Hook

Einzelnen Beitrag anzeigen

danten

Registriert seit: 19. Feb 2012
Ort: Czech Republic, Prag
126 Beiträge
 
Delphi 10.1 Berlin Architect
 
#1

Keyboard Hook

  Alt 4. Mär 2012, 14:16
Hello
I have a problem with the application.
I created a Keyboard Hook and wants to work on another computer than just for me.
Can you please help.

Delphi-Quellcode:
type
  TMainForm = class(TForm)
    lbl: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  MainForm: TMainForm;
  MainHook : HHOOK;
  Wnd1,Wnd2 : array[0..255] of char;

implementation

{$R *.dfm}

function GetCapsLockStatus: Integer;
begin
   Result := GetKeyState(VK_CAPITAL);
end;

function KeyboardHook(Code: Integer; wParam : WPARAM;
  lParam : LPARAM): Longint; stdcall;
var
  Buffer : TEventMsg;

  procedure TranslateKey(Key : Byte);
  begin
    if (Key <> VK_LBUTTON) and (Key <> VK_RBUTTON) then
    begin
      GetWindowText(GetForegroundWindow, Wnd2, SizeOf(Wnd2));
      if wnd1 <> wnd2 then
    begin
      Wnd1 := Wnd2;
    end;
    end;
    with MainForm do
    begin
      case Key of
        VK_RETURN : lbl.Caption := ('[Enter]');
        VK_BACK : lbl.Caption := ('[Backspace]');
        VK_ESCAPE : lbl.Caption := ('[Esc]');
        VK_SHIFT : lbl.Caption := ('[Shift]');
        VK_MENU : lbl.Caption := ('[Alt]');
        VK_CONTROL : lbl.Caption := ('[Ctrl]');
        VK_DELETE : lbl.Caption := ('[Delete]');
        VK_SPACE : lbl.Caption := ('[SPACE]');
        VK_NUMLOCK : lbl.Caption := ('[NumLock]');
        VK_SEPARATOR : lbl.Caption := ('[Separator]');
        VK_SUBTRACT : lbl.Caption := ('[Subtract]');
        VK_DECIMAL : lbl.Caption := ('[Decimal]');
        VK_DIVIDE : lbl.Caption := ('[Divide]');
        VK_SCROLL : lbl.Caption := ('[Scroll]');
        VK_LSHIFT : lbl.Caption := ('[Left Shift]');
        VK_RSHIFT : lbl.Caption := ('[Right Shift]');
        VK_LCONTROL : lbl.Caption := ('[Left Control]');
        VK_RCONTROL : lbl.Caption := ('[Right Control]');
        VK_LMENU : lbl.Caption := ('[Left Menu]');
        VK_RMENU : lbl.Caption := ('[Right Menu]');
        VK_PLAY : lbl.Caption := ('[Play]');
        VK_ZOOM : lbl.Caption := ('[Zoom]');
        VK_LBUTTON : lbl.Caption := ('[Left mouse button]');
        VK_RBUTTON : lbl.Caption := ('[Right mouse button]');
        VK_CANCEL : lbl.Caption := ('[Control-break processing]');
        VK_MBUTTON : lbl.Caption := ('[Rolout mouse button]');
        VK_TAB : lbl.Caption := ('[Tab]');
        VK_CLEAR : lbl.Caption := ('[Clear]');
        VK_PAUSE : lbl.Caption := ('[Pause]');
        VK_CAPITAL : lbl.Caption := ('[CapsLock]');
        VK_PRIOR : lbl.Caption := ('[Page UP]');
        VK_NEXT : lbl.Caption := ('[Page DOWN]');
        VK_END : lbl.Caption := ('[End]');
        VK_HOME : lbl.Caption := ('[Home]');
        VK_LEFT : lbl.Caption := ('[Left]');
        VK_RIGHT : lbl.Caption := ('[Right]');
        VK_UP : lbl.Caption := ('[Up]');
        VK_DOWN : lbl.Caption := ('[Down]');
        VK_SELECT : lbl.Caption := ('[Select]');
        VK_PRINT : lbl.Caption := ('[Print]');
        VK_EXECUTE : lbl.Caption := ('[Execute]');
        VK_SNAPSHOT : lbl.Caption := ('[Print Screen]');
        VK_INSERT : lbl.Caption := ('[Ins]');
        VK_HELP : lbl.Caption := ('[Help]');
        VK_F1 : lbl.Caption := ('[F1]');
        VK_F2 : lbl.Caption := ('[F2]');
        VK_F3 : lbl.Caption := ('[F3]');
        VK_F4 : lbl.Caption := ('[F4]');
        VK_F5 : lbl.Caption := ('[F5]');
        VK_F6 : lbl.Caption := ('[F6]');
        VK_F7 : lbl.Caption := ('[F7]');
        VK_F8 : lbl.Caption := ('[F8]');
        VK_F9 : lbl.Caption := ('[F9]');
        VK_F10 : lbl.Caption := ('[F10]');
        VK_F11 : lbl.Caption := ('[F11]');
        VK_F12 : lbl.Caption := ('[F12]');
        VK_F13 : lbl.Caption := ('[F13]');
        VK_F14 : lbl.Caption := ('[F14]');
        VK_F15 : lbl.Caption := ('[F15]');
        VK_F16 : lbl.Caption := ('[F16]');
        VK_F17 : lbl.Caption := ('[F17]');
        VK_F18 : lbl.Caption := ('[F18]');
        VK_F19 : lbl.Caption := ('[F19]');
        VK_F20 : lbl.Caption := ('[F20]');
        VK_F21 : lbl.Caption := ('[F21]');
        VK_F22 : lbl.Caption := ('[F22]');
        VK_F23 : lbl.Caption := ('[F23]');
        VK_F24 : lbl.Caption := ('[F24]');
        188 : lbl.Caption := ',';
        192 : lbl.Caption := '`';
        222 : lbl.Caption := Chr(39);
        220 : lbl.Caption := '';
        219 : lbl.Caption := '[';
        221 : lbl.Caption := ']';
        186 : lbl.Caption := ';';
        191 : lbl.Caption := '/';
        190 : lbl.Caption := '.';
        187 : lbl.Caption := '=';
        189 : lbl.Caption := '-';
        65 : lbl.Caption := 'a';
        66 : lbl.Caption := 'b';
        67 : lbl.Caption := 'c';
        68 : lbl.Caption := 'd';
        69 : lbl.Caption := 'e';
        70 : lbl.Caption := 'f';
        71 : lbl.Caption := 'g';
        72 : lbl.Caption := 'h';
        73 : lbl.Caption := 'i';
        74 : lbl.Caption := 'j';
        75 : lbl.Caption := 'k';
        76 : lbl.Caption := 'l';
        77 : lbl.Caption := 'm';
        78 : lbl.Caption := 'n';
        79 : lbl.Caption := 'o';
        80 : lbl.Caption := 'p';
        81 : lbl.Caption := 'q';
        82 : lbl.Caption := 'r';
        83 : lbl.Caption := 's';
        84 : lbl.Caption := 't';
        85 : lbl.Caption := 'u';
        86 : lbl.Caption := 'v';
        87 : lbl.Caption := 'w';
        88 : lbl.Caption := 'x';
        89 : lbl.Caption := 'y';
        90 : lbl.Caption := 'z';
        VK_NUMPAD0 : lbl.Caption := '0';
        VK_NUMPAD1 : lbl.Caption := '1';
        VK_NUMPAD2 : lbl.Caption := '2';
        VK_NUMPAD3 : lbl.Caption := '3';
        VK_NUMPAD4 : lbl.Caption := '4';
        VK_NUMPAD5 : lbl.Caption := '5';
        VK_NUMPAD6 : lbl.Caption := '6';
        VK_NUMPAD7 : lbl.Caption := '7';
        VK_NUMPAD8 : lbl.Caption := '8';
        VK_NUMPAD9 : lbl.Caption := '9';
        48 : lbl.Caption := 'é';
        49 : lbl.Caption := '+';
        50 : lbl.Caption := 'ě';
        51 : lbl.Caption := 'š';
        52 : lbl.Caption := 'č';
        53 : lbl.Caption := 'ř';
        54 : lbl.Caption := 'ž';
        55 : lbl.Caption := 'ý';
        56 : lbl.Caption := 'á';
        57 : lbl.Caption := 'í';
      end;

    if GetCapsLockStatus > 0 then
    begin
     lbl.Caption := AnsiUpperCase(lbl.Caption);
    end else
    begin
     lbl.Caption := AnsiLowerCase(lbl.Caption);
    end;
  end;
  end;

begin
  Result := 0;
  Buffer := PEventMsg(lParam)^;

  if Buffer.Message = WM_KEYDOWN then
  begin
    TranslateKey(Buffer.paramL);
  end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  MainHook := SetWindowsHookEx(WH_JOURNALRECORD, KeyboardHook, hInstance, 0);
  MainForm.Icon := Application.Icon;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
  UnhookWindowsHookEx(MainHook);
end;
Daniel
  Mit Zitat antworten Zitat