KEYEVENTF_KEYUP does not work if KEYEVENTF_EXTENDEDKEY is used - c++

I try to simulate CTRL+NumENTER. I checked the code with a Keyboard Test Program (PassMark KeyboardTest) an it seems that NumENTER is pressed but not released. If i do not use KEYEVENTF_EXTENDEDKEY everything gets relesaed, but then I get the normal Return.
Here is my Code:
INPUT *ip = new INPUT[2]; // set up input 1
(ip)->type = INPUT_KEYBOARD; // set up keyboard event
(ip)->ki.wScan = 0;
(ip)->ki.time = 0;
(ip)->ki.dwExtraInfo = 0;
(ip)->ki.wVk = VK_CONTROL; // set ip[0] to CTRL
(ip)->ki.dwFlags = 0;
Sleep(100);
(ip+1)->type = INPUT_KEYBOARD; // set up keyboard event
(ip+1)->ki.wScan = 0;
(ip+1)->ki.time = 0;
(ip+1)->ki.dwExtraInfo = 0;
(ip+1)->ki.wVk = VK_RETURN; // set ip[1] to ENTER
(ip+1)->ki.dwFlags = KEYEVENTF_EXTENDEDKEY; // make ENTER to NumENTER
Sleep(100);
SendInput(2, ip, sizeof(INPUT)); // send key
Sleep(100);
(ip)->ki.dwFlags = KEYEVENTF_KEYUP; // event for keyrelease
(ip+1)->ki.dwFlags = KEYEVENTF_KEYUP; // event for keyrelease
Sleep(100);
SendInput(2, ip, sizeof(INPUT)); // release keys
return 0;

Use bitwise or to set dwFlags. KEYEVENTF_EXTENDEDKEY adds a prefix (0xE0 (224)) to scanCode.
(ip)->ki.dwFlags |= KEYEVENTF_KEYUP; // event for keyrelease
(ip+1)->ki.dwFlags |= KEYEVENTF_KEYUP; // event for keyrelease

Related

How can I select text using SendInput?

I'm making a small program to select the keyword where I've placed the caret on. I'm trying to send Shift + Ctrl + Right Arrow from my program to any active window to select the text.
The problem is the "Shift" key is not sent and the text is not selected.
How can I do it?
This is my code that I have tried:
// Generate Ctrl + Shift + RightArrow input
INPUT copyText[6];
// Set the press of the "Shift" key
copyText[0].ki.wVk = VK_SHIFT;
copyText[0].ki.dwFlags = 0; // 0 for key press
copyText[0].type = INPUT_KEYBOARD;
// Set the press of the "Ctrl" key
copyText[1].ki.wVk = VK_CONTROL;
copyText[1].ki.dwFlags = 0; // 0 for key press
copyText[1].type = INPUT_KEYBOARD;
// Set the press of the "C" key
copyText[2].ki.wVk = VK_RIGHT;
copyText[2].ki.dwFlags = 0;
copyText[2].type = INPUT_KEYBOARD;
// Set the Release of the "Shift" key
copyText[3].ki.wVk = VK_SHIFT;
copyText[3].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[3].type = INPUT_KEYBOARD;
// Set the Release of the "Ctrl" key
copyText[4].ki.wVk = VK_CONTROL;
copyText[4].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[4].type = INPUT_KEYBOARD;
// Set the Release of the "C" key
copyText[5].ki.wVk = VK_RIGHT;
copyText[5].ki.dwFlags = KEYEVENTF_KEYUP;
copyText[5].type = INPUT_KEYBOARD;
// Send key sequence to system
SendInput(static_cast<UINT>(std::size(copyText)), copyText, sizeof(INPUT));

Simulate double direct keyboard input in C++

