-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydiablometer.py
147 lines (114 loc) · 4.39 KB
/
pydiablometer.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
import sys
import pyHook
import pythoncom
import Queue as queue
from multiprocessing import Process, Queue, freeze_support
from PySide.QtCore import QThread, Signal
from PySide.QtUiTools import QUiLoader
from PySide import QtGui
from PySide.QtCore import QFile, QSize
from PySide.QtGui import QLCDNumber
WINDOW_NAMES = ["Diablo III"]
class EventListener(Process):
"""
Catches event using pyHook
Can't get it working in anything but a different process
"""
def __init__(self, queue):
super(EventListener, self).__init__()
self.queue = queue
def on_event(self, event):
if event.WindowName in WINDOW_NAMES:
self.queue.put(event)
return True
def run(self):
hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(self.on_event)
hm.SubscribeKeyUp(self.on_event)
hm.HookMouse()
hm.HookKeyboard()
pythoncom.PumpMessages()
hm.UnhookMouse()
hm.HookKeyboard()
class EventProcessor(QThread):
"""
Gets the events from the EventListener process on a queue
"""
dataReady = Signal(object)
def __init__(self, queue):
super(EventProcessor, self).__init__()
self.queue = queue
self.running = True
def run(self):
while self.running:
try:
event = self.queue.get(timeout=1)
except queue.Empty:
pass
else:
self.dataReady.emit(event)
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.setWindowTitle("Diablo Meter")
size = QSize(530, 470)
self.resize(size)
# Load ui from gui definition file
try:
loader = QUiLoader()
file = QFile("widgets/widgets.ui")
file.open(QFile.ReadOnly)
mainWindow = loader.load(file, self)
file.close()
except:
self.warning_message("Error", "Can't find widgets definition file.\nIs the widgets folder in the same path as the executable?")
sys.exit()
#Reference to the components
self.left_click_lcd = mainWindow.findChild(QLCDNumber, "left_click_lcd")
self.right_click_lcd = mainWindow.findChild(QLCDNumber, "right_click_lcd")
self.action_keys_lcds = [mainWindow.findChild(QLCDNumber, "action_1_lcd"),
mainWindow.findChild(QLCDNumber, "action_2_lcd"),
mainWindow.findChild(QLCDNumber, "action_3_lcd"),
mainWindow.findChild(QLCDNumber, "action_4_lcd")]
options_menu = self.menuBar().addMenu("&Options")
options_menu.addAction(QtGui.QAction("&Restart Counters", self, triggered=self.restart_counters))
#Queue to share messsages to the message pupm on EventListener
q = Queue()
self.event_listener = EventListener(q)
self.event_listener.start()
self.thread = EventProcessor(q)
self.thread.dataReady.connect(self.on_hook_event)
self.thread.start()
#Key Names we are interested in. The order in the tuple
#determines to which action key it's mapped
self.key_maps = ("1", "2", "3", "4")
def restart_counters(self):
[lcd.display(0) for lcd in [self.left_click_lcd, self.right_click_lcd] + self.action_keys_lcds]
def on_hook_event(self, event):
if event.MessageName == "mouse left down":
self.left_click()
elif event.MessageName == "mouse right down":
self.right_click()
elif hasattr(event, "Key"):
self.key_event(event.Key)
def closeEvent(self, event):
self.event_listener.terminate()
if self.thread.isRunning():
self.thread.running = False
self.thread.wait()
def left_click(self):
self.left_click_lcd.display(self.left_click_lcd.value() + 1)
def right_click(self):
self.right_click_lcd.display(self.right_click_lcd.value() + 1)
def key_event(self, key):
if key in self.key_maps:
lcd = self.action_keys_lcds[self.key_maps.index(key)]
lcd.display(lcd.value() + 1)
def warning_message(self, title, message):
QtGui.QMessageBox.warning(self, title, message, QtGui.QMessageBox.Ok)
if __name__ == '__main__':
freeze_support()
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())