Python – Cell Phone Number Pad Input V2

This is a rewrite of my original post. This rewrite was made by Rami Davis [ramidavis at y a h o o .c o m] XDA Dev Forums.

It would go perfect with this:
http://www.flickr.com/photos/svofski/3383950702/in/pool-make

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
#using python3
 
import time
 
#Cellphone keyboard imitation
cell_keyboard = {
    "0" : (" "),
    "1" : ("!","@","#","$","%","^","&","*","(",")"),
    "2" : ("a","b","c","A","B","C"),
    "3" : ("d","e","f","D","E","F"),
    "4" : ("g","h","i","G","H","I"),
    "5" : ("j","k","l","J","K","K"),
    "6" : ("m","n","o","M","N","O"),
    "7" : ("p","q","r","s","P","Q","R","S"),
    "8" : ("t","u","v","T","U","V"),
    "9" : ("w","x","y","z","W","X","Y","Z"),
    "#" : (" "), #add something here
    "*" : (" ")
}
 
THRESHOLD = 1.0         # Constant: Seconds before resetting the keyboard
user_input = ""         # Current user input
last_key = ["", 0]         # Last key and repetitions.
 
print("Valid characters: 0-9 # * and q to quit")
 
while not user_input == "q":
    try:
        #Measure the time it takes to respond.
        response_time = time.time()
        user_input = input(">>")[0]
        response_time = time.time() - response_time
    except IndexError:
        user_input = ""
 
    #Check if it's valid
    if user_input in cell_keyboard:
 
        #If it matches the last key
        if user_input == last_key[0]:
 
            # And it was within threshold
            if response_time < THRESHOLD:
                last_key[1] += 1
            else:
                last_key[1] = 0
        else:
            # Assign the new key and 0 repetitions
            last_key[0] = user_input
            last_key[1] = 0
 
        #Now request the element on the keyboard.
        try:
            print(cell_keyboard[last_key[0]][last_key[1]])
        except IndexError:
            last_key[1] = 0
            print(cell_keyboard[last_key[0]][last_key[1]])
 
    else:
        print("Not a valid key.")

Thanks Rami!

Facebooktwittergoogle_pluspinterest

1 thought on “Python – Cell Phone Number Pad Input V2”

Comments are closed.