简体   繁体   English

使用键盘输入的简便方法?

[英]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. 我正在使用C ++中的图形模式制作一个简单的程序,我在处理输入时遇到了麻烦。 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. 我只需要一种方法来检查键盘,如果有任何键被按下,将键值存储在变量上, getch()的问题是它等待直到任何键被按下,我需要程序保持如果用户没有提供输入。 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? 那与getch()完全兼容,不是吗? 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). (我真正喜欢的是相当于getch() ,它不会等待用户输入)。

Windows, Code::Blocks IDE, GCC compiler Windows,Code :: Blocks IDE,GCC编译器

yeah, kbhit seems to work as expected, Thanks!! 是的,kbhit似乎按预期工作,谢谢!!

Use int _kbhit( void ); 使用int _kbhit( void ); : it's a non-blocking call to getch() (works only on Windows). :这是对getch()的非阻塞调用(仅适用于Windows)。 It's also included in conio.h 它也包含在conio.h

Doc here : http://msdn.microsoft.com/en-US/en-en/library/58w7c94c.aspx Doc在这里: 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 ) (* nix系统还有一个解决方法: http//cboard.cprogramming.com/c-programming/63166-kbhit-linux.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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