3

I have an odd problem with Sendkeys, what my application basically should do is that, when I press ALT+ J, it'll simulate a CTRL+C operation (on any windows) to copy some highlighted text, however the CTRL+C simulation is not working.

[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
}
Abhishek
  • 6,912
  • 14
  • 59
  • 85
Ydhem
  • 928
  • 2
  • 14
  • 36
  • 1
    +1 I can reproduce it. `SendKeys.Send("^{INS}");` fails too – Jeremy Thompson Mar 09 '13 at 07:11
  • possible duplicate of [Is it possible to send a WM\_COPY message that copies text somewhere other than the Clipboard?](http://stackoverflow.com/questions/13569532/is-it-possible-to-send-a-wm-copy-message-that-copies-text-somewhere-other-than-t) – Jeremy Thompson Mar 09 '13 at 07:21

1 Answers1

6

I think this is happening because when you send CTRL+C you already have pressed the ALT modifier of ALT+J.

If you put

Thread.Sleep(1000);

just before send keys, you'll have time to release ALT+J and then CTRL+C will work.

Also, if you plan to check when your hotkey is released, check this.

Community
  • 1
  • 1
CGodo
  • 1,478
  • 14
  • 15