【问题标题】:Simulating CTRL+C with Sendkeys fails用 Sendkeys 模拟 CTRL+C 失败
【发布时间】:2013-02-24 20:00:54
【问题描述】:

我对 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
}

【问题讨论】:

标签: c# registerhotkey


【解决方案1】:

我认为这是因为当你发送 CTRL+C 时你已经按下了 ALTALT 修饰符kbd>+J.

如果你放

Thread.Sleep(1000);

就在发送密钥之前,您将有时间释放 ALT+J,然后释放 CTRL+C 会起作用的。

另外,如果您打算检查热键何时释放,请检查this

【讨论】:

    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多