# Key combinations on keybow

**URL:** <https://forums.pimoroni.com/t/key-combinations-on-keybow/15948>\
**Category:** Support\
**Created:** [January 21, 2021, 6:33pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948 "2021-01-21T18:33:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![scotts](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@scotts](https://forums.pimoroni.com/u/scotts)\
**Post date:** [January 21, 2021, 6:33pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/1 "2021-01-21T18:33:01Z")

</div>

Searched around found some related questions but most of the threads were stale of incomplete.

Is there method without directly accessing the GPIO pins to configure actions based on key combinations not just singe key states.

Some background:  
I’m currently using the pimoroni keybow-python libraries on a pi zero and the 3 key keybow to make requests.get and requests.post calls to another pi zero then set the LEDs based on a second GET/POST on the release of the key. I have it mostly working for the 3 functions needed but would like to add a reset and shut off options via key 0&1 and 1&2 respectively. Looking at both keybow-firmware and keybow-python I do not see a method to do this in a reliable way.

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [January 21, 2021, 7:24pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/2 "2021-01-21T19:24:50Z")

</div>

Have a look at this and see if it helps.  
[Using macros and snippets with Keybow - Pimoroni Yarr-niversity](https://learn.pimoroni.com/tutorial/sandyj/using-macros-and-snippets-with-keybow)

---

<div class="post-metadata">

**Author:** ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)\
**Post date:** [January 21, 2021, 7:53pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/3 "2021-01-21T19:53:56Z")

</div>

My guess would be creating some variables to store whether the keybow buttons are currently pressed.

This script seems to done something similar so may be of some help:

> <https://github.com/pimoroni/keybow-firmware/blob/master/sdcard/layouts/mouse.lua>

---

<div class="post-metadata">

**Author:** ![alphanumeric](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/alphanumeric/32/2729_2.png) [@alphanumeric](https://forums.pimoroni.com/u/alphanumeric)\
**Post date:** [January 21, 2021, 8:09pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/4 "2021-01-21T20:09:05Z")

</div>

I don’t own a Keybow just so you know, I just know my way around the Pimoroni resources and try to help out when I can. ;)  
There are quite a few Keybow owners so you may get somebody that knows chiming in at some point.

---

<div class="post-metadata">

**Author:** ![scotts](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@scotts](https://forums.pimoroni.com/u/scotts)\
**Post date:** [January 21, 2021, 9:17pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/5 "2021-01-21T21:17:38Z")

</div>

Thanks to both @alphanumeric and @major_tomm. Just re-read both the links and still do not see a way short of directly polling the pins in a separate process.

But it did get me thinking of an alternate option using some diodes to create logical switches, making my 3 key mini appear to be a 12 key keybow with 6 missing keys but going to hold off a while before I go down that path.

---

<div class="post-metadata">

**Author:** ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)\
**Post date:** [January 21, 2021, 9:25pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/6 "2021-01-21T21:25:31Z")

</div>

My assumption was that you’d be able to do something like this

```auto
zero_pressed = false
one_pressed = false
two_pressed = false

function handle_key_00(pressed)
    zero_pressed = pressed
    if pressed and one_pressed then
        -- reset
    elseif pressed then
        -- do A
    end
end

function handle_key_01(pressed)
    one_pressed = pressed
    if pressed and zero_pressed then
        -- reset
    elseif pressed and two_pressed then
        -- shutdown
    elseif pressed then
        -- do B
    end
end

function handle_key_02(pressed)
    two_pressed = pressed
    if pressed and one_pressed then
        -- shutdown
    elseif pressed then
        -- do C
    end
end

```

But it’s probably further complicated by the fact you’re doing actions on press and release.

Edit: Apologies for the Lua, I now notice you mention the python-library but the concept should be the same.

---

<div class="post-metadata">

**Author:** ![major\_tomm](https://sea2.discourse-cdn.com/flex016/user_avatar/forums.pimoroni.com/major_tomm/32/9044_2.png) [@major\_tomm](https://forums.pimoroni.com/u/major_tomm)\
**Post date:** [January 21, 2021, 9:41pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/7 "2021-01-21T21:41:31Z")

</div>

Looking further into the python library [itself](https://github.com/pimoroni/keybow-python/blob/master/library/keybow/ __init__.py) it just seems to use the regular `RPi.GPIO` to assign events to the pins.

Looking at the API, it should just be a matter of doing: `GPIO.input(channel)` to poll the current state of a pin, with the pin numbers defined by `MINI` earlier in the file.

---

<div class="post-metadata">

**Author:** ![scotts](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@scotts](https://forums.pimoroni.com/u/scotts)\
**Post date:** [January 24, 2021, 10:58pm UTC](https://forums.pimoroni.com/t/key-combinations-on-keybow/15948/8 "2021-01-24T22:58:20Z")

</div>

I have a functional working concept, it is dirty but it works for the most part. So far the biggest leap has been the when taking to a coworker about the best way to do a lookup table in python was not to but treat the keys as binary registers. Not sure if I will look at using the API for just controlling the LEDs or trying to bolt things on to the API but ether way. I’ve got some python learning to do. I may just forgo using the keybow.on functionality at all on just record keystrokes from the pins until all are true then output a value.

This started with me just looking at the keys.py in the example directory with no real plan on the process or logic I would use so some of the names are kinda random.

```
#!/usr/bin/env python
import keybow
import time
import operator

keybow.setup(keybow.MINI)
multikey = {}
multikeyoutput = {}
#print(keybow.pins)

def keycounter():
    global multikeyoutput
    buttons_list = []
    compoundkey = None
    for key in multikeyoutput.keys():
        buttons_list.append (int(multikeyoutput.get(key)))
# print(buttons_list)
    compoundkey = "".join(str(int((button))) for button in buttons_list)
    print("KeyCode: {}".format (int((compoundkey),2)))
    multikeyoutput = {}
    multikey = {}
    button = None

@keybow.on()
def handle_key(index, state):
    global multikey
    global multikeyoutput
    for i in keybow.pins:
# print("i is: {}".format (i))
      keystatus = 0
      keystatus = int(operator.not_(keybow.GPIO.input(i)))
      multikey[keybow.pins.index(i)] = int(bool(multikey.get(keybow.pins.index(i), 0) + keystatus) == True)

    if state:
        keybow.set_led(index, 255, 0, 0)
        print("MultiKey while keys are down: {}".format (multikey))
        multikeyoutput = multikey.copy()
        multikey = {}
        keystatus = None

    else:
        keybow.set_led(index, 0, 0, 0)
        
while True:
    keybow.show()
    if bool(multikeyoutput):
        keycounter()
# else:

    time.sleep(1.0 / 60.0)
```
