【问题标题】:how to send Ctrl + G to another application?如何将 Ctrl + G 发送到另一个应用程序?
【发布时间】:2020-09-20 07:57:03
【问题描述】:

我正在尝试使用带有 ForegroundWindow 的 InputSimulator 发送 CTRL + G 键,但它不起作用。使用其他键,它可以按预期工作,例如 Ctrl + A 或 Ctrl + C

hotkeys menu options

我试过了:

Process[] p = Process.GetProcessesByName("notepad");
if (p != null && p.Length > 0)
{
    IntPtr h = p[0].MainWindowHandle;
    SetForegroundWindow(h);
    InputSimulator sim = new InputSimulator();
    sim.Keyboard.ModifiedKeyStroke (VirtualKeyCode.CONTROL, VirtualKeyCode.VK_G) .Sleep (300);
 }

 Process[] p = Process.GetProcessesByName("notepad");
 if (p != null && p.Length > 0)
 {
    IntPtr h = p[0].MainWindowHandle;
    SetForegroundWindow(h);
    InputSimulator sim = new InputSimulator();
    sim.Keyboard.KeyDown(VirtualKeyCode.CONTROL);
    sim.Keyboard.KeyPress(VirtualKeyCode.VK_G).Sleep(300);
    sim.Keyboard.KeyUp(VirtualKeyCode.CONTROL).Sleep(300);
}

也试试:

SendKeys.Send ("^ (g)");

我什至尝试过 keybd_event(user32.dll 函数)

【问题讨论】:

  • 你在用这个InputSimulator吗?执行代码时,您的其他应用程序是否是活动窗口?
  • 感谢您的回答实际上活动窗口是目标应用程序,实际上如果我使用 Ctrl-A 为例,它会按预期工作...
  • 你怎么知道它没有被发送?也许目标应用忽略了它。
  • 嗨,你是对的......但是当我发送 Ctrl-A 时,打开的窗口会正确打开。但是,Ctrl-G 不会打开保存窗口。我尝试过其他应用程序,与记事本不同,并且发生了同样的事情:( ..Thanks

标签: c# keyboard-shortcuts sendkeys ctrl inputsimulator


【解决方案1】:

在发送击键之前,您需要强制另一个窗口成为焦点。 Microsoft 文档中有详细说明此过程的示例 - link

简而言之,他们使用FindWindow 后跟SetForegroundWindow,然后通过SendKeys.SendWait() 发送密钥,尽管您可以选择使用SendKeys.Send()

This answer 似乎是FindWindow + SetForegroundWindow 过程的简洁实现。

【讨论】:

  • 嗨,谢谢你的回答......我最初的方法不完整......我的问题只是 Ctrl + G......非常感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多