Hey,

I want to write a small application to convert key presses to characters like what happens when you're writing an SMS. For this, i wrote a quick script that converts an array of numbers back to characters using a keyMap. The following is the code:

Code:
import sys
 
keyMap = {
    2: ['a', 'b', 'c'],
    3: ['d', 'e', 'f'],
    4: ['g', 'h', 'i'],
    5: ['j', 'k', 'l'],
    6: ['m', 'n', 'o'],
    7: ['p', 'q', 'r', 's'],
    8: ['t', 'u', 'v'],
    9: ['w', 'x', 'y', 'z']
    }

#                  h     e        y 
#keysPressed = [4, 4, 3, 3, 9, 9, 9]
#                 h     e        l        l        o
keysPressed = [4, 4, 3, 3, 5, 5, 5, 5, 5, 5, 6, 6, 6] 

prevkey = ''
counter = 0
counter2 = 0;
for key in keysPressed:
        if (prevkey == key):
                counter += 1
        else:
                if (prevkey != ''):
                        sys.stdout.write(keyMap[prevkey][counter - 3])
                counter = 0
                prevkey = key
 
print keyMap[prevkey][counter]
However, this doesn't handle double characters i.e. 555 / 555 which should be "ll" and not 5 / 55 / 555 etc = 'j', 'k', 'l'. Perhaps I should use some timeout feature (e.g if flightTime > datetime.timedelta(seconds=1))?

Thanks,

A