我尝试在ForegroundWindow中使用InputSimulator发送CTRL +G键,但它不起作用。对于其他键,它按预期工作,例如Ctrl +A或Ctrl +C
我已经尝试过了:
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函数)
发布于 2020-06-02 06:50:39
在发送击键之前,您需要强制另一个窗口成为焦点。微软文档中有详细说明此过程的示例-- link
简而言之,它们先使用FindWindow,然后使用SetForegroundWindow,然后通过SendKeys.SendWait()发送密钥,不过您也可以选择使用SendKeys.Send()
This answer看起来像是FindWindow + SetForegroundWindow流程的简明实现。
https://stackoverflow.com/questions/62142003
复制相似问题