如何获取D编程语言+ Tango中的单个按键? [英] How can I grab single key hit in D Programming Language + Tango?

查看:92
本文介绍了如何获取D编程语言+ Tango中的单个按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了文章,并且尝试使用D编程语言进行练习,但在第一次练习中遇到问题。

I read this article and try to do the exercise in D Programming Language, but encounter a problem in the first exercise.


(1)显示一系列数字
(1,2,3,4,5 .... etc)在无限的
循环中。如果有人敲
的特定键(说
ESCAPE键),程序应该退出。

(1) Display series of numbers (1,2,3,4, 5....etc) in an infinite loop. The program should quit if someone hits a specific key (Say ESCAPE key).

无限循环不是一个大问题,但其余的是。我怎样才能抓住D / Tango的关键点?在探戈常见问题中,它说使用C函数kbhit()或get(),但是据我所知,它们不在C标准库中,也不存在于我用来编程的Linux机器随附的glibc中。

Of course the infinite loop is not a big problem, but the rest is. How could I grab a key hit in D/Tango? In tango FAQ it says use C function kbhit() or get(), but as I know, these are not in C standard library, and does not exist in glibc which come with my Linux machine which I use to programming.

我知道我可以使用某些第三方库,例如 ncurses ,但是与kbhit()或get()一样,它也有相同的问题,它不是C或D中的标准库,也不是预装在Windows上。我希望我可以仅使用D / Tango来完成此练习,并且可以在Linux和Windows计算机上运行它。

I know I can use some 3rd party library like ncurses, but it has same problem just like kbhit() or get(), it is not standard library in C or D and not pre-installed on Windows. What I hope is that I could done this exercise use just D/Tango and could run it on both Linux and Windows machine.

我该怎么做?

推荐答案

使用D编程语言的方法如下:

Here's how you do it in the D programming language:

    import std.c.stdio;
    import std.c.linux.termios;

    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
    c = fgetc(stdin);

    // Close
    tcsetattr(1, TCSADRAIN, &ostate);       // return to original mode

这篇关于如何获取D编程语言+ Tango中的单个按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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