I'm using Windows XP Home Edition. I want to set up a menu system that displays an image when the Enter key is pressed. Is there any way I can set up an if statement that executes when the Enter key is pressed on the keyboard without setting up a GUI. For example, can I just use the hex value associated with Enter key as a trigger for the if statement.
I've looked into reading input buffers. Am I going to have to get that complex with it? I don't really
For windows there are a couple of variants you could use to implement this:
The standard way would be to call e.g. std::getline and discard the input.
A more Windows-specific solution is to use e.g. _kbhit and _getch to check for the Enter key.
Related
I'm currently working on a project using ncursor to handle animation and user input. The thing is that I want getchar() to only ask for user input when the keyboard is actually pressed. I did find a function which is called nodelay but it for some reason didn't actually delay getch().
My question is if there's a working alternative to this or if I might be using nodelay improperly. I've implemented nodelay into my code in this fashion:
nodelay(win, true);
while (inGame == true)
{
refresh();
printPlayGround();
wrefresh(win);
key = getchar();
setDirection(key);
moveSnake();
}
The thing is that it still asks for user input even if I don't press the keyboard. Could threads instead be the solution to my problem?
Ok so after looking into more what ncurser input really is i found wgetch(window variable). This is also probably why nodelay isn't working as i'm putting nodelay in one specific window but I'm using getch() on the "mainscreen". However while this does seemingly fix that issue, my code now only asks for user input once, while after that stopping to seemingly do it. But I've atleast come closer to getting it all working.
I'm trying to use getchar() to read characters one at a time from a windows console. However, when I hit tab it visually inserts the spacing of a tab in the console, rather than just registering to stdin as \t.
I want to hit tab in the console and not have anything change visually in a console, I want be able to have my code interpret tabs separately.
Is there any way to do this?
I know _getch() does this, it doesn't insert a tab in the console, but I can't use that function because I need signals to be able to be sent.
No, that's not possible because it's unrelated to your program and your code. Most terminals are line buffered (and so are Windows terminal and PowerShell). The terminal will send the input to your program after you pressed enter. Before you press enter your program won't know about any input. You need conio.h with getch or something similar. You can't achieve this behavior with standard library only.
Whenever I type some characters into the windows console and hit enter, it automatically scrolls to the next line. Is there any way to disable this behavior in C++ (using the Windows API), and if so; how?
If you call scanf or getline or similar then the underlying C runtime (CRT) handles Enter, Backspace, Delete, arrow keys, Tab, and such, and of course all printable keys.
If you want to handle Enter differently from CRT then you will have to handle every other key as well, using _getch (nonstandard function different from getchar). You will have to write some code. As far as I know there is no way to use scanf or getline, without Enter going to the next line.
I am writing a C++ CLI application how can I detect if any key is pressed by the user. I've seen that in c# but how can it be implement in c++
while(1)
{
while(/* code to check if any key is pressed*/)
{ //rest of the code
// sleep function
}
}
Hint: like in CLI games to move or to take certain action when a key is pressed or don't do any thing if no input is given.
On windows at least you could use GetKeyState
we can use _kbhit() function in c++. _kbhit is equal to 1 if any key is pressed. You have to clear the _kbhit buffer else it will remain 1. Method for clearing is character = getch(); This will save the last entered key in character which you can compare and decide which action to perform on which key.
While loop can be CPU consuming, i do not advice busy waiting method, instead you should think of event hooking.
Here you can read about winapi keystroke event hooking C++ Win32 keyboard events
If you are still interested to use the while loop, you should also free some resources by sleeping after checking that a condition is false (e.g. nanosleep )
The down arrow key of my laptop is very loose and it does not seems to last very long.
Is it possible to write any programm(in any language but especially C++) hat simulates the down arrow key.say I made a programm such that when I press A,B,C on the key board it simulates down arrow key.
If not then,
Is there any software available to do this?
Use the On-screen keyboard
If you want to simulate input, use the SendInput API. This injects input at a fairly low level, windows automatically routes it to the appropriate thread based on who has focus. Call it twice, once to send the key down, and again to send the key up.
Perhaps the easiest thing to do is to write a simple app that calls RegisterHotkey for some combination like ctrl-alt-Z, and then calls SendInput for a keypress then keyrelease of the down arrow key.
You might need to wait a short time after receiving WM_HOTKEY to give you time to release the set of hotkeys so that the down arrow gets processed alone without those modifiers from your hotkey interfering with it. (...otherwise the focused app might think you typed in shift+alt+downarrow instead of plain downarrow!)
if you're using linux, xmodmap: http://www.xfree86.org/4.2.0/xmodmap.1.html
I think that you are actually looking for Sharpkeys www.randyrants.com/sharpkeys/
This works with the windows registry and can be used to change mappings of keys.
You can easily write a program that sends WM_KEYDOWN and WM_KEYUP messages to the window which has the current focus. Once you have this program, bind it to a function key in the properties for the .exe file.