5

I'm working on a program to trigger cut and pastes

Pastes I have no problem with (I just dump a string into the clipboard)

Cut and or Copys are proving to be a little more difficult

The program I have is out of focus and has several hot keys registered with the os CTRL+ALT+2 CTRL+ALT+3 etc)

That I want to use to trigger Windows to copy anything that is highlighted in the window that is focused

I tried doing a sendkeys

SendKeys.Send("^c");

but that seems to work once or twice if at all then stop working.

is there a better way to try to trigger windows into coping highlighted content on a different window

Lou Franco
  • 87,846
  • 14
  • 132
  • 192
Crash893
  • 11,428
  • 21
  • 88
  • 123

2 Answers2

14

One way to do this is by using the Win32 SendInput function. With SendInput, you have to simulate both the key down and key up events in order for the full key press to register. To simulate CTRL+C, you'd have to do:

  • CTRL key down
  • C key down
  • C key up
  • CTRL key up

pinvoke.net has some examples of SendInput usage. One issue to be mindful of is if the key is already pressed. You can use GetAsyncKeyState to only simulate a key down event if the key is not already down.

Below is some example code of how you could simulate CTRL+C. With the code below, you can simply call Keyboard.SimulateKeyStroke('c', ctrl: true); Note that this works as if the user literally pressed CTRL+C, so the active application will behave as it always does when such an event happens (i.e. if nothing is normally copied, then nothing will be copied with this method, either).

Edit: See David's comment below about batching the sent input. The code below should be sending the entire sequence of input events through a single call to SendInput to avoid being interleaved (and misinterpreted) with real user input events.

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;

namespace SimulateKeys
{
    static class Keyboard
    {
        public static void SimulateKeyStroke(char key, bool ctrl = false, bool alt = false, bool shift = false)
        {
            List<ushort> keys = new List<ushort>();

            if (ctrl)
                keys.Add(VK_CONTROL);

            if (alt)
                keys.Add(VK_MENU);

            if (shift)
                keys.Add(VK_SHIFT);

            keys.Add(char.ToUpper(key));

            INPUT input = new INPUT();
            input.type = INPUT_KEYBOARD;
            int inputSize = Marshal.SizeOf(input);

            for (int i = 0; i < keys.Count; ++i)
            {
                input.mkhi.ki.wVk = keys[i];

                bool isKeyDown = (GetAsyncKeyState(keys[i]) & 0x10000) != 0;

                if (!isKeyDown)
                    SendInput(1, ref input, inputSize);
            }

            input.mkhi.ki.dwFlags = KEYEVENTF_KEYUP;
            for (int i = keys.Count - 1; i >= 0; --i)
            {
                input.mkhi.ki.wVk = keys[i];
                SendInput(1, ref input, inputSize);
            }
        }

        [DllImport("user32.dll")]
        static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);

        [DllImport("user32.dll")]
        static extern short GetAsyncKeyState(ushort vKey);

        struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public uint mouseData;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        struct KEYBDINPUT
        {
            public ushort wVk;
            public ushort wScan;
            public uint dwFlags;
            public uint time;
            public IntPtr dwExtraInfo;
        }

        struct HARDWAREINPUT
        {
            public int uMsg;
            public short wParamL;
            public short wParamH;
        }

        [StructLayout(LayoutKind.Explicit)]
        struct MOUSEKEYBDHARDWAREINPUT
        {
            [FieldOffset(0)]
            public MOUSEINPUT mi;

            [FieldOffset(0)]
            public KEYBDINPUT ki;

            [FieldOffset(0)]
            public HARDWAREINPUT hi;
        }

        struct INPUT
        {
            public int type;
            public MOUSEKEYBDHARDWAREINPUT mkhi;
        }

        const int INPUT_KEYBOARD = 1;
        const uint KEYEVENTF_KEYUP = 0x0002;

        const ushort VK_SHIFT = 0x10;
        const ushort VK_CONTROL = 0x11;
        const ushort VK_MENU = 0x12;
    }

    class Program
    {
        static void Main(string[] args)
        {
            Thread.Sleep(3000);
            Keyboard.SimulateKeyStroke('c', ctrl: true);
        }
    }
}
Chris Schmich
  • 29,128
  • 5
  • 77
  • 94
  • 1
    ding ding ding looks like we might have a winnar! I'll test it tonight and mark it, if it works – Crash893 May 27 '10 at 16:23
  • I'm getting an error on line 10 saying that i cant "set a default parameter " where bool shift = false. Is that a .net3.5 thing ( i'm using express 2008) – Crash893 May 27 '10 at 17:48
  • That's a .NET 4.0 thing, actually (default parameters). If you want to do optional parameters in .NET 3.5 you'll need to make multiple overloads. – CMerat May 27 '10 at 19:40
  • @Crash893: yeah, sorry, .NET 4, you can change the signature to `public static void SimulateKeyStroke(char key, bool ctrl, bool alt, bool shift)` and just specify each parameter explicitly. – Chris Schmich May 27 '10 at 20:31
  • looks like were good. I'm thinking ill just move the rest of the program up to 4.0 rather than try to convert this back to 3.5 thanks this is really cool – Crash893 May 28 '10 at 17:58
  • 2
    What is weak here is that you completely miss the point of `SendInput`. It is meant to be handed an array of structs. If you want the key events to arrive in the right order, and not interleaved with real events, you need to make a single call to `SendInput` passing all the structs. – David Heffernan Jul 12 '13 at 08:24
  • 1
    @DavidHeffernan: You are right, that is a very real possibility. I haven't updated the code, but I at least added an edit explaining the situation. Thanks. – Chris Schmich Jul 12 '13 at 08:52
1

If you can get the selected text from the focused window (maybe an easier problem to solve) then you're better off using the SetText method of the System.Windows.Forms.Clipboard class.

philiphobgen
  • 2,234
  • 17
  • 28
  • I'm not sure one of use is following (it could be me) but how would I get the text to set? that is the point that i'm having trouble. could you post some code as your answer may have just flown over my head – Crash893 May 24 '10 at 03:49
  • From your edit it sounds a bit more dificult ;-) I haven't done this in .Net, but I seem to remember from my Delphi days, that if you know the handle of the control that has the selected text, you can post a message to that window and get the selected text back. I've also heard mention of using MS UI Automation to do this, but I've never tried it. You could have a read here: http://msdn.microsoft.com/en-us/library/ms747327(v=VS.100).aspx – philiphobgen May 24 '10 at 16:47