我如何使用诅咒来响应密钥按下,而无需暂停程序等待输入?

发布于 2025-01-30 06:26:24 字数 316 浏览 2 评论 0原文

我想做的一个示例是:

import curses

while True:
        key = stdscr.getch()

        if key == 119:
            print("key w pressed")
        else:
            print("w key not pressed")

不仅在输入钥匙并且Getch不返回119时,ELSE语句不断打印的位置。换句话说,我不希望Getch函数等待按键按下,我只希望它仅返回键如果正在按下键,否则请勿返回或类似的东西。

An example of what I would like to do is the following:

import curses

while True:
        key = stdscr.getch()

        if key == 119:
            print("key w pressed")
        else:
            print("w key not pressed")

Where the else statement prints constantly, not only when a key is entered and getch does not return 119. In other words, I don't want the getch function to wait for a key press, I only want it to only return a key if a key is being pressed, otherwise return None or something like that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2025-02-06 06:26:24

在Curses中,窗口具有nodelay flag。如果将其设置为true,则GETCH函数将不会等待用户输入,但是如果不读取输入,将返回-1

import curses

stdscr.nodelay(True)

while True:
        key = stdscr.getch()
        if key == -1:
            print("no key was pressed")
        elif key == 119:
            print("key w pressed")
        else:
            print("different key than w was pressed")

In curses the window has a nodelay flag. If it is set to true, the getch function won't wait for user input, but will return -1 if no input is to be read.

import curses

stdscr.nodelay(True)

while True:
        key = stdscr.getch()
        if key == -1:
            print("no key was pressed")
        elif key == 119:
            print("key w pressed")
        else:
            print("different key than w was pressed")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文