how to simulate Ctrl + V using keybd_event () in C++ - c++

i want to paste text from clipboard to some program's textbox. so i tried to use keybd_event.
keybd_event(VK_CONTROL,0x1D, 0, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);
but this is not executed. so i tried different way
keybd_event(VK_CONTROL,0x1D, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event('V', 0x2F, 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_KEYUP, 0);
so this work well. However, since then, all keyboard inputs have been entered with the ctrl key pressed.
maybe i think key-up message is not worked
how to solve this problem?

However, since then, all keyboard inputs have been entered with the
ctrl key pressed. maybe i think key-up message is not worked
You simulate right CONTROL WM_KEYDOWN but left CONTROL WM_KEYUP. So the right-hand CTRL key has not been released.
The following code will work:
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | 0, 0);
keybd_event('V', 0x2F, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
extended-key flag (KEYEVENTF_EXTENDEDKEY)
Indicates whether the key is an extended key, such as the right-hand
ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard.
The value is 1 if it is an extended key; otherwise, it is 0.
Refer to WM_KEYUP message.
keybd_event function has been superseded. Use SendInput instead.

Related

Ctrl still pressed with keyb_event()

i'm trying to help a friend with a macro for his mouse, but i've been strugling with an error.
But when i use :
if(GetAsyncKeyState(VK_XBUTTON2)){
keybd_event(VK_LCONTROL, 0xA2, 0x0001, 0);
Sleep(50);
keybd_event(VK_LCONTROL, 0xA2, 0x0002, 0);
Sleep(50); }
My ctrl still holded unless i click in my console and press ctrl again.
Don't use magic numbers in your code, it makes it harder to read and understand. Use named constants instead. In this case, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP. Then you will notice that you are not specifying the KEYEVENTF_EXTENDEDKEY flag when releasing the key. Use the | (bitwise OR) operator to combine flags.
Also, don't hard-code the scan code, as it may differ on different machines. Use MapVirtualKey() instead.
Try this:
const BYTE scanCode = MapVirtualKey(VK_LCONTROL, MAPVK_VK_TO_VSC);
...
if (GetAsyncKeyState(VK_XBUTTON2)){
keybd_event(VK_LCONTROL, scanCode, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(50);
keybd_event(VK_LCONTROL, scanCode, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
Sleep(50);
}
That said, keybd_event() is deprecated, use SendInput() instead:
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LCONTROL;
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
...
if (GetAsyncKeyState(VK_XBUTTON2)){
input.ki.dwFlags &= ~KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
Sleep(50);
input.ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
Sleep(50);
}

PostMessage / SendMessage without interference from modifier keys

I'm trying to post/send a message to some hwnd without the interference from modifier keys (ctrl, alt, shift).
Basically i want to send F1 message (without ctrl) to hwnd while im pressing ctrl (like Example 2) but with SendMessage\PostMessage.
I tried use SendInput to set up the CTRL key, post the message and set down the CTRL key back, but it fails 50% of time.
Example 1: Code with SendMessageA:
HWND hwnd = FindWindowA(0, "Notepad");
if (GetKeyState(VK_CONTROL) < -1) // if CTRL is pressed
{
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // up the CTRL key
SendMessageA(hwnd, WM_KEYDOWN, VK_F1, 0); // send F1 keydown
keybd_event(VK_CONTROL, 0, 0, 0); // down the CTRL key
}
else
{
SendMessageA(hwnd, WM_KEYDOWN, VK_F1, 0); // send F1 keydown
}
Theoretically this code would solve the problem, but it sometimes sends the message with the CTRL pressed and sometimes not.
Example 2: Same code with SendInput (but these works fine)
HWND hwnd = FindWindowA(0, "Notepad");
if (GetKeyState(VK_CONTROL) < -1) // if CTRL is pressed
{
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0); // up the CTRL key
keybd_event(VK_F1, 0, 0, 0); // send F1 keydown
keybd_event(VK_CONTROL, 0, 0, 0); // down the CTRL key back
}
else
{
keybd_event(VK_F1, 0, 0, 0); // send F1 keydown
}

Programmaticaly simulating Alt + Enter key press is not working

Here is my code:
keybd_event(VK_MENU, 0, 0, 0);
keybd_event(VK_RETURN, 0, 0, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
The first line would press Alt
The second line would press Enter ↵ (or Return ↵),
The fourth line would release Alt,
The fifth line would release Enter ↵ (or Return ↵).
You are not setting the KEYEVENTF_EXTENDEDKEY flag to keep the keys pressed down. Change your code to:
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(200);
keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
Also you really don't need the sleep in the middle if you are just sending a Alt + Enter
You can see all of the keycodes here at the MSDN page.
Alt = VK_MENU
Left Alt = VK_LMENU
Right Alt Gr = VK_RMENU

C++ sends virtual key with WM_KEYDOWN on keybd_event()

I would like my program to send this third party virtual key VK_ALOGIN or 0xf7 with WM_KEYDOWN.
So I try the following:
keybd_event(0xf7, 0, 0, 0);
But the program that receives this keyboard message, it receives only VK_ALOGIN, and not with WM_KEYDOWN. So is it possible that I can send 0xf7 with WM_KEYDOWN?
Please note that VK_ALOGIN is not Microsoft Virtual Key.
Receiver might be looking for keydown, as well as key up. Try sending both.
keybd_event( VK_ALOGIN, 0, 0, 0 );
keybd_event( VK_ALOGIN, 0, KEYEVENTF_KEYUP, 0 );
or
keybd_event( VK_ALOGIN, 0, KEYEVENTF_EXTENDEDKEY, 0 );
keybd_event( VK_ALOGIN, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0 );
Edit: I deleted reference to SendInput because now I think it has nothing to do with the question, and I am not sure if I got SendInput right.

Cant set keyboard layout to wanted

I am writing a program and I need to change input language to english if it isn't.
I use LoadKeyboardLayout() and ActivateKeyboardLayout() but it doesen't change the input language. What am I doing wrong?
Code:
int Functions::KeyPush(char Index)
{
LoadKeyboardLayout("00000409", KLF_ACTIVATE);
ActivateKeyboardLayout(HKL_PREV, KLF_SETFORPROCESS);
keybd_event(
Index,
Index,
KEYEVENTF_EXTENDEDKEY | 0,
0);
keybd_event(
Index,
Index,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
return GetAsyncKeyState(Index);
}