如何从 USB 键盘读取击键

问题描述 投票:0回答:1

我有一个运行 Bullseye 的 Raspberry Pi 32 位。它无头运行,但使用 USB 鼠标。在 python 代码中,我想从 USB 键盘读取按键信息。我已经尝试过 python 键盘模块,但只有当我以 root 身份运行代码时才有效,但我不想这样做。 我看过很多帖子谈论 /dev/ttyUSB 只是 /dev/tty0 到 63 我怎样才能获得按键信息? 谢谢 米克

python usb pi
1个回答
0
投票

我认为你可以使用(如果你使用的是 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

我能提供的就这些了。如果它不能回答您的问题,我很抱歉。我对堆栈溢出还比较陌生(正如您在我的帐户中看到的那样)。

© www.soinside.com 2019 - 2024. All rights reserved.