d2jsp
d2jsp Forums > Programmer's Haven > C/C++/C# > Need Help To Do Ctrl + V
Add Reply New Topic New Poll
CPK001
#1 Feb 4 2020 04:55pm
Group: Member
Posts: 16,197
Joined: Mar 13 2009
Gold: 0.00
I'm working on a piece of C++ code. I am trying to simulate pressing ctrl and v for copy paste. The code does press ctrl and v but it doesn't release the ctrl key afterwards.

How would you improve the code to do ctrl v and release both keys?

Code
#include <Windows.h>

int main()
{
while(true) {
Sleep(3000);
INPUT Input = { 0 };
Input.type = INPUT_KEYBOARD;
// Control Key Down
Input.ki.wVk = 0x11; // Control Key
Input.ki.dwFlags = 0;
SendInput(1, &Input, sizeof(Input));
// V Key Down
Input.ki.wVk = 0x56; // V Key
Input.ki.dwFlags = 0;
SendInput(1, &Input, sizeof(Input));
// Keyboard UP
Input.ki.dwFlags = KEYEVENTF_KEYUP;
}
}
Klexmoo
#2 Feb 7 2020 04:07pm
Group: Member
Posts: 10,347
Joined: May 17 2013
Gold: 53,243.00
You are not sending an input to release the keys

Code

// Release the V key
Input.ki.wVk = 0x56;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));

// Release the control key
Input.ki.wVk = 0x11;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));


you can also make an array of the inputs instead of doing them one by one (and send it in one go with SendInput)

see https://social.msdn.microsoft.com/Forums/vstudio/en-US/380e2bb4-fd6a-438b-9922-30b599b5c311/how-to-press-a-key-using-its-virtual-key-with-sendinput?forum=vcgeneral
Go Back To C/C++/C# Topic List
Add Reply New Topic New Poll