我有一个运行 Bullseye 的 Raspberry Pi 32 位。它无头运行,但使用 USB 鼠标。在 python 代码中,我想从 USB 键盘读取按键信息。我已经尝试过 python 键盘模块,但只有当我以 root 身份运行代码时才有效,但我不想这样做。 我看过很多帖子谈论 /dev/ttyUSB 只是 /dev/tty0 到 63 我怎样才能获得按键信息?
我认为你可以使用(如果你使用的是 pygame)这个:
import pygame
player_x = 100
player_y = 100
player_width = 50
player_height = 50
player_speed = 5
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_x > 0:
player_x -= player_speed
if keys[pygame.K_RIGHT] and player_x < width - player_width:
player_x += player_speed
if keys[pygame.K_UP] and player_y > 0:
player_y -= player_speed
if keys[pygame.K_DOWN] and player_y < height - player_height:
player_y += player_speed