SendInput 显示乱序发送的数据
SendInput showing data sent out of sequence
我正在通过向备忘录发送字符串来试验 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;
我希望结果是这样的:
但实际上是这样的:
您使用 SendInput
发送的键盘输入通过 Windows 消息系统并最终进入您的应用程序消息队列。在您退出 Button1Click()
.
之前,消息队列未被处理
When something gets added to a queue, it takes time for it to come out the front of the queue
要按您期望的顺序查看事件,您需要在每个 SendInput()
之后插入对 Application.Processmessages()
的调用。尽管通常不建议调用 Application.ProcessMessages()
:
The Dark Side of Application.ProcessMessages in Delphi Applications
我正在通过向备忘录发送字符串来试验 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;
我希望结果是这样的:
但实际上是这样的:
您使用 SendInput
发送的键盘输入通过 Windows 消息系统并最终进入您的应用程序消息队列。在您退出 Button1Click()
.
When something gets added to a queue, it takes time for it to come out the front of the queue
要按您期望的顺序查看事件,您需要在每个 SendInput()
之后插入对 Application.Processmessages()
的调用。尽管通常不建议调用 Application.ProcessMessages()
:
The Dark Side of Application.ProcessMessages in Delphi Applications