-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboard.py
executable file
·72 lines (58 loc) · 1.54 KB
/
keyboard.py
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
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python2 -tt
import subprocess
# input keyevent 26 !! 26 turns off the screen!
# input keyevent 66 --> ENTER
# adb subprocess
adb = None
def main():
# Listen for user input including ENTER
print 'Input your text'
global adb
# Needs the stdout PIPE otherwise gets automatically redirected in my stdout
adb = subprocess.Popen(["adb", "shell"], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
try:
while(True):
process_input()
except KeyboardInterrupt: #Ctrl-C
print "\nCtrl^C received --> dying now"
adb.stdin.write("exit\n")
adb.terminate()
def process_input():
user_input = raw_input('')
# If input is empty or only is ENTER
if not user_input or user_input == "\n":
return
# Split sentence into words
words = user_input.split(' ');
# loop on all words
for word in words:
# Call adb shell input
send_word(word)
send_space()
send_eol_combo()
def send_eol_combo():
send_key(22) # Right to reach the Send button
send_enter() # ENTER to push the Send button
send_key(21) # Left to give the focus back to the text input
return
def send_space():
send_key(62)
def send_enter():
send_key(66)
# Key should be an int
def send_key(key):
adb.stdin.write("input keyevent %d\n" % key)
def send_word(text):
# Escape characters that adb doesn't like
escape_chars = {
"'": "\\'",
'"': '\\"',
"(": "\\(",
")": "\\)"
}
for key, value in escape_chars.iteritems():
text = text.replace(key, value)
# Send the escaped text to adb
adb.stdin.write("input text %s\n" % text)
if __name__ == '__main__':
main()