我从来都不记得我是怎么做到的,因为它很少出现在我身上。但是在C或c++中,从标准输入中读取字符而不等待换行符(按enter)的最佳方法是什么?
理想情况下,它也不会将输入字符回显到屏幕上。我只是想在不影响控制台屏幕的情况下捕捉击键。
我从来都不记得我是怎么做到的,因为它很少出现在我身上。但是在C或c++中,从标准输入中读取字符而不等待换行符(按enter)的最佳方法是什么?
理想情况下,它也不会将输入字符回显到屏幕上。我只是想在不影响控制台屏幕的情况下捕捉击键。
这在纯c++中以可移植的方式是不可能的,因为它太依赖于可能与stdin连接的终端(它们通常是行缓冲的)。不过,你可以使用一个库来实现:
conio available with Windows compilers. Use the _getch() function to give you a character without waiting for the Enter key. I'm not a frequent Windows developer, but I've seen my classmates just include <conio.h> and use it. See conio.h at Wikipedia. It lists getch(), which is declared deprecated in Visual C++. curses available for Linux. Compatible curses implementations are available for Windows too. It has also a getch() function. (try man getch to view its manpage). See Curses at Wikipedia.
如果你的目标是跨平台兼容性,我建议你使用curses。也就是说,我相信有一些函数可以用来关闭行缓冲(我相信这被称为“原始模式”,而不是“熟模式”-看看man stty)。如果我没记错的话,诅咒可以方便地帮你解决这个问题。
C和c++对I/O有一个非常抽象的看法,没有标准的方法来做你想做的事情。有从标准输入流中获取字符的标准方法(如果有的话),这两种语言都没有定义其他任何方法。因此,任何答案都必须是特定于平台的,可能不仅取决于操作系统,还取决于软件框架。
这里有一些合理的猜测,但如果不知道目标环境是什么,就无法回答您的问题。
CONIO.H
你需要的函数是:
int getch();
Prototype
int _getch(void);
Description
_getch obtains a character from stdin. Input is unbuffered, and this
routine will return as soon as a character is available without
waiting for a carriage return. The character is not echoed to stdout.
_getch bypasses the normal buffering done by getchar and getc. ungetc
cannot be used with _getch.
Synonym
Function: getch
int kbhit();
Description
Checks if a keyboard key has been pressed but not yet read.
Return Value
Returns a non-zero value if a key was pressed. Otherwise, returns 0.
libconio http://sourceforge.net/projects/libconio
or
conio.h的Linux c++实现 http://sourceforge.net/projects/linux-conioh
最接近可移植的方法是使用ncurses库将终端置于“cbreak模式”。API是巨大的;你最想要的是
Initscr和endwin 黄昏时分和黄昏时分 getch
好运!
如果你在windows上,你可以使用PeekConsoleInput来检测是否有任何输入,
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
DWORD events;
INPUT_RECORD buffer;
PeekConsoleInput( handle, &buffer, 1, &events );
然后使用ReadConsoleInput“消费”输入字符..
PeekConsoleInput(handle, &buffer, 1, &events);
if(events > 0)
{
ReadConsoleInput(handle, &buffer, 1, &events);
return buffer.Event.KeyEvent.wVirtualKeyCode;
}
else return 0
老实说,这是从一些旧的代码,我有,所以你必须拨弄它一点。
最酷的是它读取输入时没有任何提示,所以字符根本不会显示出来。
以下是一个从Expert C Programming: Deep Secrets中提取的解决方案,它应该可以在SVr4上工作。它使用stty和ioctl。
#include <sys/filio.h>
int kbhit()
{
int i;
ioctl(0, FIONREAD, &i);
return i; /* return a count of chars available to read */
}
main()
{
int i = 0;
intc='';
system("stty raw -echo");
printf("enter 'q' to quit \n");
for (;c!='q';i++) {
if (kbhit()) {
c=getchar();
printf("\n got %c, on iteration %d",c, i);
}
}
system("stty cooked echo");
}
我在另一个论坛上发现了这个,当时我想解决同样的问题。根据我的发现,我做了一些修改。效果很好。我运行的是OS X,所以如果你运行的是微软,你需要找到正确的system()命令来切换到raw和cooked模式。
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
// Output prompt
cout << "Press any key to continue..." << endl;
// Set terminal to raw mode
system("stty raw");
// Wait for single character
char input = getchar();
// Echo input:
cout << "--" << input << "--";
// Reset terminal to normal "cooked" mode
system("stty cooked");
// And we're out of here
return 0;
}
在Linux(和其他类unix系统)上,这可以通过以下方式完成:
#include <unistd.h>
#include <termios.h>
char getch() {
char buf = 0;
struct termios old = {0};
if (tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if (tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if (read(0, &buf, 1) < 0)
perror ("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if (tcsetattr(0, TCSADRAIN, &old) < 0)
perror ("tcsetattr ~ICANON");
return (buf);
}
基本上你必须关闭规范模式(和回声模式来抑制回声)。
#include <conio.h>
if (kbhit() != 0) {
cout << getch() << endl;
}
它使用kbhit()来检查键盘是否被按下,并使用getch()来获取被按下的字符。
You can do it portably using SDL (the Simple DirectMedia Library), though I suspect you may not like its behavior. When I tried it, I had to have SDL create a new video window (even though I didn't need it for my program) and have this window "grab" almost all keyboard and mouse input (which was okay for my usage but could be annoying or unworkable in other situations). I suspect it's overkill and not worth it unless complete portability is a must--otherwise try one of the other suggested solutions.
顺便说一下,这将分别提供按键和释放事件,如果你喜欢的话。
我总是想要一个循环读取我的输入而不按返回键。 这对我很管用。
#include<stdio.h>
main()
{
char ch;
system("stty raw");//seting the terminal in raw mode
while(1)
{
ch=getchar();
if(ch=='~'){ //terminate or come out of raw mode on "~" pressed
system("stty cooked");
//while(1);//you may still run the code
exit(0); //or terminate
}
printf("you pressed %c\n ",ch); //write rest code here
}
}
我使用kbhit()来查看是否存在一个字符,然后使用getchar()来读取数据。 在windows上,您可以使用“conio.h”。在linux上,您必须实现自己的kbhit()。
参见下面的代码:
// kbhit
#include <stdio.h>
#include <sys/ioctl.h> // For FIONREAD
#include <termios.h>
#include <stdbool.h>
int kbhit(void) {
static bool initflag = false;
static const int STDIN = 0;
if (!initflag) {
// Use termios to turn off line buffering
struct termios term;
tcgetattr(STDIN, &term);
term.c_lflag &= ~ICANON;
tcsetattr(STDIN, TCSANOW, &term);
setbuf(stdin, NULL);
initflag = true;
}
int nbbytes;
ioctl(STDIN, FIONREAD, &nbbytes); // 0 is STDIN
return nbbytes;
}
// main
#include <unistd.h>
int main(int argc, char** argv) {
char c;
//setbuf(stdout, NULL); // Optional: No buffering.
//setbuf(stdin, NULL); // Optional: No buffering.
printf("Press key");
while (!kbhit()) {
printf(".");
fflush(stdout);
sleep(1);
}
c = getchar();
printf("\nChar received:%c\n", c);
printf("Done.\n");
return 0;
}
Ncurses提供了一个很好的方法来做到这一点! 这也是我的第一篇文章(我记得),所以欢迎任何评论。我会欣赏有用的,但都是受欢迎的!
编译: g++ -std=c++11 -pthread -lncurses .cpp -o
#include <iostream>
#include <ncurses.h>
#include <future>
char get_keyboard_input();
int main(int argc, char *argv[])
{
initscr();
raw();
noecho();
keypad(stdscr,true);
auto f = std::async(std::launch::async, get_keyboard_input);
while (f.wait_for(std::chrono::milliseconds(20)) != std::future_status::ready)
{
// do some work
}
endwin();
std::cout << "returned: " << f.get() << std::endl;
return 0;
}
char get_keyboard_input()
{
char input = '0';
while(input != 'q')
{
input = getch();
}
return input;
}
下面是一个不向系统提供外壳的版本(在macOS 10.14上编写和测试)
#include <unistd.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
char* getStr( char* buffer , int maxRead ) {
int numRead = 0;
char ch;
struct termios old = {0};
struct termios new = {0};
if( tcgetattr( 0 , &old ) < 0 ) perror( "tcgetattr() old settings" );
if( tcgetattr( 0 , &new ) < 0 ) perror( "tcgetaart() new settings" );
cfmakeraw( &new );
if( tcsetattr( 0 , TCSADRAIN , &new ) < 0 ) perror( "tcssetattr makeraw new" );
for( int i = 0 ; i < maxRead ; i++) {
ch = getchar();
switch( ch ) {
case EOF:
case '\n':
case '\r':
goto exit_getStr;
break;
default:
printf( "%1c" , ch );
buffer[ numRead++ ] = ch;
if( numRead >= maxRead ) {
goto exit_getStr;
}
break;
}
}
exit_getStr:
if( tcsetattr( 0 , TCSADRAIN , &old) < 0) perror ("tcsetattr reset to old" );
printf( "\n" );
return buffer;
}
int main( void )
{
const int maxChars = 20;
char stringBuffer[ maxChars+1 ];
memset( stringBuffer , 0 , maxChars+1 ); // initialize to 0
printf( "enter a string: ");
getStr( stringBuffer , maxChars );
printf( "you entered: [%s]\n" , stringBuffer );
}
由于这里之前的解决方案不能跨平台工作,并且在特殊键上存在问题,所以这里是我的解决方案,它在Windows和Linux上都可以工作,并且使用了最少的外部库(Windows.h和Linux的sys/ioctl.h+termios.h)。
对于ASCII字符(换行符/tab/space/backspace/delete, !"#$%&'()*+,-./0-9:;<=>?@A-Z[]^_ ' a-z{|}~üäÄöÖÜßµ´§°¹³),返回ASCII码(正数);对于特殊键(方向键、上页/下页、pos1/end、转义、插入、F1-F12),返回Windows虚拟键码(负数)的负数。
#include <iostream>
#include <string>
#include <thread> // contains <chrono>
using namespace std;
void println(const string& s="") {
cout << s << endl;
}
void sleep(const double t) {
if(t>0.0) this_thread::sleep_for(chrono::milliseconds((int)(1E3*t+0.5)));
}
// ASCII codes (key>0): 8 backspace, 9 tab, 10 newline, 27 escape, 127 delete, !"#$%&'()*+,-./0-9:;<=>?@A-Z[]^_`a-z{|}~üäÄöÖÜßµ´§°¹³²
// control key codes (key<0): -38/-40/-37/-39 up/down/left/right arrow, -33/-34 page up/down, -36/-35 pos1/end
// other key codes (key<0): -45 insert, -144 num lock, -20 caps lock, -91 windows key, -93 kontext menu key, -112 to -123 F1 to F12
// not working: ¹ (251), num lock (-144), caps lock (-20), windows key (-91), kontext menu key (-93), F11 (-122)
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
int key_press() { // not working: F11 (-122, toggles fullscreen)
KEY_EVENT_RECORD keyevent;
INPUT_RECORD irec;
DWORD events;
while(true) {
ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE), &irec, 1, &events);
if(irec.EventType==KEY_EVENT&&((KEY_EVENT_RECORD&)irec.Event).bKeyDown) {
keyevent = (KEY_EVENT_RECORD&)irec.Event;
const int ca = (int)keyevent.uChar.AsciiChar;
const int cv = (int)keyevent.wVirtualKeyCode;
const int key = ca==0 ? -cv : ca+(ca>0?0:256);
switch(key) {
case -16: continue; // disable Shift
case -17: continue; // disable Ctrl / AltGr
case -18: continue; // disable Alt / AltGr
case -220: continue; // disable first detection of "^" key (not "^" symbol)
case -221: continue; // disable first detection of "`" key (not "`" symbol)
case -191: continue; // disable AltGr + "#"
case -52: continue; // disable AltGr + "4"
case -53: continue; // disable AltGr + "5"
case -54: continue; // disable AltGr + "6"
case -12: continue; // disable num block 5 with num lock deactivated
case 13: return 10; // enter
case -46: return 127; // delete
case -49: return 251; // ¹
case 0: continue;
case 1: continue; // disable Ctrl + a (selects all text)
case 2: continue; // disable Ctrl + b
case 3: continue; // disable Ctrl + c (terminates program)
case 4: continue; // disable Ctrl + d
case 5: continue; // disable Ctrl + e
case 6: continue; // disable Ctrl + f (opens search)
case 7: continue; // disable Ctrl + g
//case 8: continue; // disable Ctrl + h (ascii for backspace)
//case 9: continue; // disable Ctrl + i (ascii for tab)
case 10: continue; // disable Ctrl + j
case 11: continue; // disable Ctrl + k
case 12: continue; // disable Ctrl + l
//case 13: continue; // disable Ctrl + m (breaks console, ascii for new line)
case 14: continue; // disable Ctrl + n
case 15: continue; // disable Ctrl + o
case 16: continue; // disable Ctrl + p
case 17: continue; // disable Ctrl + q
case 18: continue; // disable Ctrl + r
case 19: continue; // disable Ctrl + s
case 20: continue; // disable Ctrl + t
case 21: continue; // disable Ctrl + u
case 22: continue; // disable Ctrl + v (inserts clipboard)
case 23: continue; // disable Ctrl + w
case 24: continue; // disable Ctrl + x
case 25: continue; // disable Ctrl + y
case 26: continue; // disable Ctrl + z
default: return key; // any other ASCII/virtual character
}
}
}
}
#elif defined(__linux__)
#include <sys/ioctl.h>
#include <termios.h>
int key_press() { // not working: ¹ (251), num lock (-144), caps lock (-20), windows key (-91), kontext menu key (-93)
struct termios term;
tcgetattr(0, &term);
while(true) {
term.c_lflag &= ~(ICANON|ECHO); // turn off line buffering and echoing
tcsetattr(0, TCSANOW, &term);
int nbbytes;
ioctl(0, FIONREAD, &nbbytes); // 0 is STDIN
while(!nbbytes) {
sleep(0.01);
fflush(stdout);
ioctl(0, FIONREAD, &nbbytes); // 0 is STDIN
}
int key = (int)getchar();
if(key==27||key==194||key==195) { // escape, 194/195 is escape for °ß´äöüÄÖÜ
key = (int)getchar();
if(key==91) { // [ following escape
key = (int)getchar(); // get code of next char after \e[
if(key==49) { // F5-F8
key = 62+(int)getchar(); // 53, 55-57
if(key==115) key++; // F5 code is too low by 1
getchar(); // take in following ~ (126), but discard code
} else if(key==50) { // insert or F9-F12
key = (int)getchar();
if(key==126) { // insert
key = 45;
} else { // F9-F12
key += 71; // 48, 49, 51, 52
if(key<121) key++; // F11 and F12 are too low by 1
getchar(); // take in following ~ (126), but discard code
}
} else if(key==51||key==53||key==54) { // delete, page up/down
getchar(); // take in following ~ (126), but discard code
}
} else if(key==79) { // F1-F4
key = 32+(int)getchar(); // 80-83
}
key = -key; // use negative numbers for escaped keys
}
term.c_lflag |= (ICANON|ECHO); // turn on line buffering and echoing
tcsetattr(0, TCSANOW, &term);
switch(key) {
case 127: return 8; // backspace
case -27: return 27; // escape
case -51: return 127; // delete
case -164: return 132; // ä
case -182: return 148; // ö
case -188: return 129; // ü
case -132: return 142; // Ä
case -150: return 153; // Ö
case -156: return 154; // Ü
case -159: return 225; // ß
case -181: return 230; // µ
case -167: return 245; // §
case -176: return 248; // °
case -178: return 253; // ²
case -179: return 252; // ³
case -180: return 239; // ´
case -65: return -38; // up arrow
case -66: return -40; // down arrow
case -68: return -37; // left arrow
case -67: return -39; // right arrow
case -53: return -33; // page up
case -54: return -34; // page down
case -72: return -36; // pos1
case -70: return -35; // end
case 0: continue;
case 1: continue; // disable Ctrl + a
case 2: continue; // disable Ctrl + b
case 3: continue; // disable Ctrl + c (terminates program)
case 4: continue; // disable Ctrl + d
case 5: continue; // disable Ctrl + e
case 6: continue; // disable Ctrl + f
case 7: continue; // disable Ctrl + g
case 8: continue; // disable Ctrl + h
//case 9: continue; // disable Ctrl + i (ascii for tab)
//case 10: continue; // disable Ctrl + j (ascii for new line)
case 11: continue; // disable Ctrl + k
case 12: continue; // disable Ctrl + l
case 13: continue; // disable Ctrl + m
case 14: continue; // disable Ctrl + n
case 15: continue; // disable Ctrl + o
case 16: continue; // disable Ctrl + p
case 17: continue; // disable Ctrl + q
case 18: continue; // disable Ctrl + r
case 19: continue; // disable Ctrl + s
case 20: continue; // disable Ctrl + t
case 21: continue; // disable Ctrl + u
case 22: continue; // disable Ctrl + v
case 23: continue; // disable Ctrl + w
case 24: continue; // disable Ctrl + x
case 25: continue; // disable Ctrl + y
case 26: continue; // disable Ctrl + z (terminates program)
default: return key; // any other ASCII character
}
}
}
#endif // Windows/Linux
最后,这里有一个如何使用它的例子:
int main() {
while(true) {
const int key = key_press(); // blocks until a key is pressed
println("Input is: "+to_string(key)+", \""+(char)key+"\"");
}
return 0;
}
ssinfod对Linux的回答的变体,对我的口味来说稍微干净一些,为wcout和wchar_t实现,并在没有错误的情况下擦除无效字符。
#include <functional>
//For Linux kbhit(). For Windows, use conio.h.
#ifdef __unix__
#include <sys/ioctl.h> //For FIONREAD.
#include <termios.h>
//Call this at program start to setup for kbhit.
void initTerminalInput()
{
//Disable internal buffering.
std::wcout << std::unitbuf;
//Turn off line buffering.
struct termios term;
tcgetattr(0, &term);
term.c_lflag &= ~ICANON;
tcsetattr(0, TCSANOW, &term);
setbuf(stdin, NULL);
}
//Returns 0 if there's no input character to read.
int kbhit()
{
static int nbbytes;
ioctl(0, FIONREAD, &nbbytes);
return nbbytes;
}
#endif
//Waits for and retrieves a single validated character, calling a validation function on each character entered and
//erasing any that are invalid (when the validation function returns false).
static wchar_t getWChar(std::function<bool(wchar_t)> validationFunction)
{
static wchar_t inputWChar;
do
{
//Wait until there's an input character.
while (!kbhit())
{
}
inputWChar = getwchar();
//Validate the input character.
if (validationFunction(inputWChar))
{
//Valid.
break;
}
else
{
//Erase the invalid character.
std::wcout << L"\b \b";
}
} while (true);
return inputWChar;
}
在下面的例子中,我想让用户输入1、2或3。输入的任何其他字符都不会显示,它将等待,直到按下其中一个有效字符:
int main()
{
#ifdef __unix__
initTerminalInput();
#endif
getWChar([] (wchar_t inputWChar)
{
return (inputWChar >= L'1' && inputWChar <= L'3');
});
return 0;
}
在WINDOWS上我这样做:
#include <Windows.h>
int main()
{
system("PAUSE>NUL");
if (GetKeyState(0x41/*(the 'A' key, choosen e.g.)*/) & (0xff80/*That stands for "Default state / Key Down"*/)) {
//whatever I want to do after 'A' is pressed
}
}
这里可以找到一个键的列表,每个键都有自己的十六进制值: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
管道演示:如何从C语言的系统调用管道读取键盘按键
理想情况下,它也不会将输入字符回显到屏幕上。我只是想在不影响控制台屏幕的情况下捕捉击键。
要在Linux上做到这一点,您可以使用以下bash命令:
read -sn1 c && printf "You Pressed: %s\n" "$c"
请看我在这里的回答关于这个的详细信息:shell脚本响应按键。
因此,要在Linux上的C或c++中做到这一点,您只需要通过使用popen()和fgets()的管道通过系统调用调用上面的bash命令,这样您就可以从bash命令读取输出。
下面是一个完整的例子,在Linux上运行良好的C和c++:
read_system_call_via_pipe__keypress.c:
#include <stdbool.h> // For `true` (`1`) and `false` (`0`) macros in C
#include <stdint.h> // For `uint8_t`, `int8_t`, etc.
#include <stdio.h> // For `printf()`
#include <stdlib.h>
#define BUFSIZE 32
// Read a keyboard key press and return the character pressed, or a negative
// number in the event of an error.
// NB: for help reading output from system calls, see here:
// 1. https://stackoverflow.com/a/28971647/4561887
// 2. https://stackoverflow.com/a/18297075/4561887
char getKeypress()
{
// This bash cmd is from my answer here:
// https://stackoverflow.com/a/70979348/4561887
const char* cmd = "bash -c 'read -s -n1 c && printf \"%s\" \"$c\"'";
FILE *fp = popen(cmd, "r");
if (fp == NULL)
{
printf("\nError opening pipe!\n");
return -1;
}
char buf[BUFSIZE] = {0};
char* retval1 = fgets(buf, BUFSIZE, fp);
if (retval1 == NULL)
{
printf("\nFailed to read cmd response.\n");
return -2;
}
// See meaning of this return value here:
// https://stackoverflow.com/questions/43116/how-can-i-run-an-external-program-from-c-and-parse-its-output/28971647#comment60311936_28971647
int retval2 = pclose(fp);
if (retval2 == -1)
{
printf("\nError obtaining the cmd's exit status code.\n");
return -3;
}
else if (retval2 != 0)
{
printf("\nCommand exited with exit status code %i.\n", retval2);
return -4;
}
char keyPressed = buf[0];
return keyPressed;
}
// int main(int argc, char *argv[]) // alternative prototype
int main()
{
printf("Press any key to continue: ");
fflush(stdout);
char keyPressed = getKeypress();
if (keyPressed > 0)
{
printf("\nKey pressed = %c\n", keyPressed);
}
return 0;
}
C和c++编译和运行命令是下面输出的一部分。下面是一些演示:
在C:
eRCaGuy_hello_world/c$ gcc -Wall -Wextra -Werror -O3 -std=gnu17 read_keypress_system_call.c -o bin/a && bin/a
Press any key to continue:
Key pressed = P
或,在c++中:
eRCaGuy_hello_world/c$ g++ -Wall -Wextra -Werror -O3 -std=c++17 read_keypress_system_call.c -o bin/a && bin/a
Press any key to continue:
Key pressed = u
参见:
要更进一步,请参阅我在这里的另一个回答,我一次检测并解析3个字符,以便检测方向键的向上、向下、向左或向右:在C语言中读取键按下。方向键,Enter键
引用:
How I learned to read from a pipe to get system call output: How can I run an external program from C and parse its output? Is there a way to obtain the output of a linux command(like ifconfig) on a .txt file using a C program? [duplicate] How to compile and use popen() in C: use -std=gnu17 instead of -std=c17: popen implicitly declared even though #include <stdio.h> is added [my answer] How to read without blocking, via bash: shell script respond to keypress [my answer] How do I read in the Enter key as an input in C?
关于这个话题的3个问题
从标准输入中捕获字符,而不需要等待按enter键 C非阻塞键盘输入 如何避免按Enter与getchar()只为读取单个字符?
我也遇到过同样的问题。这里是一个小的解决方案为windows控制台使用cygwing++与if(GetKeyState(keycode) & bitANDcompare){};
#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;
void clear() {
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;
GetConsoleScreenBufferInfo(console, &screen);
FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,
screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
SetConsoleCursorPosition(console, topLeft);
}
class Keyclick{
private:
int key;
char id;
public:
bool keydown = false;
Keyclick(int key1, char id1){
key=key1;
id=id1;
};
void watch(){
if(keydown==false){
if(GetKeyState(key) & 0x8000 ){
cout << id;
cout << " pressed.\r\n";
keydown = true;
}
}
if(keydown == true){
if(!(GetKeyState(key) & 0x8000)) {
cout << "released!!!!!!!!!!\r\n";
keydown = false;
clear();
}
}
};
};
int main()
{
bool primaryloop =true;
Keyclick keysp(VK_SPACE,'S');
Keyclick keyw(0x57,'w');
Keyclick keya(0x41,'a');
Keyclick keys(0x53,'s');
Keyclick keyd(0x44,'d');
Keyclick keyesc(VK_ESCAPE,'E');
while(primaryloop){
keysp.watch();
keyw.watch();
keya.watch();
keys.watch();
keyd.watch();
keyesc.watch();
if(keyesc.keydown){
primaryloop=false;
};
}
return 0;
}
https://github.com/wark77/windows_console_keypoller/blob/main/getkeystate_SOLUTION01.cpp