简体   繁体   English

如何读取python中按下的键

[英]How to Read Key pressed in python

How do you move a circle up, down and side to side, without downloading other things ( pygame )?你如何在不下载其他东西的情况下向上、向下和左右移动一个圆圈( pygame )? I tried using getch but it would not work.我尝试使用getch但它不起作用。 Can someone help me?有人能帮我吗?

This is my code:这是我的代码:

import msvcrt

key = msvcrt.getch()
while True:
    print(key)

On Windows, you should use在 Windows 上,您应该使用

if msvcrt.kbhit():
    c = msvcrt.getch()

inside a polling loop.在轮询循环内。

You can't do anything other than installing a module.除了安装模块之外,您不能做任何事情。 I would strongly recommend you use keyboard .我强烈建议您使用keyboard Its easy to use and unlike other modules, it'll work.它易于使用,与其他模块不同,它可以工作。

Here is an example code which will detect c :这是一个将检测c的示例代码:

import keyboard
while True:
    if keyboard.is_pressed("c"):  #It will not include capital C
        print("C pressed!")
        break

It will receive keys from the whole Windows它将接收来自整个 Windows 的密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM