使用 Sendkeys 模拟 CTRL+C 失败

3

我在使用Sendkeys时遇到了一个奇怪的问题。我的应用程序基本上应该做的是,当我按下ALT+ J时,它将模拟在任何窗口中进行CTRL+C操作以复制一些高亮文本,但是CTRL+C模拟不起作用。

[DllImport("user32.dll", SetLastError = true)]  
[return: MarshalAs(UnmanagedType.Bool)]  
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

private void Form1_Load(object sender, EventArgs e)
{
 RegisterHotKey(this.Handle,
 this.GetType().GetHashCode(), 1, (int)'J'); // Here it's waiting for the ALT+J 
}

protected override void WndProc(ref Message m) // Function to c
{
 if (m.Msg == 0x0312) // If ALT+J pressed
 {
     Copier(); // .. Simulate CTRL+C (but doesn't work)
 }
 base.WndProc(ref m);
}

public void Copier() // Function to simulate the CTRL+C
{
 Debug.WriteLine("Ok ");
 InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_C); // First way
 SendKeys.Send("^(c)"); // Second way
}

1
+1 我可以重现它。SendKeys.Send("^{INS}"); 也失败了。 - Jeremy Thompson
1个回答

6
我认为这是因为当您按下CTRL+C时,您已经按下了ALT+J的修饰键。

如果您输入

Thread.Sleep(1000);

在发送按键之前,您将有时间松开ALT+J,然后CTRL+C才能正常工作。

此外,如果您计划检查热键何时被释放,请查看此处


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接