简体   繁体   中英

Easy way to work with keyboard input?

I'm making a simple program using the graphic mode in C++, I'm having trouble dealing with the input. I just need a way to check the keyboard and, if any key has been pressed, store the key value on a variable, the problem with getch() is that it waits untill any key has been pressed, and I need the programm to keep going if the user is not giving input. I've made a research and it seems that there are hundreds of ways of doing this. which one is the easiest?

Example: the loop begins, the program checks if there has been any input and stores it, the program works with that input and then discards it to begin the loop again. That would work perfectly with getch() isn't it? Well I want the loop to also keep going if no key has been pressed this time. A sort of no input default mode so the process doesn't stop.

(what I would really like is an equivalent to getch() that does'nt wait for user input).

Windows, Code::Blocks IDE, GCC compiler

yeah, kbhit seems to work as expected, Thanks!!

Use int _kbhit( void ); : it's a non-blocking call to getch() (works only on Windows). It's also included in conio.h

Doc here : http://msdn.microsoft.com/en-US/en-en/library/58w7c94c.aspx

(There is also a workaround for *nix systems : http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM