是否有一种简单的跨平台方法可以使用 Phobos 在 D2 中进行单次击键?
例如,“按任意键继续...”提示,或者 Brainfuck 解释器。
我尝试过的所有方法都需要在传递输入之前按 Enter 键(例如 getchar())。
在 Windows 上使用 D2 的最简单的解决方案:
import std.stdio : writefln;
extern(C) int kbhit();
extern(C) int getch();
void main()
{
while(!kbhit())
{
// keep polling
// might use Thread.Sleep here to avoid taxing the cpu.
}
writefln("Key hit was %s.", cast(char)getch());
}
它甚至可能与 D1 一起使用,但我还没有尝试过。
这是一个Linux版本,修改自沃尔特的帖子:
import std.stdio : writefln;
import std.c.stdio;
import std.c.linux.termios;
extern(C) void cfmakeraw(termios *termios_p);
void main()
{
termios ostate; /* saved tty state */
termios nstate; /* values for editor mode */
// Open stdin in raw mode
/* Adjust output channel */
tcgetattr(1, &ostate); /* save old state */
tcgetattr(1, &nstate); /* get base of new state */
cfmakeraw(&nstate);
tcsetattr(1, TCSADRAIN, &nstate); /* set mode */
// Read characters in raw mode
writefln("The key hit is %s", cast(char)fgetc(stdin));
// Close
tcsetattr(1, TCSADRAIN, &ostate); // return to original mode
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)