尝试读取键盘输入没有阻止

尝试读取键盘输入没有阻止

本文介绍了尝试读取键盘输入没有阻止(Windows,C + +)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个Windows控制台应用程序(使用g ++编译的C ++),它将在循环中执行一系列指令,直到完成OR,直到按下ctrl-z(或其他按键)。我目前使用的代码不能工作(否则我不会问,对吗?):

I'm trying to write a Windows console application (in C++ compiled using g++) that will execute a series of instructions in a loop until finished OR until ctrl-z (or some other keystroke) is pressed. The code I'm currently using to catch it isn't working (otherwise I wouldn't be asking, right?):

if(kbhit() && getc(stdin) == 26)
  //The code to execute when ctrl-z is pressed

如果我按一个键,它会回显,应用程序会等待,直到我按Enter继续打开。使用值26,它不会执行预期的代码。如果我使用像65这样的值来捕获,它将重新路由执行如果我按A然后输入后。

If I press a key, it is echoed and the application waits until I press Enter to continue on at all. With the value 26, it doesn't execute the intended code. If I use something like 65 for the value to catch, it will reroute execution if I press A then Enter afterward.

有一种方式被动检查输入,抛出它出来,如果它不是我正在寻找或适当的反应,当它是我正在寻找什么?

Is there a way to passively check for input, throwing it out if it's not what I'm looking for or properly reacting when it is what I'm looking for? ..and without having to press Enter afterward?

推荐答案

尝试以避免熟食模式,以避免阻止。

Try ReadConsoleInput to avoid cooked mode, and GetNumberOfConsoleInputEvents to avoid blocking.

这篇关于尝试读取键盘输入没有阻止(Windows,C + +)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:55