Autor Beitrag
john_wayne
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Do 26.08.10 15:08 
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:

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 28.08.10 20: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).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
john_wayne Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 31.08.10 09: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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Di 31.08.10 10:54 
ja
john_wayne Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Di 31.08.10 13:09 
Hallo, hab das versucht, aber das klappt noch nicht so..- :-(
ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 763
Erhaltene Danke: 27

Win XP, Windows 7, (Linux)
D6, D2010, C#, PHP, Java(Android), HTML/Js
BeitragVerfasst: Di 31.08.10 13:42 
ausblenden 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);