How to print/press '#' using keybd_event function? - c++

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.

Related

Press a keyboard key by his char value C++

I would like to write a function that receives a char and presses it on the keyboard.
void pressKey(const char key){
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = //What to put here? (receives WORD of the hex value)
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));
}
How can I press the key 'a' (for example) and press it using this method or any other method?
When simulating keyboard input for text, you should use the KEYEVENTF_UNICODE flag to send Unicode characters as-is, use virtual key codes only for non-textual keys. And you need to send 2 input events per character, one event to press the key down, and one event to release it.
For example:
void pressKey(const char key){
INPUT ip[2] = {};
ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wScan = key;
ip[0].ki. dwFlags = KEYEVENTF_UNICODE;
ip[1] = ip[0];
ip[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, ip, sizeof(INPUT));
}
That being said, since this approach requires Unicode characters, if your key will ever contain a non-ASCII character then you will need to first convert it to Unicode UTF-16, such as with MultiByteToWideChar() or equivalent, and then you can send down/up events for each UTF-16 codeunit, as shown in this answer.

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.

unable to do alt code (alt+11) with send_input

What I an trying to do is send the alt code alt+1 which = ☺ with send input. so i have to hold atl, press numpad 1, then let go of alt. Currently when the code runs it does hold the alt button, but has no output into the active window (notepad). I verified sendinput is working with key combos (ctrl+a) work. Num lock is turned on while this program is running. alt+f also works. I think the problem is that it is maybe sending '1' instead of numpad '1', which does not work with alt codes.
Sleep(1000 * 3);
INPUT tabinput[4];
tabinput[0].type = INPUT_KEYBOARD;
tabinput[0].ki = { VK_LMENU, 0 };
tabinput[1].type = INPUT_KEYBOARD;
tabinput[1].ki = { VK_NUMPAD1, 0 };
tabinput[2].type = INPUT_KEYBOARD;
tabinput[2].ki = { VK_NUMPAD1, 0, KEYEVENTF_KEYUP };
tabinput[3].type = INPUT_KEYBOARD;
tabinput[3].ki = { VK_LMENU, 0, KEYEVENTF_KEYUP };
SendInput(4, tabinput, sizeof(INPUT));
getchar();
You can use Unicode characters directly. If code point is less than 0xFFFF use:
input.ki.wScan = L'☺';
Some characters have code point greater than 0xFFFF, they require two key strokes for each code point, in which case use a wide string to handle it:
const wchar_t *buf = L"বাংলা ☺ 🙂 Abc";
std::vector<INPUT> vec;
for(size_t i = 0, len = wcslen(buf); i < len; i++)
{
INPUT input = { 0 };
input.type = INPUT_KEYBOARD;
input.ki.dwFlags = KEYEVENTF_UNICODE;
input.ki.wScan = buf[i];
vec.push_back(input);
input.ki.dwFlags |= KEYEVENTF_KEYUP;
vec.push_back(input);
}
SendInput(vec.size(), vec.data(), sizeof(INPUT));
the following code can be used to type unicode chars
using http://www.unicodemap.org/search.asp to find the correct code
INPUT tabinput[2];
tabinput[0].type = INPUT_KEYBOARD;
tabinput[0].ki.dwFlags = KEYEVENTF_UNICODE;
tabinput[0].ki.wVk = 0;
tabinput[0].ki.wScan = 0x00E1; // This is a Bengali unicode character
tabinput[1].type = INPUT_KEYBOARD;
tabinput[1].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
tabinput[1].ki.wVk = 0;
tabinput[1].ki.wScan = 0x00E1; // This is a Bengali unicode character
SendInput(2, tabinput, sizeof(INPUT));

How to highlight an entire word in C++?

I have made the following C++ program to highlight the last word before the cursor in a typing interface by sending Control+Shift+Left and then copy it to clipboard by sending Control+C.
#define WINVER 0x0500
#include <windows.h>
#include <Winuser.h>
using namespace std;int main() {
// Create a generic keyboard event structure
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
while(true) {
if( GetKeyState(VK_LMENU) & 0x8000 ) {
Sleep(200);
// Press the "Ctrl" key
ip.ki.wVk = VK_CONTROL;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Press the "Shift" key
ip.ki.wVk = VK_SHIFT;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Press the "Left" key
ip.ki.wVk = VK_LEFT;
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "Left" key
ip.ki.wVk = VK_LEFT;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
// Release the "Shift" key
ip.ki.wVk = VK_SHIFT;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
// Press the "C" key
ip.ki.wVk = 'C';
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "C" key
ip.ki.wVk = 'C';
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
// Release the "Ctrl" key
ip.ki.wVk = VK_CONTROL;
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}}}
This is meant to work when I press the Left-Alt key. It works fine for words like abc or hello but not with words like #abc or hello%hello. I need to make it work for the entire word. By "entire word" what I mean is any collection of characters that does not include spaces or line breaks.
If you can't solve my problem completely, please know that I am open to workarounds that may work differently or include certain limitations. But I really this so please help.
And please feel free to suggest edits to help me improve this question.
As IInspectable mentioned, obviously what you consider to be a word and what the text field considers to be a word are two different things and you can't really do anything about it. So instead of trying to simulate the input which is not getting you what you want anyway, you should try and retrieve a handle (HWND) for the text field using some combination of FindWindowEx calls.
Now I can't exactly tell you how to find the window you need because I don't know where it is on your system and which application it belongs to. But you might be able to get the required information to do this (window hierarchy and class names) using some tool such as Inspect.
After that you should be able to get the text from the text field and parse it to get the first word:
#include <Windows.h>
int main()
{
/* You should adjust the following code to whatever criteria
you are using to choose the text field */
HWND appWindow = FindWindowEx(GetDesktopWindow(), NULL, NULL, "App window title");
HWND editControl = FindWindowEx(appWindow, NULL, "EDIT", NULL);
int size = GetWindowTextLength(editControl) + 1;
char *text = new char[size];
GetWindowText(editControl, text, size);
int cursorPos = 0;
SendMessage(editControl, EM_GETSEL, (WPARAM) &cursorPos, NULL);
for (int i = cursorPos; i < size; ++i) {
if (text[i] == ' ') {
text[i] = '\0';
break;
}
}
char *word = &text[cursorPos];
//do whatever you need with the word here
delete[] text;
return 0;
}
I didn't really have the opportunity to test this code and regarding copying the text to the clipboard: it seems to be a lot more complicated task to achieve using Win32 API which is in great detail described here.

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.)