确定Python中按键的长度 [英] Determine length of keypress in python
问题描述
假设我想用python编写一个非常简单的程序,该程序指示按下了一个键的时间.因此,如果我键入并按住j
键片刻,我希望编写一个能够显示诸如the key 'j' was pressed for 1.1 seconds
之类信息的程序.
Suppose that I want to make a very simply program in python that indicates how long a key is pressed. So if I type and hold the j
key for a few moments, I am looking to write a program capable of displaying information like the key 'j' was pressed for 1.1 seconds
.
据我了解,应该通过检测并标记KEYDOWN事件和KEYUP事件并加时间戳,并适当减去时间戳来实现此目的.因此检测到KEYDOWN和KEYUP事件就足够了.
From what I understand, the way this should be achieved is by detecting and timestamping the KEYDOWN events and KEYUP events, and making appropriate subtractions of timestamps. So it would suffice to detect KEYDOWN and KEYUP events.
关于SO的检测和回答有很多种,例如这个,它们都使用某种形式的getch.我看过python curses库,从中可以看出,密钥检测的主要形式也是单字符getch()形式.但是这些不能检测到按键的长度---它们只能检测到KEYDOWN.
There are a wide variety of questions and answers on SO concerning detecting a single keypress or about detecting single character input, such as this one or this one, which both use some form of getch. I've looked at the python curses library, and from what I can tell the primary form of key detection is also in the form of single-character getch(). But these do not detect the length of keypress --- they only detect KEYDOWN.
我认识到检测按键长度是游戏中的必要任务,因此我希望pygame具有检测按键持续时间的方法.但我希望可以使用更纤薄,更直接的库来检测按键持续时间.
I recognize that detecting the length of a keypress is a necessary task in gaming, and so I expect that pygame has methods to detect keypress duration. But I would hope that it is possible to use a much slimmer and more direct library to detect keypress duration.
推荐答案
使用pynput
模块:(最佳)
您可以使用以下代码:
Using pynput
module: (Best)
You can use this code:
from pynput import keyboard
import time
def callb(key): #what to do on key-release
ti1 = str(time.time() - t)[0:5] #converting float to str, slicing the float
print("The key",key," is pressed for",ti1,'seconds')
return False #stop detecting more key-releases
def callb1(key): #what to do on key-press
return False #stop detecting more key-presses
with keyboard.Listener(on_press = callb1) as listener1: #setting code for listening key-press
listener1.join()
t = time.time() #reading time in sec
with keyboard.Listener(on_release = callb) as listener: #setting code for listening key-release
listener.join()
使用pygame
:(好)
Using pygame
: (Good)
import time
import pygame
import os
os.environ["SDL_VIDEO_CENTERED"] = "1"
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Time")
clock = pygame.time.Clock()
pygame.init()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
break
if event.type == pygame.KEYDOWN:
# detect key 'a'
if event.key == pygame.K_a: # key 'a'
t = time.time()
if event.type == pygame.KEYUP:
if event.key == pygame.K_a: # key 'a'
t = time.time() - t; t = str(t); t = t[:5]
print("You pressed key 'a' for",t,'seconds')
screen.fill((255, 255, 255))
pygame.display.update()
clock.tick(40)
它将仅检测您将在代码中编写的键.
It will only detect the keys that you will write in the code.
使用pip install pynput
安装pynput
.
使用pip install pygame
安装pygame
.
Use pip install pynput
to install pynput
.
Use pip install pygame
to install pygame
.
这篇关于确定Python中按键的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!