I wrote a simple program which simulates clicking particular button. I strive to convert 'a' to 0x41.
#include <iostream>
#include <Windows.h>
#define WINVER 0x0500
int main() {
INPUT ip;
// Pause for 5 seconds.
Sleep(5000);
// 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;
// Press the "A" key
ip.ki.wVk = 0x41; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "A" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
return 0;
}
After compiling and executing following code It does clicking "a" which is correct. But when I change value in ip.ki.wVk to for ex. (int)'a' it acts different.
ip.ki.wVk = 0x41; //gives me "a" which is correct
ip.ki.wVk = 65 //the same as above which is good;
but
ip.ki.wVk = int('a')//gives "1";
Please try
ip.ki.wVk = int('A')
The virtual key code represents ASCII value of the letter in UPPERCASE value. Hence 'a' is turning to be the wrong key value.
The list of virtual keys mentioned here
'a' = 97 = 0x61 is Virtual-Key code for 'Numeric keypad 1 key`.
Related
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.
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.
I have MFC application which gets invoked on hotkey (should be running, just brings it to front), send clipboard data to one of the text box using,
SendMessage(hWnd,WM_PASTE,0,0);
Everything is working fine till now.
We get new requirement wherein we should be able to copy selected text to clipboard using hot key (without using Ctrl + C), and then launch our application.
I tried to use SendMessage(WM_COPY), but does not work.
Please suggest how to get selected text of any other application on clipboard.
Finally I myself got the answer.
I had to use SendInput, following is sample code,
INPUT ip;
// 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;
// Press the "Ctrl" key
ip.ki.wVk = VK_CONTROL;
ip.ki.dwFlags = 0; // 0 for key press
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));
I wanted to write a c++ code to emulate pressing a keyboard key "A":
// 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;
// Press the "..." key
ip.ki.wVk = code; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "..." key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
It works fine when I launch other program and wait to my program execute, the "A" is clicked and first program react to it. But I found that in the other application my action was somehow prevented (I can manually press "A" on keyboard, but using my program do not cause any action).
So, what I can do to make pressing "A" from program more identical to manually pressed "A" (so the second program won't recognize that it was called from program)?
I do not have source code of second program and do not know how it recognize that "A" wasn't pressed manually.
I'm sure that the window I want to react to my code is foreground, receive and block my key (so it can decide that event doesn't come from user but from program).
You can use SendInput() to send hardware scan codes as well (as opposed to virtual scan codes, which DirectInput might ignore). It's poorly documented, but SendInput() can indeed bypass DirectInput. The reason Eric's solution didn't work is he set the hardware scan code, but ended up using a virtual scan code (by setting dwFlags to 0 and wVk to non-zero).
Essentially, to do a key press you want to set:
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
And to do a key release, set:
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
A full working sample is below and it prints the letter 'a'. You can find other scan codes here.
#define WINVER 0x0500
#include <windows.h>
using namespace std;
int main()
{
//Structure for the keyboard event
INPUT ip;
Sleep(5000);
//Set up the INPUT structure
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; //We're doing scan codes instead
ip.ki.dwExtraInfo = 0;
//This let's you do a hardware scan instead of a virtual keypress
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
ip.ki.wScan = 0x1E; //Set a unicode character to use (A)
//Send the press
SendInput(1, &ip, sizeof(INPUT));
//Prepare a keyup event
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
return 0;
}
Note: You can combine keypresses (like, shift + a for A) by passing SendInput() an array of INPUT structures.
You often need to set the scan code:
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = MapVirtualKey(code, MAPVK_VK_TO_VSC); // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the "..." key
ip.ki.wVk = code; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
And building an array as IInspectable suggests is also definitely the way to go.
If you are looking to create a game bot, have you looked at the program AutoHotKey?
http://www.autohotkey.com/
It offers a scripting language, that allows you to do a lot of the tasks involved in 'bot' creation and it's rather easier than trying to do it all in C++
(It certainly played Farmville for me, when all my family pressured me into creating an account)
I wanted to write a c++ code to emulate pressing a keyboard key "A":
// 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;
// Press the "..." key
ip.ki.wVk = code; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "..." key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
It works fine when I launch other program and wait to my program execute, the "A" is clicked and first program react to it. But I found that in the other application my action was somehow prevented (I can manually press "A" on keyboard, but using my program do not cause any action).
So, what I can do to make pressing "A" from program more identical to manually pressed "A" (so the second program won't recognize that it was called from program)?
I do not have source code of second program and do not know how it recognize that "A" wasn't pressed manually.
I'm sure that the window I want to react to my code is foreground, receive and block my key (so it can decide that event doesn't come from user but from program).
You can use SendInput() to send hardware scan codes as well (as opposed to virtual scan codes, which DirectInput might ignore). It's poorly documented, but SendInput() can indeed bypass DirectInput. The reason Eric's solution didn't work is he set the hardware scan code, but ended up using a virtual scan code (by setting dwFlags to 0 and wVk to non-zero).
Essentially, to do a key press you want to set:
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
And to do a key release, set:
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
A full working sample is below and it prints the letter 'a'. You can find other scan codes here.
#define WINVER 0x0500
#include <windows.h>
using namespace std;
int main()
{
//Structure for the keyboard event
INPUT ip;
Sleep(5000);
//Set up the INPUT structure
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0; //We're doing scan codes instead
ip.ki.dwExtraInfo = 0;
//This let's you do a hardware scan instead of a virtual keypress
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
ip.ki.wScan = 0x1E; //Set a unicode character to use (A)
//Send the press
SendInput(1, &ip, sizeof(INPUT));
//Prepare a keyup event
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
return 0;
}
Note: You can combine keypresses (like, shift + a for A) by passing SendInput() an array of INPUT structures.
You often need to set the scan code:
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = MapVirtualKey(code, MAPVK_VK_TO_VSC); // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
// Press the "..." key
ip.ki.wVk = code; // virtual-key code for the "a" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
And building an array as IInspectable suggests is also definitely the way to go.
If you are looking to create a game bot, have you looked at the program AutoHotKey?
http://www.autohotkey.com/
It offers a scripting language, that allows you to do a lot of the tasks involved in 'bot' creation and it's rather easier than trying to do it all in C++
(It certainly played Farmville for me, when all my family pressured me into creating an account)