-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyHookCodeFinal.py
executable file
·176 lines (147 loc) · 6.52 KB
/
pyHookCodeFinal.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import pyHook, serial, time, collections, csv, os
start_time = time.time()
bluetooth = serial.Serial("COM21", 9600)
def disconnect():
print("Bluetooth disconnected")
bluetooth.close()
def connect():
print("Bluetooth connected")
bluetooth.open()
key_record = []
time_record =[]
file_counter=0
recording = False
def OnKeyboardEvent(event):
global recording, start_time, key_record, time_record, file_counter, commands, times, lineCounter, bluetooth
elapsed_time = time.time() - start_time
print(event.KeyID)
if (event.KeyID == 13):
#Changes the state of recording with every button press
recording = not recording
#Resets the start time if recording is true
if (recording):
start_time = time.time()
print("Now recording to " + str(file_counter) + ".csv!")
return False
#Prints the lists and clears them when recording is turned off (by pressing enter)
else:
print("Saving now (PRESS ENTER BEFORE RECORDING AGAIN!!!)")
print("Keys pressed: " + str(key_record))
print("Time record: " + str(time_record))
#SAVE THE LISTS IN CSV FILE
#First save the two lists as a list of lists for writing in the CSV file
fileName = str(file_counter) + ".csv"
#pathFile = open("path" +str(file_counter) + ".csv", "wt")
pathFile = open(fileName, "wt")
file_counter +=1
if file_counter == 2:
file_counter = 0
writer = csv.writer(pathFile)
for x in range(len(time_record)):
key_record[x] = str(key_record[x])
time_record[x] = str(time_record[x])
#writer.writerow([str(key_record[x]), str(time_record[x])])
writer.writerow(key_record)
writer.writerow(time_record)
#Need to reconvert this time record to a float
# convert time record to float
key_record.clear()
time_record.clear()
print("This file was saved as: " + fileName)
return False
#If the key pressed is not enter and the current mode is recording,
elif (chr(event.Ascii) != '\r' and event.KeyID != 221 and event.KeyID != 162 and recording):
bluetooth.write(str(chr(event.Ascii)).encode())
print("Key: " + chr(event.Ascii) + " Time: " + str(elapsed_time))
key_record.append(chr(event.Ascii))
time_record.append(elapsed_time)
#Open serial port with = key
#elif(event.KeyID == 187):
#connect()
#Close serial port with - key
#elif(event.KeyID == 189):
#disconnect()
else:
bluetooth.write(str(chr(event.Ascii)).encode())
pass
#If 'Tab' is pressed...
if(event.KeyID == 9 and not recording):
#Load the array from the file (read)
timer = time.time()
eventIndex = 0
commands = [] #Need to empty the commands and times everytime tab is pressed
times = []
#Parse the csv here
#fileName = input("Which file would you like to open?") + ".csv"
fin = open("0.csv", 'r')
fileReader = csv.reader(fin)
lineCounter = 0
for line in fin:
line = line.strip()
if (lineCounter == 0):
print(line)
commands = line.split(",")
elif(lineCounter == 2):
print(line)
times = line.split(",")
for i in range(len(times)):
times[i] = float(times[i])
else:
pass
#print(commandList)
#print(lineCounter)
lineCounter += 1
print(commands)
print(times)
while(True):
if(eventIndex < len(commands)-1):
if(times[eventIndex] < time.time() - timer):
print("Command: " +commands[eventIndex] + " ||| Time: " + str(times[eventIndex])) #send event through bluetooth
#send event through bluetooth
#For some reason ignores the first two commands
bluetooth.write(str(commands[eventIndex]).encode())
eventIndex +=1
else:
bluetooth.write("m".encode())
break
#If ] is pressed
if(event.KeyID == 192 and not recording):
#Load the array from the file (read)
timer = time.time()
eventIndex = 0
commands = [] #Need to empty the commands and times everytime tab is pressed
times = []
#Parse the csv here
fin = open("1.csv", 'r')
fileReader = csv.reader(fin)
lineCounter = 0
for line in fin:
line = line.strip()
if (lineCounter == 0):
print(line)
commands = line.split(",")
elif(lineCounter == 2):
print(line)
times = line.split(",")
for i in range(len(times)):
times[i] = float(times[i])
else:
pass
lineCounter += 1
print(commands)
print(times)
while(True):
if(eventIndex < len(commands) -1):
if(times[eventIndex] < time.time() - timer):
print("Command: " +commands[eventIndex] + " ||| Time: " + str(times[eventIndex])) #send event through bluetooth
#send event through bluetooth
#For some reason ignores the first two commands
bluetooth.write(str(commands[eventIndex]).encode())
eventIndex +=1
else:
bluetooth.write("m".encode())
break
return True
hooks_manager = pyHook.HookManager ( )
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard ( )