我有一个代码,可以在按下键盘时发送一个字符串。有时似乎不起作用。应用程序仅获得键盘按下的一半。
有人知道如何在两次发送键盘按键之间增加延迟吗?
代码:
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;