Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi windows keyhooks (https://www.delphipraxis.net/4766-windows-keyhooks.html)

Jan 11. Mai 2003 11:29


windows keyhooks
 
Hallo DP,
Ich habe ein Programm geschrieben, welches so aussieht:

Delphi-Quellcode:
unit Unit1;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, activex, SHDocVw, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  maus:tmouse;
  KBHook: HHook; {this intercepts keyboard input}

function KeyboardHookProc
   (Code: Integer;
   WordParam: Word;
   LongParam: LongInt): LongInt; stdcall;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
setCursorPos(620, 500);
end;

function KeyboardHookProc
   (Code: Integer;
   WordParam: Word;
   LongParam: LongInt) : LongInt;
begin
 case WordParam of
  VK_NUMPAD1   :
   begin
  setCursorPos(600, 390);
  mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
      mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD2:
   begin
    setCursorPos(570, 420);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);
  end;
   VK_NUMPAD3:
   begin
     setCursorPos(570, 470);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD4:
   begin
     setCursorPos(600, 510);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD5:
   begin
     setCursorPos(630, 540);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD6:
   begin
     setCursorPos(680, 540);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD7:
   begin
     setCursorPos(720, 510);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD8:
   begin
     setCursorPos(750, 470);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD9:
      begin
     setCursorPos(750, 430);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
         mouse_event(MOUSEEVENTF_LEFTup, 0, 0, 0, 0);

   end;
   VK_NUMPAD0:
   begin
     setCursorPos(730, 380);
     mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);

   end;
 end; {case}

Result:=0;
 {To prevent Windows from passing the keystrokes
 to the target window, the Result value must
 be a nonzero value.}
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
webbrowser1.Navigate('http://www.dummy.de');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 UnHookWindowsHookEx(KBHook);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 {Set the keyboard hook so we
 can intercept keyboard input}
 KBHook:=SetWindowsHookEx(WH_KEYBOARD,
           {callback —>}  @KeyboardHookProc,
                          HInstance,
                          GetCurrentThreadId());

end;
end.
Das klappt auch schön, ausser einem Makel: es wird immer Doppelgeklickt. Wie kann ich das abstellen?
Gruß
Jan

jbg 11. Mai 2003 11:50

Re: windows keyhooks
 
Zitat:

Zitat von Jan
es wird immer Doppelgeklickt. Wie kann ich das abstellen?

Du fängst ja auch die WM_KEYDOWN und WM_KEYUP Botschaften ab. Und da jede Taste irgendwann mal losgelassen wird, bekommst du eben 2x einen KeyboardHookProc-Aufruf.
Ich habe im Moment kein PSDK zur Hand (Linux gebootet), aber schau die mal den lParam Parameter an.


Alle Zeitangaben in WEZ +1. Es ist jetzt 19:47 Uhr.

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