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
| #coding=utf-8
'''
Created on 2010-10-12
@author: lxd
'''
import wx
import win32con
import time
import threading
class WorkThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.timeToQuit = threading.Event()
self.timeToQuit.clear()
def stop(self):
self.timeToQuit.set()
def run(self):
while True:
if not self.timeToQuit.isSet():
print 'work'
time.sleep(1)
else:
break
class FrameWithHotKey(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.regHotKey()
self.Bind(wx.EVT_HOTKEY, self.OnHotKeyStart, id=self.hotKeyId_start)
self.Bind(wx.EVT_HOTKEY, self.OnHotKeyEnd, id=self.hotKeyId_end)
self.Bind(wx.EVT_HOTKEY, self.OnHotKeyQuit, id=self.hotKeyId_quit)
self.work = None
def regHotKey(self):
self.hotKeyId_start = 100
self.RegisterHotKey(self.hotKeyId_start, win32con.MOD_ALT, win32con.VK_F1)
self.hotKeyId_end = 101
self.RegisterHotKey(self.hotKeyId_end, win32con.MOD_ALT, win32con.VK_F2)
self.hotKeyId_quit = 102
self.RegisterHotKey(self.hotKeyId_quit, win32con.MOD_ALT, win32con.VK_F3)
def OnHotKeyStart(self, evt):
if not self.work:
self.work = WorkThread()
self.work.setDaemon(True)
self.work.start()
def OnHotKeyEnd(self, evt):
if self.work:
self.work.stop()
self.work = None
def OnHotKeyQuit(self, evt):
exit()
app = wx.App()
FrameWithHotKey(None)
app.MainLoop()
|