# Kbd input processing with python

**URL:** <https://community.unix.com/t/kbd-input-processing-with-python/346948>\
**Category:** Shell Programming and Scripting\
**Created:** [October 6, 2014, 11:30am UTC](https://community.unix.com/t/kbd-input-processing-with-python/346948 "2014-10-06T11:30:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdohn](https://community.unix.com/letter_avatar/sdohn/32/6_22734026db12803ebb3e059622ac3534.png) [@sdohn](https://community.unix.com/u/sdohn)\
**Post date:** [October 6, 2014, 11:30am UTC](https://community.unix.com/t/kbd-input-processing-with-python/346948/1 "2014-10-06T11:30:30Z")

</div>

Hi, I'm new to python, and I work on a little program fpr my rapsberry pi.  
This scenario here is a little Midi sysex app for my Roland D-50 Synthesizer.  
With an usb attached number keypad I can type numbers from 0 to 9 and the referenced Midi sysex file would be send out to my Roland D-50 Synthesizer.  
This therefore works as expected. But now I want to improove my little python programm for the processing of 2 digits taken from the usb number keypad.

In theory the behavior should be as follow:  
We check for 2 digits first, If one enters 23 we do in a "elif content == "23":" block the magic. But if one has typed only 1 digit we trigger the elif block for 1 digit. Problem for me is, I don't know how to check for 1 or 2 digits in the code.

Here is my code:

```nohighlight
#!/usr/bin/env python
import select
import termios
import tty
import sys
import datetime
import os

class mylocontrol(object):
    def __init__ (self):
        self.checkkeys()

    def showcontent(self,content):
        if content == "1":
            os.system("/usr/bin/amidi -p hw:1,0,0 -s /work/MIDI/d50_00.syx") 
        elif content == "2":
            os.system("/usr/bin/amidi -p hw:1,0,0 -s /work/MIDI/PND50-02.syx")
        
    def getkey(self):
        old_settings = termios.tcgetattr(sys.stdin)
        tty.setraw(sys.stdin.fileno())
        select.select([sys.stdin], [], [], 0)
        answer = sys.stdin.read(1)
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
        return answer

    def checkkeys(self):
        while True:
            answer=self.getkey()
            if "|" in answer:
                break
            else:
                ret = self.showcontent(answer)

mylo = mylocontrol()

```

Maybe someone could help me out!

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_22734026db12803ebb3e059622ac3534.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [October 6, 2014, 11:34am UTC](https://community.unix.com/t/kbd-input-processing-with-python/346948/2 "2014-10-06T11:34:06Z")

</div>

> [@sdohn](#):
>
> Problem for me is, I don't know how to check for 1 or 2 digits in the code.

You have to wait for the ":" to tell the difference.
