d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help To Do Ctrl + V
Add Reply New Topic New Poll
Member
Posts: 17,256
Joined: Mar 13 2009
Gold: 0.00
Feb 4 2020 04:55pm
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;
}
}
Member
Posts: 12,703
Joined: May 17 2013
Gold: 2,935.00
Feb 7 2020 04:07pm
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 Programming & Development Topic List
Add Reply New Topic New Poll