moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.79k stars 248 forks source link

Hotkteys every time make function twice, why? #394

Closed akzeitlos closed 3 years ago

akzeitlos commented 3 years ago

Hello, I have used Hotkey from pynput and everything works fine but the execute functions are activated twice, can anybody tell me why.

CODE

# Author: Andreas Krüger
# E-Mail: posttoandreas@googlemail.com

# Import all Pythonfiles from selectede Subfolders vorbereiten
import glob
# Importieren vom Tastatur und Maus Eventlistener
from pynput.keyboard import HotKey, Key, KeyCode, Listener

### Formularefiles importieren
modules = glob.glob('Formulare/*.py')
from Formulare import *

### Hotkeys
abro = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='7')],
    executeAbro
)

copd = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='6')],
    executeCopd
)

dmd = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='5')],
    executeDmd
)

dms = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='0')],
    executeDms
)

khk = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='8')],
    executeKhk
)

hotkeys = [abro, copd, dmd, dms, khk]

def signal_press_to_hotkeys(key):
    for hotkey in hotkeys:
        hotkey.press(l.canonical(key))

def signal_release_to_hotkeys(key):
    for hotkey in hotkeys:
        hotkey.release(l.canonical(key))

with Listener(on_press=signal_press_to_hotkeys, on_release=signal_release_to_hotkeys) as l:
    l.join()
moses-palmer commented 3 years ago

Thank you for the report, and I apologise for this late reply.

I had to modify your script slightly to be able to run it:

from pynput.keyboard import HotKey, Key, KeyCode, Listener

def m(name):
    def inner():
        print(name)
    return inner

executeAbro = m('executeAbro')
executeCopd = m('executeCopd')
executeDmd = m('executeDmd')
executeDms = m('executeDms')
executeKhk = m('executeKhk')

abro = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='7')],
    executeAbro
)

copd = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='6')],
    executeCopd
)

dmd = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='5')],
    executeDmd
)

dms = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='0')],
    executeDms
)

khk = HotKey(
    [Key.ctrl, Key.shift, KeyCode(char='8')],
    executeKhk
)

hotkeys = [abro, copd, dmd, dms, khk]

def signal_press_to_hotkeys(key):
    for hotkey in hotkeys:
        hotkey.press(l.canonical(key))

def signal_release_to_hotkeys(key):
    for hotkey in hotkeys:
        hotkey.release(l.canonical(key))

with Listener(on_press=signal_press_to_hotkeys, on_release=signal_release_to_hotkeys) as l:
    l.join()

When running this, I only get one print of the formula name for each hotkey invocation. Does your system react differently?

moses-palmer commented 3 years ago

I will close this due to lack of feedback. Please reopen if you have more information.