Autor Beitrag
Flamefire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1207
Erhaltene Danke: 31

Win 10
Delphi 2009 Pro, C++ (Visual Studio)
BeitragVerfasst: Do 20.03.08 21:36 
Ich will einen Hotkey (STRG+N) an ein Programm, was nicht den focus hat, schicken
hab das hier:

ausblenden 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:
procedure sendKey();
var hWindow: HWND;
    Pid1, Pid2: Cardinal;
    lParam: LongInt;
    ksNew, ksOld: TKeyboardState;
begin
  hWindow:=FindWindow('xxxx',nil);
  Pid1 := GetWindowThreadProcessId(Application.Handle);
  Pid2 := GetWindowThreadProcessId(hWindow);
  
  if AttachThreadInput(Pid1, Pid2, True) then
  begin
    lParam := MakeLong(0, MapVirtualKey(Ord('N'), 0));
    ksNew[VK_CONTROL] := $80;
    ksNew[VK_SHIFT] := $0;
    ksNew[Ord('N')] := Ord('N');
    GetKeyboardState(ksOld);
    SetKeyboardState(ksNew);
    Sleep(10);
    PostMessage(hWindow, WM_KEYDOWN, Ord('N'), lParam);
    Sleep(10);
    PostMessage(hWindow, WM_KEYUP, Ord('N'), lParam or $C0000000);
    Application.ProcessMessages;
    Sleep(100);
    SetKeyboardState(ksOld);
  end;
  AttachThreadInput(Pid1, Pid2, False);
end;

seltsamerweise funktioniert es manchmal nicht
das heißt, der hotkey kommt nicht an
ich verschicke auch noch andere messages mit meine programm, vl liegts daran

wenn ich aber das fenster meines programms ein stück bewege und dann die funktion nochmal ausführe funktioniert es
woran liegt das? gibt es noch eine bessere möglichkeit, als meine?
Ich hätte auch noch nen button auf ner Toolhelp32 in dem programm welcher die gleiche funktion wie der hotkey hat

Edit: es scheint an den anderen messages zu liegen, welche ich an ein control aus dem fenster schicke...
habs ausprobiert...