如何使用Python3中的键盘打破倒计时?

发布于 2025-01-29 16:46:16 字数 128 浏览 2 评论 0原文

在某个过程的中间,我有一个倒计时的暂停(对于时间循环。我希望能够用键盘将其打破,以便能够比Countdown更早地完成。 使用Python3(没有Linux root访问)的Python3可能吗?在这种情况下,我可以获取有关按键的一些信息吗?

In the middle of some process I have a pause with countdown (for loop with time.sleep(1) inside). I'd like to be able to break it with keyboard to be able to continue earlier than countdown got finished.
Is it possible with python3 (cross-platform without Linux root access)? Can I get some information about key pressed in this case ?

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

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

发布评论

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

评论(2

蓝海似她心 2025-02-05 16:46:16

与此问题有关:在(1)python 解决方案时使用Getch

解决方案:使用:使用:使用: kbhit()getch() https://docs.python.org/3.10/library/msvcrt.html#console-io

工作示例:

import msvcrt
from time import sleep
def a():
    for n in range(10):
        if msvcrt.kbhit():
            return msvcrt.getch()
        sleep(1)

请注意,它的反应延迟长达1秒

Related to this question: Use getch in while (1) python

Solution: use kbhit() and getch(): https://docs.python.org/3.10/library/msvcrt.html#console-i-o

Working example:

import msvcrt
from time import sleep
def a():
    for n in range(10):
        if msvcrt.kbhit():
            return msvcrt.getch()
        sleep(1)

Note that it has a reaction delay of up to 1 second

欢你一世 2025-02-05 16:46:16

这是我的“跨平台”非常简化的解决方案。

def my_sleep(wait_sec):
    current_platform = platform.system()
    if current_platform == "Windows":
        import msvcrt

    elif current_platform == "Linux":
        import termios
        from select import select

        # save the terminal settings
        fd = sys.stdin.fileno()
        new_term = termios.tcgetattr(fd)
        old_term = termios.tcgetattr(fd)

        # switch to normal terminal
        def set_normal_term():
            termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)

        # switch to unbuffered terminal
        def set_curses_term():
            termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)

        def kbhit():
            dr, dw, de = select([sys.stdin], [], [], 0)
            return dr != []

        set_curses_term()
        # new terminal setting unbuffered
        new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
        set_curses_term()

    for sec_left in range(wait_sec, 0, -1):
        if current_platform == 'Windows':
            if msvcrt.kbhit():
                # print(f':: {msvcrt.getch()}')
                break
        elif current_platform == 'Linux':
            if kbhit():
                ch = sys.stdin.read(1)
                # print(f'::  {ch}')
                break
        print(sec_left)
        time.sleep(1)
    
    if current_platform == "Linux":
        set_normal_term()

Here is my "cross-platform" very simplified solution.

def my_sleep(wait_sec):
    current_platform = platform.system()
    if current_platform == "Windows":
        import msvcrt

    elif current_platform == "Linux":
        import termios
        from select import select

        # save the terminal settings
        fd = sys.stdin.fileno()
        new_term = termios.tcgetattr(fd)
        old_term = termios.tcgetattr(fd)

        # switch to normal terminal
        def set_normal_term():
            termios.tcsetattr(fd, termios.TCSAFLUSH, old_term)

        # switch to unbuffered terminal
        def set_curses_term():
            termios.tcsetattr(fd, termios.TCSAFLUSH, new_term)

        def kbhit():
            dr, dw, de = select([sys.stdin], [], [], 0)
            return dr != []

        set_curses_term()
        # new terminal setting unbuffered
        new_term[3] = (new_term[3] & ~termios.ICANON & ~termios.ECHO)
        set_curses_term()

    for sec_left in range(wait_sec, 0, -1):
        if current_platform == 'Windows':
            if msvcrt.kbhit():
                # print(f':: {msvcrt.getch()}')
                break
        elif current_platform == 'Linux':
            if kbhit():
                ch = sys.stdin.read(1)
                # print(f'::  {ch}')
                break
        print(sec_left)
        time.sleep(1)
    
    if current_platform == "Linux":
        set_normal_term()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文