I am using RegisterHotKey to define a system-wide hot key that, when pressed, will automatically copy any highlighted text to the clipboard in Microsoft Windows.
Text is copied to the clipboard by simply pressing Ctrl + C as this seems to be one of the only things that I could find that works in almost any Windows application. The keys themselves are being pressed by invoking keybd_event.
The global hot key, and the copy to clipboard, actually works great in every case except when I’ve assigned the hot key to be something that contains Ctrl. This Ctrl seems to be interfering with the copy to clipboard or something along those lines.
Example: If I set the hotkey to be F11, everything will initiate properly and copy the text. Whereas, Ctrl + Alt + D will simply refuse to copy the highlighted text.
I’ve tried toying around with BlockInput to see if that would prevent any additional things from having an effect on my keystrokes.
I’ve also tried to send KEYEVENTF_KEYUP to Ctrl, Alt, Shift with the hopes that it would set all my keys to a KeyUp state. An example of this is below:
// This will send ctrl + C to the active window to copy something to the clipboard.
public static void SendCtrlC(IntPtr hWnd)
{
const byte Control = (byte)VKCode.Control;
const byte LeftControl = (byte)VKCode.Lcontrol;
const byte LeftAlt = (byte)VKCode.Lmenu;
const byte LeftShift = (byte)VKCode.Lshift;
const byte RightControl = (byte)VKCode.Rcontrol;
const byte RightAlt = (byte)VKCode.Rmenu;
const byte RightShift = (byte)VKCode.Rshift;
const byte KeyC = (byte)VKCode.KeyC;
const int KeyUp = (int)KeyEvent.KeyEventFKeyUp;
const int KeyDown = (int)KeyEvent.KeyEventFKeyDown;
KeyBdEvent(LeftControl, 0, KeyUp, 0); // Left Control Up
KeyBdEvent(LeftAlt, 0, KeyUp, 0); // Left Alt Up
KeyBdEvent(LeftShift, 0, KeyUp, 0); // Left Shift Up
KeyBdEvent(RightControl, 0, KeyUp, 0); // Left Control Up
KeyBdEvent(RightAlt, 0, KeyUp, 0); // Left Alt Up
KeyBdEvent(RightShift, 0, KeyUp, 0); // Left Shift Up
Window.SetForegroundWindow(hWnd);
KeyBdEvent(Control, 0, KeyDown, 0); // Left Control Down
KeyBdEvent(KeyC, 0, KeyDown, 0); // Key C Down
KeyBdEvent(KeyC, 0, KeyUp, 0); // Key C Up
KeyBdEvent(Control, 0, KeyUp, 0); // Left Control Up
}
Check:
http://pinvoke.net/default.aspx/user32.sendinput,
SendInput @ msdn
You can also add double click with SendInput to automatize getting highlighted text. Keep in mind you shouldn’t manipulate clipboard to send information to application.