-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (133 loc) · 4.38 KB
/
main.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
import platform
currentPlatform = platform.system() # "Windows", "Linux", "OSX"?
import pyxhook, pyautogui, sys, subprocess, json
class Pahk:
def __init__(self, commandsFile="./default_commands.json", configFile="./default_configs.json"):
self.setConfigs(configFile)
self.setCommands(commandsFile)
self.stopEventListener = False
self.keysPressed = ""
self.keywordObject = {
"hotStrings" : self.hotStrings,
"hotKeys" : self.hotKeys
}
self.initHook()
def OnKeyPress(self, event):
print(event)
print(self.stopEventListener)
if self.isLocked():
return
if self.modifiers.has_key(event.Key):
self.modifiers[event.Key]['active'] = 1
return
self.updateKeysPressed(event, {})
self.handleHotStrings()
self.handleHotKeys(event)
if event.Key == 'Escape':
self.hook.cancel()
sys.exit("Exited safely")
def OnKeyRelease(self, event):
if self.isLocked():
return
if self.modifiers.has_key(event.Key):
self.modifiers[event.Key]['active'] = 0
def executeCommand(self, options):
p = subprocess.Popen(options.get('cmd'), stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, b''):
#print(line.rstrip())
pass
def updateKeysPressed(self, event, options):
# If on keyword begins with the received letter
foundMatchAtFirstChar = False
# If on keyword has a char that corresponds with the received letter at the length of the keysPressed
foundMatchAtCurrentChar = False
# If a matching hotkey is found
foundMatchingHotKey = False
for keywordObject in self.keywordObject:
for keywords in self.keywordObject[keywordObject]:
if keywordObject == "hotStrings":
if len(keywords) > len(self.keysPressed):
if len(keywords) > 0 and keywords[0] == chr(event.Ascii):
foundMatchAtFirstChar = True
if keywords[len(self.keysPressed)] == chr(event.Ascii):
foundMatchAtCurrentChar = True
if keywordObject == "hotKeys":
if keywords == event.Key:
foundMatchingHotKey = True
if foundMatchAtCurrentChar:
self.keysPressed += chr(event.Ascii)
else:
self.resetKeysPressed()
if foundMatchAtFirstChar:
self.keysPressed += chr(event.Ascii)
return {
"foundMatchAtFirstChar" : foundMatchAtFirstChar,
"foundMatchAtCurrentChar" : foundMatchAtCurrentChar,
"foundMatchingHotKey" : foundMatchingHotKey
}
def handleHotStrings(self):
print("\n - " + self.keysPressed + " - ")
hotString = self.hotStrings.get(self.keysPressed)
if hotString:
self.lock()
self.resetKeysPressed("", True)
self.handleActions(hotString["actions"])
self.unlock()
def handleHotKeys(self, event):
hotKey = self.hotKeys.get(event.Key)
if hotKey:
self.lock()
self.handleActions(hotKey["actions"])
self.unlock()
def handleActions(self, actions):
for action in actions :
if action["type"] == "cmd":
self.executeCommand({
'cmd' : action["args"]
})
elif action["type"] == "pyautogui":
getattr(pyautogui, action["function"])(*action["params"])
def resetKeysPressed(self, keysPressed="", cancelKeyStrokes=False):
if cancelKeyStrokes:
backspaces = []
for x in range(0, len(self.keysPressed)):
backspaces.append("backspace")
pyautogui.typewrite(backspaces)
self.keysPressed = keysPressed
def lock(self):
self.stopEventListener = True
def unlock(self):
self.stopEventListener = False
def isLocked(self):
return self.stopEventListener
def initHook(self):
self.hook = pyxhook.HookManager()
self.hook.KeyDown = self.OnKeyPress
self.hook.KeyUp = self.OnKeyRelease
self.hook.HookKeyboard()
self.hook.start()
def setConfigs(self, jsonFile):
with open(jsonFile) as file:
configs = json.load(file)
self.modifiers = configs.get('modifiers')
self.forceExitKeys = configs.get('forceExitKeys')
def setCommands(self, jsonFile):
with open(jsonFile) as file:
commands = json.load(file)
self.hotKeys = commands.get('hotKeys')
self.hotStrings = commands.get('hotStrings')
def main():
subprocess.call(["xhost", "+"])
if len(sys.argv) == 2:
print("Using '" + sys.argv[1] + "' command file.")
pahk = Pahk(sys.argv[1]);
elif len(sys.argv) == 3:
print("Using '" + sys.argv[1] + "' as command file.")
print("Using '" + sys.argv[2] + "' as config file.")
pahk = Pahk(sys.argv[1], sys.argv[2]);
else:
print("Using './default_commands.json' as command file.")
print("Using './default_configs.json' as config file.")
pahk = Pahk();
if __name__ == "__main__":
main()