如何在D2(Phobos)中获得单个击键? [英] How to get single keystroke in D2 (Phobos)?

查看:116
本文介绍了如何在D2(Phobos)中获得单个击键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的跨平台方法可以使用Phobos在D2中获得单个击键?

Is there a simple, cross-platform way to get a single keystroke in D2 using Phobos?

例如,出现按任意键继续..."提示或Brainfuck解释器.

For instance, a "Press any key to continue..." prompt, or a Brainfuck interpreter.

我尝试过的所有方法在传递输入(例如,getchar())之前都需要按Enter键.

All the methods I've tried require an Enter keypress before passing the input (getchar(), for instance).

推荐答案

我对此事进行了一些研究,发现虽然D 1.0下的Phobos库确实具有您需要的形式, std.c.stdio.getch(),D 2.0缺少此功能. Phobos中的其他标准输入功能似乎都没有您想要的行为.

I've done some research on the matter, and I've found that, while the Phobos library under D 1.0 had exactly what you need in the form of std.c.stdio.getch(), D 2.0 lacks this function. None of the other standard input functions in Phobos appear to have the behavior you want.

据我了解,这是因为所需的行为(即,无需Enter键即可获取单个字符)是相当不规范的,必须以相对丑陋,特定于平台的方式来实现. (功能getch最初以形式存在于C的 <conio.h> 中, DOS专用标头,尽管它不是标准C库的一部分,但实际上已成为事实上的跨平台标准.)显然,Phobos运行时库的维护者决定在操作系统中剥离掉这一向后兼容的功能.清除程序库的名称,但以牺牲此功能为代价.

As I understand it, this is because the desired behavior (that is, getting a single character without the need for an Enter keypress) is rather nonstandard and has to be implemented in relatively ugly, platform-specific ways. (In its initial form, the function getch existed in C's <conio.h>, a DOS-specific header that has become somewhat of a de facto cross-platform standard despite not being part of the standard C library.) Evidently the maintainers of the Phobos runtime library decided to strip out this particular bit of backwards-compatible functionality in the name of a cleaner library, but at the expense of this functionality.

据报道,您可以通过将其添加到源文件中来解决此缺失的函数声明:

Reportedly, you can get around this missing function declaration by adding this to your source file:

extern (C) int getch();

但是,我发现这会产生一个链接器错误,这表明该函数已从运行时库中完全删除,而不仅仅是从std.c.stdio中删除了它的声明.当然值得一试-它可能恰好可以在您的系统和编译器上运行,我真的不知道.

However, I have found that this produces a linker error, suggesting that the function was removed from the runtime library entirely, rather than merely having its declaration removed from std.c.stdio. It's certainly worth a try—it might happen to work on your system and compiler, I really don't know.

实际上,这似乎在Windows上有效;在Linux方面对我来说失败了.看来Windows下的DMD首先链接到Phobos/D运行时(phobos.lib),然后是C运行时(snn.lib);但是,在Linux上,DMD链接到同时提供这两个部分的一个运行时库.这种差异似乎导致与未声明函数(其中的getch)的链接仅在Windows上有效.如果Windows是您关心的唯一平台,则此解决方案可能是合适的.如果您需要更多的跨平台兼容性,请继续阅读.

Edit 2: This actually seems to work on Windows; it failed for me on the Linux side. It appears that DMD under Windows first links to the Phobos/D runtime (phobos.lib), then the C runtime (snn.lib); however, on Linux, DMD links to one runtime library that supplies both parts. This difference appears to cause linkage to undeclared functions (getch among them) to only work on Windows. If Windows is the only platform you're concerned with, this solution is probably suitable. If you need more cross-platform compatibility, read on.

另一种可能性是使用ncurses库.它实现了 getch 函数,该函数肯定会满足您的要求-只要您查找库的D绑定或仅使用C接口很酷.请注意,它不仅需要调用所需的函数,还需要更多的设置. 此线程具有有关此问题的更多信息.

Another possibility is to use the ncurses library. It implements a getch function that will definitely do what you want—provided that you're cool with finding D bindings for the library or just using the C interface. Be aware that it requires a smidgen more setup than just calling the function you want; this thread has more information on the matter.

现在,使用一些非常丑陋的解决方案.使用D 1.0可以使您在Phobos标准库中找到所需的内容-但这显然需要使用语言的更老版本,我个人认为在标准库中缺少一个控制台IO功能是没有道理的使用旧版本的D.

Now, for some considerably uglier solutions. Using D 1.0 would allow you to find what you need in the Phobos standard library—but this obviously entails using the older, crustier version of the language and I personally do not find the lack of one console IO function in the standard library to be justification for using the old version of D.

我相信Tango在切换到D 2.0时也丢失了它的getch声明(在tango.stdc.stdio下),但是我对Tango的了解非常有限,所以我可能是错的.

I believe that Tango also lost its getch declaration (under tango.stdc.stdio) over the switch to D 2.0, but my knowledge of Tango is extremely limited, so I may be wrong.

如果确定,您可以编写您自己的getch.我无法使用 Google代码搜索getch的跨平台C实现. >,这使我对可以简单地适用于D的相对简单的10行左右函数实现的可能性感到悲观.

If you're determined, you could just write your own getch. I wasn't able to find a cross-platform C implementation of getch using Google Code Search, which leaves me pessimistic about the likelihood of a relatively simple, 10-line-or-so function implementation that could simply be adapted to D.

另一方面,Walter Bright(您设计语言的人)为这种功能此处.但是,即使这样似乎也有些过时,因为在当前版本的DMD2编译器中未定义符号之一cfmakeraw.但是,它实际上几乎是一个可行的解决方案.

On the other hand, Walter Bright—you know, the guy who designed the D language—provides a D implementation of just this sort of function here. However, even this appears to be somewhat outdated, because one of the symbols, cfmakeraw, is undefined under the current version of the DMD2 compiler. However, it's really close to being a workable solution.

这篇关于如何在D2(Phobos)中获得单个击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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