INPUT, INPUT_KEYBOARD, ip were not declared in this scope - c++

#include <windows.h>
int main()
{
// This structure will be used to create the keyboard
// input event.
INPUT ip;
// Pause for 10 seconds.
Sleep(1000*10);
// 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 "F5" key
ip.ki.wVk = 0x74; // virtual-key code for the "F5" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "F5" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
// Exit normally
return 0;
}
The codes above are simulating to press F5 button, but there are some errors related to declaration.
INPUT, INPUT_KEYBOARD, ip were not declared in this scope.
How to solve it?

Without knowing what compiler and platform toolset you're using it's hard to determine what, exactly, is wrong. Reading the fine manual says:
Header Winuser.h (include Windows.h)
Which you're doing.
It also says:
A problem with INPUT using
When I try to use the INPUT structure, my compiler tells me this structure is undeclared, but I have included <windows.h>. After some searching, I find a solution, the <winable.h> should also be included. I don't know why, but it works.
MarrySunny
12/6/2011
Although you'd probably be better off using a recent version of Microsoft Visual Studio and the Windows SDK.

You're trying to use "SendInput()", which isn't available in some versions of Windows.
One possibility is to this:
#define WINVER 0x0500
#include <windows.h>
...
This might force Windows to use a specific version ... and might (depending on your specific platform) work exactly like you want :)
Here is the documentation:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx
Another possibility might be to substitute an OLDER APi, like keybd_event:
http://msdn.microsoft.com/en-us/library/ee504289.aspx
To be sure, it would be useful to know your specfic:
Platform: Windows 7?
Compiler: MSVS 2013?
Library: Microsoft-provided Win32 library? "Something else"?

Related

How to use DeviceIOControl to send keyboard press to a video game?

