pimoroni / pmk-circuitpython

MIT License
88 stars 22 forks source link

How to define multiple keycodes in a keymap array #3

Open waynelx opened 2 years ago

waynelx commented 2 years ago

I tried out the advanced example but instead of single keys, I want to define multiple keycodes per key i.e Shift+Tab. Is there an example for that?

mikejf999 commented 2 years ago

Hi has anyone had an answer for this one:- 'I tried out the advanced example but instead of single keys, I want to define multiple keycodes per key i.e Shift+Tab. Is there an example for that?'

Thanks in advance

pangolingo commented 1 year ago

You could do this by altering the sample code to store lists of keycodes instead of single values. Then you can use the splat operator to unpack the list to arguments for keyboard.send.

An example (I did not test this) - see the first key which uses a group of keys.

keymap =    [[Keycode.LEFT_SHIFT, Keycode.TAB],
             [Keycode.ONE],
             [Keycode.TWO],
             [Keycode.THREE],
             [Keycode.FOUR],
             [Keycode.FIVE],
             [Keycode.SIX],
             [Keycode.SEVEN],
             [Keycode.EIGHT],
             [Keycode.NINE],
             [Keycode.A],
             [Keycode.B],
             [Keycode.C],
             [Keycode.D],
             [Keycode.E],
             [Keycode.F]]

for key in keys:
    @pmk.on_press(key)
    def press_handler(key):
        keycodes = keymap[key.number]
        keyboard.send(*keycodes)