我正在通过将字符串发送到备忘录来试验 SendInput。我将 SendInput 命令与对 Memo.Lines.Add('....') 的调用混合在一起。令我惊讶的是,所有的 Memo.Lines.Add 命令都在任何 SendInput 例程之前执行。为什么?如何让备忘录以正确的顺序显示信息?
我的代码如下所示:
procedure TForm1.Button1Click(Sender: TObject);
const
AStr = '123 !@# 890 *() abc ABC';
var
i: integer;
KeyInputs: array of TInput;
procedure KeybdInput(VKey: Byte; Flags: DWORD);
begin
SetLength(KeyInputs, Length(KeyInputs)+1);
KeyInputs[high(KeyInputs)].Itype := INPUT_KEYBOARD;
with KeyInputs[high(KeyInputs)].ki do
begin
wVk := VKey;
wScan := MapVirtualKey(wVk, 0);
dwFlags := Flags;
end;
end;
begin
Memo1.SetFocus;
Memo1.Lines.Add('AStr := ' + AStr);
Memo1.Lines.Add('');
Memo1.Lines.Add('Use: KeybdInput(ord(AStr[i]),0)');
SetLength(KeyInputs,0);
for i := 1 to Length(AStr) do KeybdInput(ord(AStr[i]),0);
SendInput(Length(KeyInputs), KeyInputs[0], SizeOf(KeyInputs[0]));
Memo1.Lines.Add('');
Memo1.Lines.Add('Use: KeybdInput(vkKeyScan(AStr[i]),0)');
SetLength(KeyInputs,0);
for i := 1 to Length(AStr) do KeybdInput(vkKeyScan(AStr[i]),0);
SendInput(Length(KeyInputs), KeyInputs[0], SizeOf(KeyInputs[0]));
end;
我希望结果看起来像这样:
但它实际上看起来像这样: