I've already looked at previous post, but can't find something that works...
Globally my goal: a script in python that if I press 'k' it will start to hold 'k' until I press escape. But the problem is: it doesn't hold 'k' and I don't know why
If someone might help I'd be glad, Thank you
from pynput import keyboard
import pyautogui
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
if key.char == 'k':
return False
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
def listening_k():
with keyboard.Listener(
on_press=on_press) as listener:
listener.join()
return True
def listening_esc():
with keyboard.Listener(
on_release=on_release) as listener:
listener.join()
return True
def hold_key(key):
while listening_k() == True :
pyautogui.keyDown(key)
if listening_esc() == True :
break
hold_key('k')
You can make it simple by having a variable that holds the state (boolean value) if 'k' key is on press. Make it True anytime you press 'k' key and False for pressing 'esc' key.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.