Answer the question
In order to leave comments, you need to log in
Why is the function being re-executed?
I have this code
from pynput.keyboard import Key, KeyCode, Listener
from pynput.mouse import Button, Controller
import sys, os, ctypes
import pyscreenshot as ImageGrab
# закончить код
def end_fun():
sys.exit()
def start_mouse():
print('dddd')
def save_image():
print('ssss')
im = ImageGrab.grab()
# Код для горячих клавищ
combination_to_function = {
frozenset([KeyCode(vk=49)]): end_fun,
frozenset([KeyCode(vk=50)]): start_mouse,
frozenset([KeyCode(vk=53)]): save_image,
}
# The currently pressed keys (initially empty)
pressed_vks = set()
def get_vk(key):
return key.vk if hasattr(key, 'vk') else key.value.vk
def is_combination_pressed(combination):
return all([get_vk(key) in pressed_vks for key in combination])
def on_press(key):
vk = get_vk(key) # Get the key's vk
print(':vk =', vk, ':key =', key)
pressed_vks.add(vk) # Add it to the set of currently pressed keys
for combination in combination_to_function: # Loop through each combination
if is_combination_pressed(combination): # Check if all keys in the combination are pressed
combination_to_function[combination]() # If so, execute the function
def on_release(key):
vk = get_vk(key) # Get the key's vk
pressed_vks.remove(vk) # Remove it from the set of currently pressed keys
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()
im = ImageGrab.grab()
Answer the question
In order to leave comments, you need to log in
It seems that the required keycode is not removed from pressed_vks. Check that the same thing is deleted there as it is inserted.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question