【问题标题】:capturing key strokes in PyCharm debug console在 PyCharm 调试控制台中捕获击键
【发布时间】:2014-01-15 08:45:43
【问题描述】:

PyCharm 有一个不错的调试控制台,您可以在其中查看程序的输出。我有一个捕获用户击键的交互式程序。它在命令 shell 中运行良好,但是它不会在 PyCharm 的调试控制台中捕获击键。 “显示命令行”也无济于事,因为 PyCharm 会捕获那里写的任何内容。

有没有办法设置控制台,以便击键到达正在运行的程序?

【问题讨论】:

    标签: debugging pycharm


    【解决方案1】:

    我在不等待输入/返回的情况下尝试读取击键时遇到了同样的问题。我最终使用了这段代码,它会给我一个 getch(),它不会正常等待输入/返回,但仍然可以在 PyCharm 调试器中工作(使用输入/返回)足以进行调试:

    def getchfunc():
        import termios
        import sys, tty
        if sys.stdin.isatty():
            def _getch():
                fd = sys.stdin.fileno()
                old_settings = termios.tcgetattr(fd)
                try:
                    tty.setraw(fd)
                    ch = sys.stdin.read(1)
                finally:
                    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
                return ch
    
        else:
            def _getch():
                return sys.stdin.read(1)
        return _getch
    

    【讨论】:

      猜你喜欢
      • 2012-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 2020-08-31
      • 1970-01-01
      • 1970-01-01
      • 2016-12-05
      相关资源
      最近更新 更多