C++ cin keypress event

后端 未结 4 1666
天命终不由人
天命终不由人 2021-01-05 16:47

I believe this is a very simple question, but I can\'t find a simple answer to it. I have an infinite loop, e.g. while(1), for(;;), and I need to b

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-05 17:26

    There is no "keyboard" in C++. You only have an opaque input data stream which your terminal popu­lates occasionally with its own keyboard input. This is almost always a buffered, editable line-wise input, so you have no way of knowing when any given key was pressed.

    You need a platform-specific method to communicate directly with your terminal on a lower level. One such library, fairly wide-spread and portable, is ncurses (a compatible Windows variant exists). Portable graphical application frameworks such as SDL and Allegro also provide raw keyboard handling.

提交回复
热议问题