trying to use pynput's globalhotkeys and pynput's keyboard controller in same script - is this possible?

Viewed 17

I cannot get pynput's keyboard.GlobablHotKeys() and keyboard.Controller() to work in the same script. In this example script, CTRL+c isn't being sent (or received by system, as selected text doesn't get copied into clipboard). I'm using pygetwindow just to confirm that the correct window has focus prior to sending CTRL+c.

from pynput import keyboard
import clipboard 
import pygetwindow as gw

keybrd = keyboard.Controller()

def copy_clipboard():
    print(gw.getActiveWindowTitle())
    with keybrd.pressed(keyboard.Key.ctrl.value):
        keybrd.press('c')
        keybrd.release('c')
    return clipboard.paste()

def function_1():
    print('Function 1 activated')
    clipboardContents = copy_clipboard()
    print(clipboardContents)

def function_2():
    print('Function 2 activated')
    clipboardContents = copy_clipboard()
    print(clipboardContents)
  
with keyboard.GlobalHotKeys({
        '<alt>+<shift>+u': function_1,
        '<ctrl>+<alt>+b': function_2}
        ) as h:
    h.join()
0 Answers
Related