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.
Related
I'm new to programming, and I try to program a menu in C++.
I want it to be in a while loop, breaking when ESC is pressed, but I want the character to be read instantly without having to press Enter.
while (breaker != 27)
{
//menu based on switch(breaker)
}
I found the getch() function and <conio.h> header, but people say not to use it because it works only on Windows.
Is there any other method that I can get a character without pressing Enter, and it's multiplatform/meets coding ethic?
My operating system is Windows 11, but I would like to know the solution that works on other systems.
It is generally not possible to use the functions provided by the ISO C++ standard library to detect whether the ESC key has been pressed. However, most platforms provide a platform-specific API which does provide this functionality.
On Microsoft Windows, you can use the functions _getch, _getche or ReadConsoleInput.
On Linux, you can use the ncurses library.
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.
I'm trying to write a Keyboard class that can read in the keyboard buttons. I have looked at this link - http://www.daniweb.com/software-development/cpp/code/216732/reading-scan-codes-from-the-keyboard But as stated on there, it is not very accurate for all computers (I don't know if this is even true). Therefore, my question is whats the best method in implementing my keyboard class? This will be used for Windows
Many thanks
There are three ways to read keyboard input:
By reading input from a console window as described in your link. It's true that it's hard to get this to work correctly, for starters because it's reading ANSI characters and not Unicode characters, but there are other issues. Console input/output is kind of obscure, as is the documentation for it
By handling UI events associated with a normal window. In this case you would handle the WM_KEYDOWN message in a window procedure
By going deep into the Win32 API with functions like SetWindowsHookEx. In this case you don't even need a window (normal or console), and you can read keystrokes pressed in any application or in the desktop
It's hard to suggest which one to use without knowing how you intend to use this Keyboard class.
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.
how would i read keystrokes using the win32 api? i would also like to see them from international keyboards like german umlauts.
thanks
There's a difference between keyboard presses and the characters they generate.
At the lowest level, you can poll the keyboard state with GetKeyboardState. That's often how keylogging malware does it, since it requires the least privileges and sees everything regardless of where the focus is. The problem with this approach (besides requiring constant polling) is that you have to piece together the keyboard state into keystrokes and then keystrokes into a character stream. You have to know how the keyboard is mapped, you have keep state of shift keys, control keys, alt keys, etc. You have to know about auto-repeat, dead keys, and possibly other complications.
If you have privileges you can install a keyboard hook, as Jens mentioned in his answer.
If you have focus, and you're a console app, you use one of the functions to read from standard input. On Windows, it's hard to get true Unicode input. You generally get so-called ANSI characters, which correspond to the current code page for the console window. If you know the code page, you can use MultiByteToWideChar to convert the single- or multi-byte input into UTF-16 (which Windows documentation calls Unicode). From there you can convert it to UTF-8 (with WideCharToMultiByte) or whatever other Unicode encoding you want.
If you have focus, and you're a GUI app, you can see keystrokes with WM_KEYDOWN (and friends). You can can also get fully resolved UTF-16 characters with WM_CHAR (or UTF-32 from WM_UNICHAR). If you need UTF-8 from those, you'll have to do a conversion.
To get keyboard input regardless of focus, you'll probably need to hook the keyboard.
Take a look at SetWindowsHookEx with WH_KEYBOARD or WH_KEYBOARD_LL. Add a W to the call for the Unicode variant.