等效于 Mac/Linux 崩溃的 Windows getch() [英] Equivalent to Windows getch() for Mac/Linux crashes

查看:14
本文介绍了等效于 Mac/Linux 崩溃的 Windows getch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 getch() 并且我的应用程序立即崩溃.包括在做的时候:

I am using getch() and my app crashes instantly. Including when doing:

int main()
{
    getch();
}

我找不到链接,但据推测问题是它需要关闭缓冲或类似的奇怪的东西,我仍然希望 cout 与跨平台代码一起工作.

I can't find the link but supposedly the problem is that it needs to turn off buffering or something strange along those lines, and I still want cout to work along with cross platform code.

有人告诉我使用 std::cin.get(),但我希望应用程序在按下某个键时退出,而不是在用户输入字母或数字时退出按回车键退出.

I was told to use std::cin.get(), but I'd like the app to quit when a key is pressed, not when the user typed in a letter or number then press enter to quit.

这有什么功能吗?代码必须在 Mac(我的操作系统)和 Windows 下工作.

Is there any function for this? The code must work under Mac (my os) and Windows.

链接/编译不是问题;我在 XCode 中包含 <curses.h> 并与 -lcurses 链接,而 Windows 使用 <conio.h>.

Linking/compiling is not an issue; I include <curses.h> and link with -lcurses in XCode, while Windows uses <conio.h>.

推荐答案

你有没有看过 看看 getch() 函数做了什么?

Have you looked in <curses.h> to see what the getch() function does?

提示:OSX 和 Linux 与 Windows 不同.

Hint: OSX and Linux are not the same as Windows.

具体来说,作为<curses.h>中的宏,我们发现:

Specifically, as a macro in <curses.h>, we find:

#define getch() wgetch(stdscr)

现在,在您的系统上,curses 库中出现了一个实际的函数 getch(),但它希望设置 stdscr,并且由 curses 初始化函数(initscr() 和相关函数)完成,而这显然不是由您的代码完成的.因此,您的代码通过在正确初始化完成之前调用 curses 例程来调用未定义的行为,从而导致崩溃.

Now, there appears, on your system, to be an actual function getch() in the curses library, but it expects stdscr to be set up, and that is done by the curses initialization functions (initscr() and relatives), and that is signally not done by your code. So, your code is invoking undefined behaviour by calling curses routines before the correct initialization is done, leading to the crash.

(来自 dmckee 的好提示 - 它有助于将链接线从 acidzombie24 中取出,这很重要.)

(Good hint from dmckee - it helped get the link line out of acidzombie24, which was important.)

要达到可以读取单个按键并干净地终止程序的程度,您必须在 Unix(OSX、Linux)上做大量工作.您必须捕获终端的初始状态,安排一个 atexit() 函数 - 或一些类似的机制 - 来恢复终端的状态,将终端从熟模式更改为原始模式,然后调用一个函数来读取一个字符(可能只是 read(0, &c, 1)),然后退出.可能还有其他方法可以做到这一点 - 但它肯定会涉及一些设置和拆卸操作.

To get to a point where a single key-stroke can be read and the program terminated cleanly, you have to do a good deal of work on Unix (OSX, Linux). You would have to trap the initial state of the terminal, arrange for an atexit() function - or some similar mechanism - to restore the state of the terminal, change the terminal from cooked mode into raw mode, then invoke a function to read a character (possibly just read(0, &c, 1)), and do your exit. There might be other ways to do it - but it certainly will involve some setup and teardown operations.

可能有帮助的一本书是 高级 Unix 编程,第二马克·罗奇金德的 Edn;它涵盖了所需级别的终端处理.或者,您可以正确使用 <curses.h> - 这比自己动手解决方案更简单,而且可能更可靠.

One book that might help is Advanced Unix Programming, 2nd Edn by Mark Rochkind; it covers terminal handling at the level needed. Alternatively, you can use <curses.h> properly - that will be simpler than a roll-your-own solution, and probably more reliable.

这篇关于等效于 Mac/Linux 崩溃的 Windows getch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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