Entwickler-Ecke

Windows API - Globaler Hook


john_wayne - Do 26.08.10 14:08
Titel: Globaler Hook
Hallo Leute, ich will mit hilfe der Hooks folgendes erreichen:

Einmal will ich nur die Tastatur benutzen, dabei soll die Maus sich nicht bewegen und nicht gecklickt werden können.

Der andere zustand wäre bewegliche Maus und jeder Tastendruck wird als linker Mausklick interpretiert.

Die Umschaltung zwischen den Zuständen soll mit der linken Steuerungstaste geschen. Dazu habe ich eine .dll geschrieben in der je ein Hook für die Maus und für die Tastatur sind. Der Code für die dll ist:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
//Keyboard Hook
function GlobalKeyboardHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
begin
  if code < 0 then begin //if code<0 run next hook and return the value frm next hook
  GlobalKeyboardHook := CallNextHookEx(CurrentKeyboardHook, code, wParam, lParam);
  exit;
  end;

  if ((wParam=VK_CONTROL) and ((lParam and $8100FFFF)=$80000001))//If the left is Controlkey pressed and not held
  then Mouse_mode := not Mouse_mode;
  if Mouse_mode then begin
    if(((lParam and $8000FFFF) = $80000001and (wParam <> VK_CONTROL)) then
    begin
      sysutils.beep;
      mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
      mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
    end;
    GlobalKeyboardHook := 1;
  end
  else
  GlobalKeyboardHook := 0;

  CallNextHookEx(CurrentKeyboardHook, code, wParam, lParam);
  //GlobalKeyboardHook := 1; //if hook returns non-zero value, the window that should
                           //the keyboard message doesn't get it
end;

//------------------------------------------------------------------------------
//Mouse Hook
function GlobalMouseHook(code: integer; wParam: word; lParam: longword): longword; stdcall;
begin
  if code < 0 then begin //if code<0 run next hook and return the value frm next hook
  GlobalMouseHook := CallNextHookEx(CurrentMouseHook, code, wParam, lParam);
  exit;
  end;

  if Mouse_mode then  //if mouse mode do nothing
    begin
      GlobalMouseHook:=0
    end
  else
    begin
      Windows.SetCursorPos(100230);  //Set cursor to position
      GlobalMouseHook:=1;              //disable click
    end;

  CallNextHookEx(CurrentMouseHook, code, wParam, lParam);
  GlobalMouseHook:=0;
end;


Die Hooks werden dann im Hauptprogramm gesetzt.

Das funktioniert eigentlich auch, nur dass ich bei jedem Fenster das ich aufmache, ich auf den gewünschten zustand schalten muss, obwohl es globale Hooks sein sollten :( Ich will aber dass ein Zustand für alle Fenster gilt.

Was muss ich da anders machen?

Danke für die Hilfe
Eugen

Moderiert von user profile iconGausi: Delphi-Tags hinzugefügt


BenBE - Sa 28.08.10 19:24

Du musst via IPC (Interprozess-Kommunikation) alle Instanzen deines Hooks über Prozess-Grenzen hinweg synchron halten. Das geht am einfachsten mit Hilfe von MMF (Memory Mapped Files).


john_wayne - Di 31.08.10 08:44

Hallo BenBE,
danke für die Hilfe!
Also erstelle ich in dem Hauptprogramm in dem die hooks gesetzt werden ein MMF mit der Variablen in der ich den Zustand speichere. Und in den hooks selber kann ich die Variable dann lesen od. verändern und endsprechend reagieren.
Habe ich das richtig verstanden?

Gruß
Eugen


thepaine91 - Di 31.08.10 09:54

ja


john_wayne - Di 31.08.10 12:09

Hallo, hab das versucht, aber das klappt noch nicht so..- :-(

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
type TData = record
     Mouse_mode: boolean;
end;
PTData = ^TData;
MapHandle: THandle; 
Memory: PTData;

procedure TForm2.FormCreate(Sender: TObject); //On Create
begin
  MapHandle := CreateFileMapping($FFFFFFFFnil, PAGE_READWRITE, 0, Sizeof(TData), 'MyMapFile');
  if MapHandle > 0 then
    Memory := MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 000);
  Memory.Mouse_mode:=true;
end;


Wie bekommt die dll jetzt mit wo das im Speicher liegt?

Moderiert von user profile iconNarses: Delphi-Tags hinzugefügt


thepaine91 - Di 31.08.10 12:42


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
HANDLE OpenFileMapping(

    DWORD dwDesiredAccess,  // access mode 
    BOOL bInheritHandle,  // inherit flag 
    LPCTSTR lpName   // pointer to name of file-mapping object 
   );

const
  memFileconst: PAnsiChar = 'MyMapFile';

....

procedure foo();
  memfile := OpenFileMapping(FILE_MAP_WRITE, False, memFileconst);