Autor Beitrag
whitef
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 202
Erhaltene Danke: 1

Windows X
Delphi XE X
BeitragVerfasst: So 10.01.16 21:58 
Hi,
ich wollte mal fragen ob es auch eine Funktion ähnlich wie bei WORD gibt, mit der man ganz simpel einen string ausgibt, egal welches fenster den fokus hat. also einen tastendruck simuliert?

ich wollte bei drücken des Hotkeys einen bestimmten Text in das "unbekannte" Fenster eingeben lassen.

bis jetzt habe ich das:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
procedure TFormMain.WMHotKey(var Msg: TWMHotKey);
var
  HandleActiveWindow: THandle;
  s : String;
  i : Integer;
begin


HandleActiveWindow := GetForegroundWindow;
if HandleActiveWindow <> 0 then
begin
  s := 'test';
   for i := 1 to Length(s) do
  begin
    SendMessage(HandleActiveWindow, WM_CHAR, Word(s[i]), 0);
  end;
  showmessage('ausgeführt');
end;


Dies geht jedoch nicht. die msgbox wird zwar angezeigt wenn ich zb. notepad offen habe und den hotkey drücke, aber mehr nicht.
whitef Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 202
Erhaltene Danke: 1

Windows X
Delphi XE X
BeitragVerfasst: So 10.01.16 22:10 
hiermit klappt es schonmal für einen Tastendruck:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
procedure TFormMain.WMHotKey(var Msg: TWMHotKey);
var
  TypeText : String;
  i : Integer;
begin
  if Msg.HotKey = Hotkey1_id then
  begin
    TypeText := 'test1';
  end;

  if Msg.HotKey = Hotkey2_id then
  begin
    TypeText := 'test2';
  end;

  for i := 1 to Length(TypeText) do
  begin
    SendKey(VkKeyScan(TypeText[i]));
  end;
end;



Um einen String abzuschicken:

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:
procedure EnterText(AText: String);
var lCount : Integer;
    lScanCode : Smallint;
    lWithAlt,
    lWithCtrl,
    lWithShift : Boolean;
begin
  for lCount := 1 To Length(AText) Do
  begin
    lScanCode := VkKeyScan(AText[lCount]);
    //Ermitteln ob Shift gedrückt wurde
    lWithShift := lScanCode and (1 shl 8) <> 0;
    //Ermitteln ob Strg gedrückt wurde
    lWithCtrl := lScanCode and (1 shl 9) <> 0;
    //Ermitteln ob Alt gedrückt wurde
    lWithAlt := lScanCode and (1 shl 10) <> 0;

    if lWithShift then
      keybd_event(VK_SHIFT, 000);
    if lWithCtrl then
      keybd_event(VK_CONTROL, 000);
    if lWithAlt then
      keybd_event(VK_MENU, 000);

    keybd_event(lScanCode, 000);
    keybd_event(lScanCode, 0, KEYEVENTF_KEYUP, 0);

    if lWithAlt then
      keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
    if lWithCtrl then
      keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
    if lWithShift then
      keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
  end;
end;

procedure TFormMain.WMHotKey(var Msg: TWMHotKey);
var
  TypeText : String;
  i : Integer;
begin
  if Msg.HotKey = Hotkey1_id then
  begin
    TypeText := 'test1';
  end;

  if Msg.HotKey = Hotkey2_id then
  begin
    TypeText := 'test2';
  end;

  EnterText(TypeText);
end;