Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Multimedia (https://www.delphipraxis.net/16-multimedia/)
-   -   Keyboard Hook (https://www.delphipraxis.net/166888-keyboard-hook.html)

danten 4. Mär 2012 13:16

Keyboard Hook
 
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 := '&#283;';
        51             :   lbl.Caption := 'š';
        52             :   lbl.Caption := '&#269;';
        53             :   lbl.Caption := '&#345;';
        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;

Luckie 4. Mär 2012 13:25

AW: Keyboard Hook
 
Zitat:

Zitat von danten (Beitrag 1154425)
I created a Keyboard Hook and wants to work on another computer than just for me.

Sorry but I don't understand what you mean esspecially the part after the "and" does not make any sense to me.

Sir Rufo 4. Mär 2012 13:26

AW: Keyboard Hook
 
Could u give us more details on "i have a problem"?

danten 4. Mär 2012 13:33

AW: Keyboard Hook
 
Liste der Anhänge anzeigen (Anzahl: 1)
When I run the program on a PC with Windows XP, the program does not work.
When I run the program on a PC with Windows 7, the program does not work.
When I run the program in VirtualBox, so also does not work.
When I run programv computer where I created it, so it works perfectly.

himitsu 4. Mär 2012 14:05

AW: Keyboard Hook
 
http://www.delphipraxis.net/93871-ve...exthookex.html ?

danten 4. Mär 2012 14:29

AW: Keyboard Hook
 
Thank you, but I do not want to use external DLLs.
It would not write my code to make it work without DLL?

Please show the code or modify my code.

Sir Rufo 4. Mär 2012 14:45

AW: Keyboard Hook
 
if u want to have a global keyhook u have to use a dll. there is no alternative!

danten 4. Mär 2012 15:22

AW: Keyboard Hook
 
Sorry, I somehow do not understand.
Creating a program for creating tutorials and sample code from one part.
It shows users what button was pressed the keyboard.

I do not do any program on stealing data from the computer as some keylogery.

Very please someone who has the time to download the attachment KeyHook.7z above and added a piece of code to me so angry.
For you, it's a matter of a few lines and I did a lot of help in further work.

Thank you for your help.

Luckie 4. Mär 2012 17:58

AW: Keyboard Hook
 
Zitat:

Zitat von Sir Rufo (Beitrag 1154444)
if u want to have a global keyhook u have to use a dll. there is no alternative!

That is not quite correct. It depends on the hook type. Some global hooks don't need a DLL.

Sir Rufo 4. Mär 2012 18:18

AW: Keyboard Hook
 
Zitat:

Zitat von Luckie (Beitrag 1154483)
Zitat:

Zitat von Sir Rufo (Beitrag 1154444)
if u want to have a global keyhook u have to use a dll. there is no alternative!

That is not quite correct. It depends on the hook type. Some global hooks don't need a DLL.

We are talking about a global hook for the Keyboard ... not about some hooks ;)


Alle Zeitangaben in WEZ +1. Es ist jetzt 08:02 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz