社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

如何在python程序中检测“space”?

Roberto Chavo • 5 年前 • 1650 次点击  

我正在创建一个基于文本的游戏,为了加速过去的对话,我希望用户能够点击'空间'并跳过它。

import time
import sys

text_speed = .05

def slow_type(line, speed): #You input the dialogue and speed(smaller = faster)
    for l in line:
        sys.stdout.write(l)
        sys.stdout.flush()
        time.sleep(speed)
    time.sleep(.5)

if <'Space'> pressed:
    text_speed = 0

NL1 = "Huh, I see you finally came. "

slow_type(NL1, text_speed)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40285
 
1650 次点击  
文章 [ 3 ]  |  最新文章 5 年前
Black Thunder
Reply   •   1 楼
Black Thunder    6 年前

我建议你用 keyboard .

下面是一个检测空格的示例代码,还请注意,该代码将等待空格键被按下。

import keyboard

#.....

if keyboard.is_pressed("space"):
    text_speed = 0

#....
blhsing
Reply   •   2 楼
blhsing    6 年前

你可以使用 getch 模块。

import getch
while getch.getch() != ' ':
    pass

或者在windows上,您可以使用 msvcr.getch :

import msvcrt
while msvcrt.getch() != ' ':
    pass
BernardL
Reply   •   3 楼
BernardL    6 年前

空间的python表示是 u"\u0020" ,参考 here .

并测试:

if input('enter:') == u"\u0020":
    print('pass')
else:
    print('fail')