在C ++中捕获击键 [英] Capturing a keystroke in C++

查看:152
本文介绍了在C ++中捕获击键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些阅读,我看到我可以使用getch()获得一个击键。我所看到的是,这被认为是坏的做法,但我看到相互冲突的意见。我为我的类写一个控制台应用程序,并希望能够基于按下的箭头键在屏幕上移动标记(*)。是getch()正确的方法去做这个,或有一个更好的方法来捕获它。我希望他们只是能够推箭头,不需要推输入或任何东西。我不需要具体的代码,我只是想知道如果我应该避免getch(),如果是,这种类型的想法有什么功能。

I have been doing some reading, and I see that I can use getch() to get a keystroke. What I have seen is that this is considered bad practice, however I have seen conflicting opinions. I am writing a console application for my class, and would like to be able to move a marker(*) around the screen based on the arrow keys being pressed. Is getch() the right way to go about this, or is there a better method to capture it. I want them to just be able to push the arrow, not need to push enter or anything. I don't need the code specifically, I just want to know if I should avoid getch(), and if so, what functions are there for this type of idea.

推荐答案

getch()不是C或C ++中的标准函数。它存在于某些过时的编译器中,例如 Turbo C ,并且在某些常用库中定义作为 curses ,但在任何情况下它是一个C函数,而不是C ++。对于C ++,你应该坚持使用标准的C ++ I / O。如果你不能这样做的某种原因,然后去最便携的选项,例如。诅咒。

getch() is not a standard function in either C or C++. It's found in some obsolete compilers, such as Turbo C and it's also defined in certain commonly used libraries such as curses, but in any case it's a C function, not C++. For C++ you should probably just stick with standard C++ I/O. If you can't do this for some reason then go for the most portable option, e.g. curses.

这篇关于在C ++中捕获击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