Kbd input processing with python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kbd input processing with python
# 1  
Old 10-06-2014
Kbd input processing with python

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:

Code:
#!/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!
# 2  
Old 10-06-2014
Quote:
Originally Posted by 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python problem with input

Hello everyone, I have just started python, and there is something that escapes me. I don't understand why my little script doesn't work. When I do, it work: my_string = "bonjour" if len(my_string) < "6": print(my_string + " < 6") else: print(my_string + " > 6") ... (2 Replies)
Discussion started by: Arnaudh78
2 Replies

2. Shell Programming and Scripting

If condition after raw input in python

Hi, I have a python script which is completely interactive. I have almost 10+ raw input statements like below which are collected from user one by one. req_number = raw_input("Please enter request number or Q to Quit: ") if (req_number in ): sys.exit() Is it possible for a more... (6 Replies)
Discussion started by: ctrld
6 Replies

3. Shell Programming and Scripting

Reducing the decimal points of numbers (3d coordinates) in a file; how to input data to e.g. Python

I have a file full of coordinates of the form: 37.68899917602539 58.07500076293945 57.79100036621094 The numbers don't always have the same number of decimal points. I need to reduce the decimal points of all the numbers (there are 128 rows of 3 numbers) to 2. I have tried to do this... (2 Replies)
Discussion started by: crunchgargoyle
2 Replies

4. Shell Programming and Scripting

[Python]StringVar() Class String processing

I have a requirement where I collect inputs from entry widget in gui(via variables a,b,c) and then output a string like this: -a a -b b -c c. Hence if I have given a=1 b=2 c=3, The output string is --> -a 1 -b 2 -c 3 or if I have given a=1 b=2 (c left blank) then the output is --> -a 1... (1 Reply)
Discussion started by: animesharma
1 Replies

5. Solaris

Processing user input in .sh file

Hi, I am new to shell scripting. script should accept the user value and then compare that value with the null. If null then assign the value "*" to the variable else will use the user inputed value. How to do this ? With Regards (3 Replies)
Discussion started by: milink
3 Replies

6. Shell Programming and Scripting

giving input to a python wch has been called frm shell script....urgent

i'm calling a python script from shell script. the python needs Y as an input everytime. how can i giv it thru shell script. I tried below code for arg in `cat erd_gen_list.lst` do generateErdSql.py -S $arg << Y done This is giving me err : `<<' unmatched pls help. (1 Reply)
Discussion started by: vini
1 Replies
Login or Register to Ask a Question