简体   繁体   中英

How to Read Key pressed in python

How do you move a circle up, down and side to side, without downloading other things ( pygame )? I tried using getch but it would not work. Can someone help me?

This is my code:

import msvcrt

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

On Windows, you should use

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 . Its easy to use and unlike other modules, it'll work.

Here is an example code which will detect 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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