How to get key presses / combinations without being modified into alt combinations?

48 Views Asked by At

I am trying to get all of the key presses so that I can log the commands that someone would like to use for a project that I am doing in rumps. When I try to listen to the keyboard inputs with pynput, it seems that when I try to press alt + j it converts the output into the unicode variant .

from pynput.keyboard import Key, Listener

class keyboard_Input:
    def __init__(self):
        self.keys_released = []

    def on_press(self, key):
        print('{0} pressed'.format(key))

    def on_release(self, key):
        print(vars(key))
        print('{0} release'.format(key))
        self.keys_released.append(key)
        if key == Key.esc:
            self.keys_released.pop()
            # Stop listener
            return False

    def listen(self):
        with Listener(on_press=self.on_press, on_release=self.on_release) as listener:
            listener.join()
        return listener

    def keys_entered(self):
        return self.keys_released


keyListen = keyboard_Input()
keyListen.listen()
print(keyListen.keys_entered())

Using this code and pressing down alt + cmd + j gives:

# plus various print statements while running the code (removed for clarity)
[<Key.cmd: <55>>, '∆', <Key.alt: <58>>, <Key.cmd: <55>>]

How can I try to convert the character back into the actual characters (the alt + j) that are being pressed or is there a better method of logging the button presses?

I thought about trying to convert it into the alt code, but from here: Decode Alt codes with python it says that the alt key method is not on macOS (which is what I am running).

I found this: Is there an Alt key combination to copy to clipboard? but the comment mentions windows, which is not the platform that I am developing on.

0

There are 0 best solutions below