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

查看:80
本文介绍了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天全站免登陆