Python Keyboard library not suppressing keystrokes on Linux

Viewed 1568

I am making a script that remaps a single key (right-ctrl) into alt+tab using the Python library known as keyboard. This was easy to do with Autohotkey on windows, however, this doesn't seem to be possible on Linux. After all in the keyboard documentation they have funcion(param, param, Suppress=False), so it should work right?

import keyboard

def altTab:
    keyboard.release(97)
    keyboard.send("alt+tab")

# 97 is the key_code for [right ctrl] on my system
keyboard.on_press_key(97, altTab, suppress=True)

 
I've tried releasing the key from the code standpoint but it doesn't seem to work, as ctrl+alt+tab is different from alt+tab. I have also tried using the keyboard.remap_key function to change right ctrl into right alt, and right alt into left alt so that right alt would work, then sending just tab instead of alt+tab but it still doesn't work. I am using Ubuntu Linux.
Please help, I'm stumped

1 Answers

You need hook_key, that is the method to call provided callback every time provided key is pressed:

import keyboard

def altTab(e):
    if e.event_type == "down":
        keyboard.release(97)
        keyboard.send("alt+tab")

# 97 is the key_code for [right ctrl] on my system
keyboard.hook_key(97, altTab, suppress=True)

Edit: added code to deal with key press only.

Related