I am trying to make a way of passing keyboard and mouse inputs over a LAN connection. While I have been successful at doing this using the SendInput() function for things like Notepad and Visual Studio, the inputs are not registered in many video games. After doing some research, it appears that I'll need to use DeviceIOControl to accomplish the task, however I have no idea how I am supposed to do this when the keycodes have to be determined at runtime.
I'm not sure if I can just arbitrarily set the hDevice parameter to the incoming keycode or not, or what the code would look like in the end.
If it helps, the below code is what is currently being used to input key presses. The server receives a string which is parsed out to determine the key press and the state of the key (whether it's a key up or key down event). This information is passed as the parameters to this function:
void keyPress(WORD k, int state)
{
INPUT Inputs;
std::cout<<"Key="<<k<<", state="<<state<<std::endl;
Inputs.type=INPUT_KEYBOARD;
Inputs.ki.wVk=k;
if(state==1)
{
Inputs.ki.dwFlags=0;
SendInput(1, &Inputs, sizeof(INPUT));
}
else
{
Inputs.ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(1, &Inputs, sizeof(INPUT));
}
if((int)k!=0) //Keycode of 0 means that the last key pressed is being held down.
{
LastKey=k;
key[ 0]=LastKey;
}
}
Also, if I am going down the wrong rabbit hole and there is a better solution, please let me know!
Thank you,

SendInput doesn't work with Print Screen key

I'm making a front end for various emulators and triggering their various functionality such as save/load state, save screenshot, but with a unified interface. FS-UAE annoyingly uses "Print Screen" as its screenshot key, and I'd like to avoid the user having to change emulators from their default hotkey settings.
I've managed to simulate any key press I want with SendInput, except for the "Print Screen" key.
I haven't had any luck with using virtual key codes, I think that doesn't work with full screen applications. Hence that part of the code is commented out. (EDIT: better explanation - Virtual Key codes are ignored by DirectInput software)
Using scan codes, I can get any key to press - almost. Print Screen seems to be the odd one out.
Here's the reference I'm using for the scan codes;
https://msdn.microsoft.com/en-us/library/aa299374(v=vs.60).aspx
Below is the minimum viable code to reproduce the problem. If you run it, press a key then quickly switch to notepad and wait 2 seconds, it should press the letter "q" into notepad, then quit.
Change the scan code from 0x10 (q) to 0x37 (Print Screen), be sure to do it in both places - KEY DOWN, and KEY UP.
Now run it again, press a key and wait. To see if Print Screen worked, open MS Paint or whatever and press CTRL+V, see if you get a screenshot of your desktop. It doesn't work! But if you manually press Print Screen, and CTRL+V into MS Paint, it will work.
Why doesn't the Print Screen key work?
#include "stdafx.h"
//For create process & keyboard codes
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int main()
{
INPUT ip = {};
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0;
ip.ki.wVk = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.dwFlags = 0;
printf("Press a key, then taskswitch.\n");
system("pause");
Sleep(2000);
//KEY DOWN
ip.ki.wScan = 0x10; //0x37 PrintScreen, 0x10 Q
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
//ip.ki.wVk = VK_SNAPSHOT;
//ip.ki.dwFlags = 0;
SendInput(1, &ip, sizeof(INPUT));
//KEY UP
ip.ki.wScan = 0x10;
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
//ip.ki.wVk = VK_SNAPSHOT;
//ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
printf("Done.\n");
system("pause");
return 0;
}
Use wVk instead of wScan, and make sure KEYEVENTF_SCANCODE is not set because that ignores wVk. You have to use VK_SNAPSHOT
INPUT ip[2] = { 0 };
ip[0].type = INPUT_KEYBOARD;
ip[0].ki.wVk = VK_SNAPSHOT;
ip[1] = ip[0];
ip[1].ki.dwFlags |= KEYEVENTF_KEYUP;
SendInput(2, ip, sizeof(INPUT));
Okay I'm answering my own question here;
So I've done some more research, found this amazing post by someone who's nutted out the details of scan codes, and it seems that Print Screen is a weird one, requiring a special sequence to be triggered properly;
https://handmade.network/forums/t/2011-keyboard_inputs_-_scancodes,_raw_input,_text_input,_key_names
(And for additional reading, there is this article on the history of the 3 different scan code sets, which gives some insight as to why some keys might be weird, as new keys were added to the standard) http://www.quadibloc.com/comp/scan.htm
However, I still couldn't get it to work trying the variety of code sequences in that article - in the end, I managed to work around my original problem by learning that FS-UAE supports an ALTERNATE key sequence for saving a screenshot: F12-S, which I am using instead and it's working great.
So I may have avoided the issue this time, but woe be the person who needs to legitimately simulate a Print Screen key press at the scan code level WITHOUT using "Virtual Key codes"... it is a mysterious arcane task that I have still not achieved.

Detect KeyDown KeyPress and KeyUp events in g++

I am editing a very old question of mine when I used to use turbo c++. So now I may not be able to explain exact behaviour of turboc++ compiler and hence will make some assumptions.
I know turbo c/c++ is not standard but am trying to achieve similar behaviour in g++ ubuntu.
Before I ask my question, it is necessary to quote the meaning of KeyDown, KeyPress and KeyUp event taken from one of the answers from Difference between the KeyDown Event, KeyPress Event and KeyUp Event in Visual Studio
The MSDN documentation states the order in which the three events occur fairly clearly:
Key events occur in the following order:
KeyDown
KeyPress
KeyUp
KeyDown is raised as soon as the user presses a key on the keyboard, while they're still holding it down.
KeyPress is raised for character keys (unlike KeyDown and KeyUp, which are also raised for noncharacter keys) while the key is pressed. This is a "higher-level" event than either KeyDown or KeyUp, and as such, different data is available in the EventArgs.
KeyUp is raised after the user releases a key on the keyboard.
Generally, you should handle the KeyUp event in your application. Actions should not be initiated in the UI until after the user releases the key. And since KeyUp is a lower-level event than KeyPress, you'll always have plenty of information at your fingertips about the key that was pressed, and it will even work for handling non-character keys.
NOTE : My question has nothing to do with MSDN. It was quoted only to get familiar with the KeyDown KeyPress and KeyUp events.
Now, getch in turbo c/c++ does not need the key to be released. The execution of getch(); gets completed as soon as one touches/presses any keyboard key(most probably only alphanumeric keys(assumption)).
Now a number of questions like this(Detecting when a key is released) and many other focus on windows plateform and not on g++ linux environment.
This question
C++ mutiple key input or key press/release events seems closest to my question but the question seems incomplete with information about neither the methodologies/code used to take input nor any error message(s).
Answer by Stephen Veiss with 29 up votes in the following question says that it is probably the closest equivalent.
https://stackoverflow.com/questions/1377403/alternative-function-in-iostream-h-for-getch-of-conio-h#:~:text=For%20getch()%2C%20int%20ch,getch%20does%20an%20unbuffered%20read.]
But it still needs the enter key to be pressed after typing input at the input terminal.
The first answer of the following question seems to correctly simulate getch() equivalent in g++(gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12) ).
What is the equivalent to getch() & getche() in Linux?
#include<iostream>
using namespace std;
#include <termios.h>
#include <stdio.h>
static struct termios old, current;
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
current = old; /* make new settings same as old settings */
current.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
current.c_lflag |= ECHO; /* set echo mode */
} else {
current.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, &current); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* Read 1 character - echo defines echo mode */
char getch_(int echo)
{
char ch;
initTermios(echo);
ch = getchar();
resetTermios();
return ch;
}
/* Read 1 character without echo */
char getch(void)
{
return getch_(0);
}
/* Read 1 character with echo */
char getche(void)
{
return getch_(1);
}
/* Let's test it out */
int main(void) {
char c;
printf("(getche example) please type a letter: ");
c = getche();
printf("\nYou typed: %c\n", c);
printf("(getch example) please type a letter...");
c = getch();
printf("\nYou typed: %c\n", c);
return 0;
}
I have tested the above code. Touching any alphanumeric key completes execution of getch function but keystrokes of keys like CAPS_LOCK, SHIFT, CONTROL, ALT etc doesn't. Moreover, it forces us to use .h versions of header(stdio.h and termios.h), which I don't find commendable.
The following code
#include<iostream>
#include<ncurses.h>
using namespace std;
int main() {
cout<<"NIRBHAY KUMAR PANDEY"<<endl;
getch();
return 0;
}
compiles(g++ filename.cpp -lncurses) and runs successfully in my (Linux Mint 18.2 Cinnamon 64-bit, g++(gcc-5.4.0)) but gives following compilation errors in geeksforgeeks and hakerrank online compilers.
IN GEEKSFORGEEKS :
geeksforgeeks prog.cpp:2:20: fatal error: ncurses.h: No such file or directory.
IN HACKERRANK :
hakerrank /cc1ZsEbF.o: In function main': solution.cc:7: undefined reference to stdscr' solution.cc:7: undefined reference to `wgetch' collect2: error: ld returned 1 exit status Exit Status 255
Now my question is "are there any standard functions in standard headers in (Linux, g++) for detecting - KeyDown KeyPress and KeyUp events of all keys(including control keys and any key combination) of the keyboard - at the input terminal while waiting for input from the user ?
If yes, then a complete full running program will be appreciated.
(Note : I have achieved all this almost 12 years ago very easily by reading let us C 6th edition on turbo c/c++ compiler. I can recall that there were concept of ascii code and scan code. The book C++ primer 5th edition by Stanley B. Lippman has nothing to say anything about ascii code and scan code)

Activate windows "Alt Tab" switcher with SendInput

I need to show the windows switcher with SendInput. Another question I asked explains the reason of doing this. Shortly speaking, when I am holding Alt Tab to switch to other apps, my app may fire a key stroke using SendInput, which will interrupt current switcher, and this is why I need to refire a Alt Tab. Currently I am working on posting another tab key stroke (I am still holding alt when switching) or the entirely alt down + tab down & up. But with alt holding, a single tab stroke sent by SendInput will not trigger the switcher. And the entire combined key does not work neither. Here's some test code.
#include <windows.h>
#include <WinUser.h>
#include <iostream>
int main(void) {
Sleep(1000 * 3);
INPUT tabinput[2];
tabinput[0].type = INPUT_KEYBOARD;
tabinput[0].ki = {0x09, 0}; // KEY_TAB = 0x09
tabinput[1].type = INPUT_KEYBOARD;
tabinput[1].ki = {0x09, 0, KEYEVENTF_KEYUP};
SendInput(2, tabinput, sizeof(INPUT));
getchar();
}
I'm trying to fire a tab key stroke delayed 3s. I'am holding the alt key. This doesn't work. But the tab key is triggered, because When I run this code and switch to a text editor or something, there will be a tab event. My system is win8.1 64bit.
Windows 8 is blocking you.
In the Windows 8 security model, apps don’t have the privileges required to be a UI automation client. But you can write a desktop app that acts as an automation client with your app as the target. To do this, your desktop automation client app needs to be built with UIAccess permissions.
Change the manifest to UIAccess="true" and require administrator priviledge, created a certificate, sign the application with that certificate, put it in a location under Program Files, and run it. As explained here
https://msdn.microsoft.com/en-us/library/windows/desktop/dd979761.aspx?f=255&MSPPError=-2147217396
and here
https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4b6dbc43-a026-4957-9178-91d2001e2d0d/windows-8-block-alttab-simulation#291eb5b4-f6d2-49b6-83db-658bd832f2c9
plus this
https://msdn.microsoft.com/en-us/library/ms742884.aspx?f=255&MSPPError=-2147217396
and this
https://translate.google.com/translate?sl=it&tl=en&js=y&prev=_t&hl=en&ie=UTF-8&u=http%3A%2F%2Fblogs.msdn.com%2Fb%2Fitasupport%2Farchive%2F2009%2F09%2F16%2Fsendsas-step-by-step.aspx&edit-text=&act=url
Arrays are 0-indexed, but you are populating the array using 1-based indexes. So you are not populating the first array element, and are trashing memory after the last array element. Use this instead:
int main(void) {
Sleep(1000 * 3);
INPUT tabinput[2];
tabinput[0].type = INPUT_KEYBOARD;
tabinput[0].ki = {VK_TAB, 0};
tabinput[1].type = INPUT_KEYBOARD;
tabinput[1].ki = {VK_TAB, 0, KEYEVENTF_KEYUP};
SendInput(2, tabinput, sizeof(INPUT));
getchar();
}

SendInput not working for hold key C++

I have this bit of code which uses SendInput to send a key press but it doesn't work for when I want to long hold a key (eg long hold 'a' would return 'aaaaaaaaa' in notepad).
Now I have looked all over google and the only way I can see to get around this is to keep sending the input if I want a long hold. I don't want to do that as this will just simulate 'a' being pressed over and over again.
keyboard.wVk = 0;
keyboard.wScan = MapVirtualKey(key, 0);
keyboard.dwFlags = KEYEVENTF_SCANCODE;
if (index_vector_no)
pressed[index_vector_no] = true;
keyboard.dwExtraInfo = 0;
input.type = INPUT_KEYBOARD;
input.ki = keyboard;
SendInput(1, &input, sizeof (input));
So I would like some answers to the following questions:
A) Am I right in thinking there is no way around this using SendInput and why doesn't it work for long hold?
B) What is an alternative method for successfully being able to send key down and key up signals. Preferably sending the keys to windows and not just to a particular application.
C) Is there a good lightweight C++ library I can use that handles global keyboard and mouse simulation?
Thanks in advance! =)
EDIT: Take a look at this post for more details of my problem: http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_20833788.html
Repeating keystrokes is a feature of the keyboard controller, not of Windows or SendInput. You can certainly emulate it with a timer, repeatedly calling SendInput().