Reputation: 285
My question is how to send shortcut from c++ to another application. Let's say for example that the notepad is open, and I want send to them shortcut like CTRL+P, or more complexive shortcut like CTRL+SHIFT+HOME, or ALT+F4, I find many good tutorials that explained how to send one key like 'A' or 'ALT', but I did not find how to send it together.
Upvotes: 1
Views: 3110
Reputation: 179961
This kind of "key combinations" is usually called Keyborad Accelerators in Microsoft Windows. The MSDN intro page
Upvotes: 0
Reputation: 2753
You can use SendInput()
or keybd_event()
to send keys to the currently focused window. SendInput()
might be the better choice as it would not intersperse the keyboard events with those form the actual keyboard.
// send CTRL+SHIFT+HOME
INPUT input[6];
memset(input, 0, sizeof(INPUT)*6);
input[0].type = INPUT_KEYBOARD;
input[0].ki.wVk = VK_CONTROL;
input[1].type = INPUT_KEYBOARD;
input[1].ki.wVk = VK_SHIFT;
input[2].type = INPUT_KEYBOARD;
input[2].ki.wVk = VK_HOME;
input[3].type = INPUT_KEYBOARD;
input[3].ki.wVk = VK_HOME;
input[3].ki.dwFlags = KEYEVENTF_KEYUP;
input[4].type = INPUT_KEYBOARD;
input[4].ki.wVk = VK_SHIFT;
input[4].ki.dwFlags = KEYEVENTF_KEYUP;
input[5].type = INPUT_KEYBOARD;
input[5].ki.wVk = VK_CONTROL;
input[5].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(6, input, sizeof(INPUT));
Upvotes: 3
Reputation: 24998
You can send a number of WM_KEYDOWNs followed by a number of WM_KEYUPs so you could try sending DOWN ctrl, shift, home, UP home, shift, ctrl - that should do the trick.
Upvotes: 0