Ejemplo 1: pulsación de la tecla de comprobación de Python
import keyboard
# if key 'a' is pressed
if keyboard.is_pressed('a'):
print('a key has ben pressed')
Ejemplo 2: Python obtiene un valor de tecla presionada
import keyboard # using module keyboard
while True: # making a loop
try: # used try so that if user pressed other than the given key error will not be shown
if keyboard.is_pressed('q'): # if key 'q' is pressed
print('You Pressed A Key!')
break # finishing the loop
except:
break # if user pressed a key other than the given key the loop will break
Ejemplo 3: detectar pulsación de tecla python
# pip3 install pynput
from pynput.keyboard import Key, Listener
def show(key):
pressed_key = str(key).replace("'", "")
print(" key: ", pressed_key)
if key == Key.esc:
# Stop listener
return False
# Listener
with Listener(on_press=show) as listener:
listener.join()
Ejemplo 4: Python comprobar si se presiona la tecla
import keyboard
# Check if b was pressed
if keyboard.is_pressed('b'):
print('b Key was pressed')
Ejemplo 5: como detectar pulsaciones de teclas en Python
# in console: pip install keyboard
import time
import keyboard as kb
key = "a"
while (True):
if (kb.is_pressed(key)):
print("a pressed")
time.sleep(0.001) # decreases CPU usage
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)