Autor Beitrag
reichemi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 41

WinXP home + prof, SUSE 9.2
Delphi 6
BeitragVerfasst: Do 23.03.06 09:11 
hallo,
und sorry für dieses thema (wurde ja schon häufiger beleuchtet), aber ich kriegs einfach nicht zum laufen :(

ziel ist es, eine beliebige tastenkombination (TShortCut) an ein anderes fenster zu senden. soweit auch ok, habe dazu folgendes gefunden:
  • PostKeyExHWND
  • AttachThreadInput
  • SetFocus bzw. SetForegroundWindow


hier mein quelltext:

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:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
procedure TForm1.Button3Click(Sender: TObject);
var
  Wnd: HWND;
  i: Integer;
  sc: TShortCut;
  key: Word;
  shift: TShiftState;
  shift_str: String;
  MyThreadID, WndThreadID: Cardinal;
begin
  if ComboBox1.ItemIndex = -1 then
    Exit;

  Wnd := HWND(ComboBox1.Items.Objects[ComboBox1.ItemIndex]);
  log('window handle = '+IntToStr(Wnd));

  if Wnd = 0 then
    Exit;

  MyThreadID := GetCurrentThreadId;
  log('my thread ID = '+IntToStr(MyThreadID));

  WndThreadID := GetWindowThreadProcessId(Wnd, nil);
  log('window thread ID = '+IntToStr(WndThreadID));

  if not AttachThreadInput(MyThreadID, WndThreadID, TRUE) then begin
    ShowMessage('could not attach thread id '+IntToStr(WndThreadID)+' (window "'+ComboBox1.Text+'") to the current thread id '+IntToStr(MyThreadID));
    Exit;
  end;
  log('thread input attached');

  try
    //windows.SetFocus(Wnd);
    SetForegroundWindow(Wnd);

    for i:=0 to ListBox1.Items.Count-1 do begin
      sc := TShortCut(ListBox1.Items.Objects[i]);
      ShortCutToKey(sc, key, shift);

      shift_str := '';
      if ssShift in shift then
        shift_str := shift_str + 'ssShift, ';
      if ssAlt in shift then
        shift_str := shift_str + 'ssAlt, ';
      if ssCtrl in shift then
        shift_str := shift_str + 'ssCtrl, ';
      if Length(shift_str) > 0 then
        shift_str := LeftStr(shift_str, Length(shift_str)-2);

      log('sending shortcut "'+ShortCutToText(sc)+'" (key='+IntToStr(key)+' shiftstate=['+shift_str+'])');

      PostKeyExHWND(Wnd, key, shift, False);
    end;

  finally
    if not AttachThreadInput(MyThreadID, WndThreadID, FALSE) then
      ShowMessage('could not detach thread id '+IntToStr(WndThreadID)+' (window "'+ComboBox1.Text+'")')
    else
      log('thread input detached');
  end;
end;


die ComboBox1 wird zuvor mittels EnumWindows gefüllt, klappt auch alles super.
in die ListBox1 trage ich die zu sendenden Shortcuts ein:

ausblenden Delphi-Quelltext
1:
ListBox1.Items.AddObject(ShortCutToText(HotKey1.HotKey), TObject(HotKey1.HotKey));					


ich teste es u.a. mit dem notepad (benutze WinXP home SP2 und delphi 6 pro).

der effekt: es kommt zwar in den vordergrund, allerdings scheint der shortcut nicht anzukommen...

das log sieht z.b. folgendermaßen aus (eingetragen waren: 'A', 'B', 'Umsch+C'):

Zitat:

window handle = 3677646
my thread ID = 4384
window thread ID = 5408
thread input attached
sending shortcut "A" (key=65 shiftstate=[])
sending shortcut "B" (key=66 shiftstate=[])
sending shortcut "Umsch+C" (key=67 shiftstate=[ssShift])
thread input detached



hat jemand eine idee? überall wo ich gesucht hab stand es so (oder ähnlich) da, und dort ging es offenbar... :eyes:

edit: die function PostKeyExHWND hab ich von hier: www.swissdelphicente.../showcode.php?id=220