Issue
Usng pynput, I would like to execute some code only when some keys are pressed.
import sys
print( sys.executable )
from pynput import keyboard
from pynput.keyboard import Key, Controller
import time
import win32ui
import dde
import pandas as pd
import numpy as np
import os
keyboard_controller = Controller()
# The event listener will be running in this block
with keyboard.Events() as events:
for event in events:
if event.key == keyboard.Key.esc:
break
if event.key == keyboard.Key.f10:
print('Received event {}'.format(event))
When pressing f10, this outputs
Received event Press(key=Key.f10)
Received event Release(key=Key.f10)
How can I get rid of the 'release' event and only execute code on key press?
Solution
You can add an additional type check to skip Release
events:
if event.key == keyboard.Key.f10:
if type(event) is keyboard.Events.Press:
print(f"Received event {event}")
Answered By - import random
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.