mooz / xkeysnail

Yet another keyboard remapping tool for X environment
890 stars 112 forks source link

Defining device-specific modmaps? #148

Closed abhchand closed 2 years ago

abhchand commented 2 years ago

When I have an external keyboard plugged in, I have 2 inputs that xkeysnail correctly detects.

Screenshot from 2022-01-11 12-53-13

These keyboards have different configurations (specifically they swap super and alt keys).

Is there a way to define input-specific modmaps so that keys around bound to whatever device is sending the input? Currently I have to keep restarting xkeysnail with two different config files / modmaps based on what keyboard I'm using.

Thanks for building a fantastic tool!

abhchand commented 2 years ago

After reading through the code, it looks like this is possible with xkeysnail already! Although the feature seems to be undocumented.

Any lambda passed to define_conditional_modmap can accept a second parameter for the device_name.

In my case I set different key mappings based on the device Name in the screenshot above:

# Built-in Keyboard
define_conditional_modmap(lambda wm_class, device: device == 'AT Translated Set 2 keyboard', {

    Key.LEFT_CTRL:  Key.LEFT_META,
    Key.LEFT_META:  Key.LEFT_ALT,
    Key.LEFT_ALT:   Key.RIGHT_CTRL,
    Key.RIGHT_ALT:  Key.RIGHT_CTRL,
    Key.RIGHT_CTRL: Key.RIGHT_META,
    Key.RIGHT_META: Key.RIGHT_ALT,

    })

# Bluetooth Keyboard
define_conditional_modmap(lambda wm_class, device: device == 'Kinesis KB800MB-BT Keyboard', {

    Key.LEFT_CTRL:  Key.LEFT_META,
    Key.LEFT_ALT:   Key.LEFT_ALT,
    Key.LEFT_META:  Key.RIGHT_CTRL,
    Key.RIGHT_META: Key.RIGHT_CTRL,
    Key.RIGHT_ALT:  Key.RIGHT_META,
    Key.RIGHT_CTRL: Key.RIGHT_META,

    })