keybd_event showing weird behavior - c++

I am trying to simulate enter key press using following code:
keybd_event(VK_RETURN , 0, 0, 0);
keybd_event(VK_RETURN , 0, KEYEVENTF_KEYUP, 0);
It works well in my 32 bit Windows XP OS but when I try in Windows 8 x64 machine, spacebar key press is simulated instead of enter key.
What should I do to get correct result?
I also tried using SendInput as shown below but i face the same problem.
void typeKey(short virtualKey)
{
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.wVk = virtualKey; // virtual-key code for the character
ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
//function call
typeKey(VK_RETURN);
Note: I am compiling a 32 bit executable on XP machine and using the same executable on Windows 8.

This is just an educated guess, but your old function uses argument types that differ in length from x86 to x64. That may be a problem. Listen to the documentation that says:
Note This function has been superseded. Use SendInput instead.

I confirm your observation on 64-bit Windows 8.1. For some reason, VK_RETURN does not work correctly with SendInput().
The solution seems to be to supply the scan code as well like this:
ip.ki.wScan = MapVirtualKey(virtualKey, MAPVK_VK_TO_VSC);

Related

SendInput in C or C++ do not mimic the keyboard exactly, is there a better function for this? [duplicate]

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)

Qt Send the given keys to the active application

I have been looking without success to any Qt class that has methods to send keys to MS Windows desktop OS active application, I wonder if there is straight solution. What I want is something similar to VS Clr/C++ class System::Windows::Forms::SendKeys which has the ethod SendWait(String^).
Under windows using SendInput from user32 library with Qt is direct and simple like this:
.pro
win32: LIBS += -lUser32
a function can take an arbitrary keys to be re-transmitted:
.cpp
#include "Windows.h"
// Because the SendInput function is only supported in
// Windows 2000 and later, WINVER needs to be set as
// follows so that SendInput gets defined when windows.h
// is included below.
#define WINVER 0x0500
void sendKeys::sendKeysqt(QString bCodeSequence)
{
// This structure will be used to create the keyboard
// input event.
int arraySizeqtt = bCodeSequence.length();
INPUT ip;
// Pause for 2 seconds.
Sleep(2000);
for (int i=0; i < arraySizeqtt; i++)
{
// 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
byte x = VkKeyScan(bCodeSequence.at(i).unicode());
ip.ki.wVk = x; // 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));
}
}

C Virtual Keyboard Input selectively working

I am working a on project that needs virtual input which is being coded in Microsoft Visual Studios using the windows.h header. To do this I am using the keybd_event() method and the VkKeyScan() method:
keybd_event(VkKeyScan('w'), 0, 0, 0);
keybd_event(VkKeyScan('w'), 0, KEYEVENTF_KEYUP, 0);
However, the virtual input is only being recognized by some programs, such as Notepad, Command Prompt, browsers, and other applications that use text fields. The purpose of virtual input for my project is to use a client to control "VisualGameBoyAdvance" which takes keyboard input that is then translated into a command such as 'w' = start, 'z' = a button, etc.
Why is the virtual input being read when using applications in text fields, but not as commands? Are there alternative methods of the windows.h header file or are there better methods in the header?
Update: I have been trying to use VkKeyScanEx() as an alternative method for taking virtual input as a shot-in-the-dark effort. How to I specify my input locale identifier? I have been trying to use UTF-8 and en_AU.UTF-8, with no luck. Is there a chart that translates how to specify this?
I've also tried using
keybd_event(GetKeyState('w'), 0, 0, 0);
keybd_event(GetKeyState('w'), 0, KEYEVENTF_KEYUP, 0);
Which did not work at all and tried
while(1){
/*Sleep(1000);
keybd_event(GetKeyState('w'), 0, 0, 0);
keybd_event(GetKeyState('w'), 0, KEYEVENTF_KEYUP, 0);*/
// Pause for 5 seconds.
Sleep(500);
// 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 "W" key
ip.ki.wVk = 0x57; // 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));
// Exit normally
}
The problem was that the game was running though DirectX, which prevented virtual keyboard strokes. By rending the game right from the sys, it avoided directx all together and worked fine :)

virtual key "backspace" not working [duplicate]

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)

SendInput() not equal to pressing key manually on keyboard in C++?

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)