以编程方式模拟用户输入的简单方法是什么?

发布于 2024-12-08 22:49:34 字数 309 浏览 0 评论 0原文

我有一个由于错误情况而弹出的对话框。我希望对话框保持打开状态至少 30 秒,并在收到最后一次用户输入(鼠标或键盘)后关闭 30 秒。

我可以通过检查 GetLastInputInfo 返回的值并在超过 30 秒前关闭对话框来实现此目的,但如果在用户 30 秒内没有使用鼠标或键盘时弹出对话框,则 GetLastInputInfo 测试通过立即,对话框立即再次关闭。我可以使用另一个计时器来完成此操作,但我认为在对话框打开之前模拟鼠标移动一点或发出(无害的)按键会更简单。它还可能具有将系统踢出屏幕保护程序的优点。

实现此目的最简单的 1 行 Delphi 代码片段是什么?

I have a dialog that pops up as result of an error condition. I want the dialog to remain open for at least 30 seconds, and close 30 seconds after the last user input (mouse or keyboard) is received.

I can implement this by checking the value returned by GetLastInputInfo and closing the dialog when this is more than 30 seconds ago, but if the dialog pops up when the user hasn't been at the mouse or keyboard for 30 seconds, the GetLastInputInfo test passes immediately, and the dialog closes again immediately. I could do this with another timer, but I figure it would be much simpler to simulate the mouse being moved a bit, or issuing a (harmless) keypress, just before the dialog opens. It would also have the advantage presumably of kicking the system out of screensaver.

What's the simplest 1-line Delphi code fragment to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

未央 2024-12-15 22:49:34

最简单的是使用 < code>keybd_event 函数(一行代码)

keybd_event(Ord('A'), 0, 0, 0);

也可以使用 SendInput< /a> 功能,但需要多行:)

Var
  pInputs : TInput;
begin
    pInputs.Itype := INPUT_KEYBOARD;
    pInputs.ki.wVk := Ord('A');
    pInputs.ki.dwFlags := KEYEVENTF_KEYUP;
    SendInput(1, pInputs, SizeOf(pInputs));
end;

the most simple is using the keybd_event function (one line of code)

keybd_event(Ord('A'), 0, 0, 0);

Also you can use the SendInput function but requires more than one line :)

Var
  pInputs : TInput;
begin
    pInputs.Itype := INPUT_KEYBOARD;
    pInputs.ki.wVk := Ord('A');
    pInputs.ki.dwFlags := KEYEVENTF_KEYUP;
    SendInput(1, pInputs, SizeOf(pInputs));
end;
淡莣 2024-12-15 22:49:34

使用 keybd_event 输入多个字节字符:

procedure InsertText(text:string);
var i:integer;
    j:integer;
    ch:byte;
    str:string;
begin
  i:=1;
  while i<=Length(text) do
  begin
    ch:=byte(text[i]);
    if Windows.IsDBCSLeadByte(ch) then
       begin
         Inc(i);
         str:=inttostr(MakeWord(byte(text[i]), ch));
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
         j:=1;
         while j<=Length(str) do
         begin
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 0, 0);
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 2, 0);
               j:=j+1;
         end;
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
       end
    else begin
           keybd_event(VkKeyScan(text[i]),0,0,0);
           keybd_event(VkKeyScan(text[i]),0,2,0);
         end;
    Inc(i);
  end;
end;

Input multiple byte characters with keybd_event:

procedure InsertText(text:string);
var i:integer;
    j:integer;
    ch:byte;
    str:string;
begin
  i:=1;
  while i<=Length(text) do
  begin
    ch:=byte(text[i]);
    if Windows.IsDBCSLeadByte(ch) then
       begin
         Inc(i);
         str:=inttostr(MakeWord(byte(text[i]), ch));
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
         j:=1;
         while j<=Length(str) do
         begin
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 0, 0);
               keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 2, 0);
               j:=j+1;
         end;
         keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
       end
    else begin
           keybd_event(VkKeyScan(text[i]),0,0,0);
           keybd_event(VkKeyScan(text[i]),0,2,0);
         end;
    Inc(i);
  end;
end;
吃素的狼 2024-12-15 22:49:34

https://github.com/WladiD/SendInputHelper 作者:Waldemar Derr 先生。

简单通用,选择2

例子:

uses
  ..., SendInputHelper;

procedure TForm1.Button1Click(Sender: TObject);
var
  SIH: TSendInputHelper;
begin
  SIH := TSendInputHelper.Create;
  try
    // Start command shell
    SIH.AddShortCut([ssWin], 'r'); // Win+R
    SIH.AddDelay(100);
    SIH.AddText('cmd', True); // Second parameter True means AppendReturn
    SIH.AddDelay(500);

    SIH.AddText('ping google.de', True); // Perform a ping.

    SIH.Flush; // Isn't it easy?
  finally
    SIH.Free;
  end;
end;

https://github.com/WladiD/SendInputHelper by Mr. Waldemar Derr.

Simple and versatile, choose 2.

Example:

uses
  ..., SendInputHelper;

procedure TForm1.Button1Click(Sender: TObject);
var
  SIH: TSendInputHelper;
begin
  SIH := TSendInputHelper.Create;
  try
    // Start command shell
    SIH.AddShortCut([ssWin], 'r'); // Win+R
    SIH.AddDelay(100);
    SIH.AddText('cmd', True); // Second parameter True means AppendReturn
    SIH.AddDelay(500);

    SIH.AddText('ping google.de', True); // Perform a ping.

    SIH.Flush; // Isn't it easy?
  finally
    SIH.Free;
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文