I found this c++ script that let you simulate a direct keyboard input, however, I need to simulate two inputs like [Ctrl + A]. I've been looking for ways to make it but I can't find the answer, if someone could help me or give me a clue I'd be grateful.
#include "stdafx.h"
#pragma comment(lib,"user32")
using namespace std;
int main()
{
char ch = 'a';
INPUT key;
memset(&key,0,sizeof(INPUT));//Zero the structure.
key.type = INPUT_KEYBOARD;
key.ki.dwExtraInfo = GetMessageExtraInfo();//<-- you will need to pinvoke this too.
key.ki.wScan =
static_cast<WORD>(MapVirtualKeyEx(VkKeyScanA(ch), MAPVK_VK_TO_VSC, GetKeyboardLayout(0)));//more pinvoking
key.ki.dwFlags = KEYEVENTF_SCANCODE;//<-- you will probably have to declare this constant somewhere-
//in your C# program.
//Ready to send the key-down event.
SendInput(1, &key, sizeof(INPUT));
Sleep(1000);//Wait one second before sending key-up.
//Sending key-up.
key.ki.dwExtraInfo = GetMessageExtraInfo();
key.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;//Key-up need be defined too, or just use the value.
SendInput(1, &key, sizeof(INPUT));
}
I've written this code, Ctrl press isn't working, however, 'W' press does.
int main()
{
Sleep(2000);
INPUT ip;
char key1 = VK_CONTROL, key2 = 'w';
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wScan = key1;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Press key2
ip.ki.wScan = key2;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release key2
ip.ki.wScan = key2;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
// Release key1
ip.ki.wScan = key1;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
I've noticed if I delete the VK_CONTROL press lines, 'W' press doesn't work neither.
int main() {
Sleep(2000);
INPUT ip;
char key2 = 'w';
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press key2
ip.ki.wScan = key2;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release key2
ip.ki.wScan = key2;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
To send a ctrl-a, you need to send, in order:
key-down events
VK_CONTROL
virtual code for a
key-up events
virtual code for a
VK_CONTROL
so, using VkKeyScanExW to get the virtual scan code for 'a'
key.ki.wVk = VK_CONTROL;
/* send keystroke */
key.ki.wVk = /* the virtual key code for a that you derived */
/* send keystroke */
/* set flag to keyup */
key.ki.wVk = /* Virtual code for a */
/* send keystroke */
key.ki.wVk = VK_CONTROL;
/* send keystroke */
where VK_CONTROL is the macro defined in Windows that gives you the keyboard virtual code for ctrl. Leave wScan set to 0.
You are sending virtual codes to the scan code parameter. The scan code for w is not 'w'.
In scan code set 1 (US keyboards), w's scan code is 0x11, and left control's scan code is 0x1D. Microsoft's Keyboard Scan Code Specification details sets 1 and 2, and can be found at https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc. There are commonly 3 sets of scan codes, and they are enumerated various places on the web. Or, you can use a utility to capture your own keyboard's scan codes. Sharpkeys or AutoHotkey appear to be popular utilities for doing just this.

C++ How to simulate special key strokes (alt+numepad)

I'm working on a project where I have to send certain text into specific webpage (in Chrome) but I can't send it directly or copy&paste it. The solution I came up with is keyboard bot - I use virtual keys to do the job, but in that text I have special sign "↔" (Alt+numepad2+numepad9) and I can't make it with virtual keys. I'll be greatful for any working solution. I did try various methods I found but nothing worked, if I simulate only alt press and manually type on numepad it works, but not when program does it.
Here's my code:
#define WINVER 0x0500
#include <windows.h>
int main()
{
INPUT typ;
typ.type = INPUT_KEYBOARD;
typ.ki.wScan = 0;
typ.ki.time = 0;
typ.ki.dwExtraInfo = 0;
//Time to select window
Sleep(5000);
// Press the "X" key
typ.ki.wVk = 'X';
typ.ki.dwFlags = 0; // 0 for key press
SendInput(1, &typ, sizeof(INPUT));
// Release the "X" key
typ.ki.wVk = 'X';
typ.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &typ, sizeof(INPUT));
// Press the "Alt" key
typ.ki.wVk = VK_MENU;
typ.ki.dwFlags = KEYEVENTF_EXTENDEDKEY|0;
SendInput(1, &typ, sizeof(INPUT));
//Sleep(3000);
// Press the "2" key
typ.ki.wVk = VK_NUMPAD2;
typ.ki.dwFlags = KEYEVENTF_EXTENDEDKEY|0;
SendInput(1, &typ, sizeof(INPUT));
// Release the "2" key
typ.ki.wVk = VK_NUMPAD2;
typ.ki.dwFlags = KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP;
SendInput(1, &typ, sizeof(INPUT));
// Release the "Alt" key
typ.ki.wVk = VK_MENU;
typ.ki.dwFlags = KEYEVENTF_EXTENDEDKEY|KEYEVENTF_KEYUP;
SendInput(1, &typ, sizeof(INPUT));
return 0;
}
If someone needs the unicode key press
// Press a unicode "key"
typ.ki.dwFlags = KEYEVENTF_UNICODE;
typ.ki.wVk = 0;
typ.ki.wScan = 0x2194; // HEX UNICODE
SendInput(1, &typ, sizeof(INPUT));
// Release key
typ.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
SendInput(1, &typ, sizeof(INPUT));
This works:
keybd_event(0x12, MapVirtualKey(0x12, 0), 0, 0); //Alt press
keybd_event(0x62, MapVirtualKey(0x62, 0), 0, 0); // Numpad2 press
keybd_event(0x62, MapVirtualKey(0x62, 0), KEYEVENTF_KEYUP, 0); //Numpad2 relese
keybd_event(0x69, MapVirtualKey(0x69, 0), 0, 0); // Numpad9 press
keybd_event(0x69, MapVirtualKey(0x69, 0), KEYEVENTF_KEYUP, 0); //Numpad9 relese
keybd_event(0x12, MapVirtualKey(0x12, 0), KEYEVENTF_KEYUP, 0); // Alt relese

Virtual key codes

I have a c++ console program. Ho can I simulate the "é" character ?
Code:
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = 0x45; //e
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
How do I convert it to the "é" instead of "e"?
Thanks.
Per the KEYBDINPUT documentation, you can use the KEYEVENTF_UNICODE flag:
INPUT ip[2];
// Set up a generic keyboard event.
ip[0].type = INPUT_KEYBOARD;
ip[0].ki.dwFlags = KEYEVENTF_UNICODE; // We want to send a Unicode key code.
ip[0].ki.wScan = 0x00E9; // Unicode value of é
ip[0].ki.time = 0;
ip[0].ki.dwExtraInfo = 0;
ip[0].ki.wVk = 0; // Ignored
ip[1] = ip[0]; // Duplicate entry
ip[1].ki.dwFlags |= KEYEVENTF_KEYUP; // but make it key up
SendInput( 2, ip, sizeof(ip[0]));
(If you are ambitious, you can make that final "2" be your favourite ARRAY_COUNT macro or template function.)

How to print/press '#' using keybd_event function?

To press 'a' code is
keybd_event(VkKeyScan(64),0,0,0);
Releasing key code is
keybd_event(VkKeyScan(64),0,KEYEVENTF_KEYUP,0);
For pressing '#' i need combination of two key - SHIFT & 2 , but i don't know how.
keybd_event (https://msdn.microsoft.com/en-us/library/windows/desktop/ms646304(v=vs.85).aspx)
Try the following:
Press Shift
Press 2
Release 2
Release Shift
Addendum
I just checked my own code where I did the same thing... keybd_event is deprecated (as stated on the site you linked), you should use SendInput instead.
This are my two functions to send the key press and release:
void sendKeyDown(unsigned char keyCode)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = keyCode;
input.ki.dwFlags = 0;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
SendInput(1, &input, sizeof(INPUT));
}
void sendKeyUp(unsigned char keyCode)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wVk = keyCode;
input.ki.dwFlags = KEYEVENTF_KEYUP;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
SendInput(1, &input, sizeof(INPUT));
}
And this should then give you an #:
sendKeyDown(VK_SHIFT);
sendKeyDown(0x32);
sendKeyUp(0x32);
sendKeyUp(VK_SHIFT);
Please check the 0x32, I can't reliably test it at the moment to be the key 2.