Python Pynput Keyboardtracking CTRL + "second_key" doesn't output "second_key"

65 Views Asked by At

I am trying to track the Keyboard Inputs, of a User if they press a combination with any CTRL-Key. For that I use Python with Pynput.

The regular tracking works fine. But as soon as I press CTRL + any regular Key, the output is unusable for me, because it changes the expected Key Output completely...

In the example I am pressing & releasing "a" first, then CTRL_L, then CTRL_L + "a".

So does someone know how I can "translate" the "\x01" output to "CTRL + a" or something like that? :D

Logging Output:

>> 08:58:46.694 | v1.0.0 | DEBUG -> Line 413: 'a' pressed -> <class 'pynput.keyboard._win32.KeyCode'>
>> 08:58:46.695 | v1.0.0 | DEBUG -> Line 415: Type: KeyCode
>> 08:58:46.695 | v1.0.0 | DEBUG -> Line 417: char: a
>> 08:58:46.695 | v1.0.0 | DEBUG -> Line 425: ['a']
>> 08:58:46.696 | v1.0.0 | DEBUG -> Line 433: 'a' released
>> 08:58:47.697 | v1.0.0 | DEBUG -> Line 413: Key.ctrl_l pressed -> <enum 'Key'>
>> 08:58:47.697 | v1.0.0 | DEBUG -> Line 425: [<Key.ctrl_l: <162>>]
>> 08:58:47.798 | v1.0.0 | DEBUG -> Line 433: Key.ctrl_l released
>> 08:58:50.541 | v1.0.0 | DEBUG -> Line 413: Key.ctrl_l pressed -> <enum 'Key'>
>> 08:58:50.541 | v1.0.0 | DEBUG -> Line 425: [<Key.ctrl_l: <162>>]
>> 08:58:51.140 | v1.0.0 | DEBUG -> Line 413: '\x01' pressed -> <class 'pynput.keyboard._win32.KeyCode'>
>> 08:58:51.143 | v1.0.0 | DEBUG -> Line 415: Type: KeyCode
>> 08:58:51.143 | v1.0.0 | DEBUG -> Line 417: char: ☺
>> 08:58:51.144 | v1.0.0 | DEBUG -> Line 425: [<Key.ctrl_l: <162>>, '\x01']
>> 08:58:51.144 | v1.0.0 | DEBUG -> Line 433: '\x01' released
>> 08:58:51.354 | v1.0.0 | DEBUG -> Line 433: Key.ctrl_l released

Callback Functions:

def keyboard_on_press(self, key):
    try:
        logger.debug(f"{key} pressed -> {type(key)}")
        if type(key) is keyboard._win32.KeyCode:
            logger.debug("Type: KeyCode")
            if key.char:
                logger.debug(f"char: {key.char}")
            elif key.is_dead:
                logger.debug(f"is_dead: {key.is_dead}")
            elif key.vk:
                logger.debug(f"vk: {key.vk}")
            
        if key not in self.pressedKeys:
            self.pressedKeys.append(key)
            logger.debug(self.pressedKeys)
            
        if keyboard.Key.ctrl in self.pressedKeys:
            logger.debug(f"ctrl + {key} pressed")
    except e as Exception:
        logger.warn(e)

def keyboard_on_release(self, key):
    logger.debug(f"{key} released")
    if key in self.pressedKeys:
        self.pressedKeys.remove(key)
    else:
        logger.warn(f"{key} not found in List!!")

Used Versions:

  • Windows 11 Pro (tried Windows 10 as well)
  • Python 3.11.4
  • Pynput 1.7.6

I have looked up the Docs and GitHub issues page, but didn't find anything fitting. Also tried Windows 10 on bare Metal as the Windows 11 PC is a VM. But sadly with the same Output :/

0

There are 0 best solutions below