嗨,我正在尝试在可视化的基本delphi中完成经典的SendKeys ('hello world');,但是我发现您不能这样做。

有谁知道该怎么办?

最佳答案

查看Win32 API keybd_event()SendInput()函数。这两个函数都在Delphi的Windows单元中声明。

例如:

uses
  Windows;

procedure SendKeys(const S: String);
var
  I: Integer;
begin
  for I := 1 to Length(S) do
  begin
    // keybd_event() does not support Unicode, so you should use SendInput() instead...
    keybd_event(S[I], MapVirtualKey(S[I], 0),0, 0);
    keybd_event(S[I], MapVirtualKey(S[I], 0), KEYEVENTF_KEYUP, 0);
  end;
end;




uses
  Windows;

{$POINTERMATH ON}
procedure SendKeys(const S: String);
var
  InputEvents: PInput;
  I, J: Integer;
begin
  if S = '' then Exit;
  GetMem(InputEvents, SizeOf(TInput) * (Length(S) * 2));
  try
    J := 0;
    for I := 1 to Length(S) do
    begin
      InputEvents[J].Itype := INPUT_KEYBOARD;
      InputEvents[J].ki.wVk := 0;
      InputEvents[J].ki.wScan := Ord(S[I]);
      InputEvents[J].ki.dwFlags := KEYEVENTF_UNICODE;
      InputEvents[J].ki.time := 0;
      InputEvents[J].ki.dwExtraInfo := 0;
      Inc(J);
      InputEvents[J].Itype := INPUT_KEYBOARD;
      InputEvents[J].ki.wVk := 0;
      InputEvents[J].ki.wScan := Ord(S[I]);
      InputEvents[J].ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
      InputEvents[J].ki.time := 0;
      InputEvents[J].ki.dwExtraInfo := 0;
      Inc(J);
    end;
    SendInput(J, InputEvents[0], SizeOf(TInput));
  finally
    FreeMem(InputEvents);
  end;
end;

10-05 22:41