Ctrl still pressed with keyb_event() - c++

i'm trying to help a friend with a macro for his mouse, but i've been strugling with an error.
But when i use :
if(GetAsyncKeyState(VK_XBUTTON2)){
keybd_event(VK_LCONTROL, 0xA2, 0x0001, 0);
Sleep(50);
keybd_event(VK_LCONTROL, 0xA2, 0x0002, 0);
Sleep(50); }
My ctrl still holded unless i click in my console and press ctrl again.

Don't use magic numbers in your code, it makes it harder to read and understand. Use named constants instead. In this case, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP. Then you will notice that you are not specifying the KEYEVENTF_EXTENDEDKEY flag when releasing the key. Use the | (bitwise OR) operator to combine flags.
Also, don't hard-code the scan code, as it may differ on different machines. Use MapVirtualKey() instead.
Try this:
const BYTE scanCode = MapVirtualKey(VK_LCONTROL, MAPVK_VK_TO_VSC);
...
if (GetAsyncKeyState(VK_XBUTTON2)){
keybd_event(VK_LCONTROL, scanCode, KEYEVENTF_EXTENDEDKEY, 0);
Sleep(50);
keybd_event(VK_LCONTROL, scanCode, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
Sleep(50);
}
That said, keybd_event() is deprecated, use SendInput() instead:
INPUT input = {};
input.type = INPUT_KEYBOARD;
input.ki.wVk = VK_LCONTROL;
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
...
if (GetAsyncKeyState(VK_XBUTTON2)){
input.ki.dwFlags &= ~KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
Sleep(50);
input.ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
Sleep(50);
}

Related

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

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.

KEYEVENTF_KEYUP does not work if KEYEVENTF_EXTENDEDKEY is used

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

from C++ how do I type a string to my desktop or word document on my computer (not window)

So far from c++ I found how to move the mouse to position x and y and to right and left click. I cannot seem to figure out how to click on something and then type from c++ . If I had a word document up I want to be able to click it, open it and type something into it. Thanks in advance !
#include <windows.h>
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
SetCursorPos(97,758);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click
Sleep( 1000 );
SetCursorPos(418,657);
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
SetCursorPos(266,34);
Sleep( 1000 );
mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//right here is where I would like to type something to the document
}
try using SendInput
SendInput on MSDN
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 "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));