gpt4 book ai didi

delphi - SendInput 显示不按顺序发送的数据

转载 作者:行者123 更新时间:2023-12-02 00:13:54 26 4
gpt4 key购买 nike

我正在通过将字符串发送到备忘录来尝试 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

关于delphi - SendInput 显示不按顺序发送的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39607879/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com