Programmaticaly simulating Alt + Enter key press is not working - c++

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

Related

how to simulate Ctrl + V using keybd_event () in 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.

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
}

C++ Simulating key presses only once

I have tried everything I found in google but nothing works. I want to press A for 5 seconds, but everything presses it only once.It just releases it immediately. Pressing multiple times is not the answer because the key must be pressed for atleast 30ms.
const int KEYEVENT_KEYUP = 0x02;
keybd_event(0x41, 0, 0, 0);
Sleep(5000);
keybd_event(0x41, 0, KEYEVENT_KEYUP, 0);

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);
}