Python 3 - 如何在不按 <return> 的情况下输入数据在 OSX 中 [英] Python 3 - How to input data without pressing <return> in OSX

查看:16
本文介绍了Python 3 - 如何在不按 <return> 的情况下输入数据在 OSX 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在游戏中使用asdw"键移动角色,但我找不到不按回车键不断输入数据的方法.我已经看到在 windows 上有一个名为 msvcrt 的模块,它有一个 getch 功能,所以我想知道是否有办法在 OSX 中模拟这个,或者更简单地只是不断地从键盘输入数据.

I am attempting to move a character using the "asdw" keys in a game, but I cannot find a way to constantly input data without pressing return. I have seen that on windows there is a module called msvcrt, which has a getch function, so I am wondering if there is a way to simulate this in OSX, or more simply to just constantly input data from the keyboard.

推荐答案

试试 curses 库:

http://docs.python.org/py3k/library/curses.html

Curses 是一个用于控制终端的库,它还包括绘制框形状等功能.它适用于任何与 POSIX 兼容的系统,包括 Mac OS X 和 GNU/Linux.

Curses is a library for controlling the terminal, and includes features such as drawing box shapes as well. It's available on any POSIX-compatible system, which includes Mac OS X and GNU/Linux.

这是一个例子:

import curses
import time

# Turn off line buffering
curses.cbreak()

# Initialize the terminal
win = curses.initscr()

# Make getch() non-blocking
win.nodelay(True)

while True:
    key = win.getch()
    if key != -1:
        print('Pressed key', key)
    time.sleep(0.01)

这篇关于Python 3 - 如何在不按 <return> 的情况下输入数据在 OSX 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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