Simulating CTRL+C with Sendkeys fails

c#, registerhotkey

Solution

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.

Problem

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 } ```

Original source

Related problems