-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeycapture.py
More file actions
80 lines (69 loc) · 2.38 KB
/
keycapture.py
File metadata and controls
80 lines (69 loc) · 2.38 KB
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
import os
import pickle
import serial
import pyautogui as pyky
import tkinter as tk
import threading
from pynput import keyboard
COMPORT = "COM6"
keys = [
"q", "w" , "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\\",
"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'",
"z", "x", "c", "v", "b", "n", "m", ",", ".", "/",
"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "backspace",
"esc", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
"tab", "capslock", "shiftleft", "shiftright", "ctrlleft", "ctrlright", "win", "altleft", "altright","numlock","space",
"insert","home", "pageup", "delete", "end", "pagedown",
"up", "down", "left", "right",
"enter"
]
keymapFile = "./keymaps/keymap.dat"
mappedKeys = {}
def keyMapper():
mappingKeys = {}
com = serial.Serial(COMPORT, 9600)
for i in keys:
print("Press key for",i)
print("Mapped to =>", end ="")
keyData = com.readline()
if keyData:
decodeData = keyData.decode('utf-8')
print(decodeData)
mappingKeys[decodeData[0:-2]] = i
com.close()
return mappingKeys
def writeKeymap():
File = open(keymapFile, 'wb')
keymapDict = keyMapper()
pickle.dump(keymapDict, File)
File.close()
def listenToKeyboard():
com = serial.Serial(COMPORT, 9600)
while True:
keyData = com.readline()
if keyData:
decodeData = keyData.decode('utf-8')[0:-2]
if(decodeData in mappedKeys.keys()):
pyky.press(mappedKeys[decodeData])
else:
print("key is not mapped")
if __name__ == "__main__":
try:
com = serial.Serial(COMPORT, 9600)
print("Connecting to:", com.portstr)
com.close()
if os.path.exists(keymapFile):
print("Keymap Exist")
File = open(keymapFile, 'rb')
mappedKeys = pickle.load(File)
print(mappedKeys)
print("Keymapped Loaded")
listenToKeyboard()
else:
print("Unable to load Keymap")
print("Write keymapp")
writeKeymap()
print("Mapping is Done...")
print("Turning on Listner")
except Exception as e:
print(e)