-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisaka.pyw
154 lines (131 loc) · 4.13 KB
/
misaka.pyw
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
#coding=utf-8
#py2
from __future__ import division
range=xrange
from Tkinter import *
from ttk import *
import threading
import pyHook
import pythoncom
import time
tk=Tk()
tk.attributes('-toolwindow',True)
tk.attributes('-topmost',True)
tk.geometry('220x600')
tk.title('Misaka')
tk.columnconfigure(0,weight=1)
tk.columnconfigure(1,weight=1)
tk.rowconfigure(1,weight=1)
SPECIAL_KEYS={
'Lcontrol','Lmenu','Lwin','Rcontrol','Rmenu','Rwin',
'Escape','Snapshot','Home','End','Prior','Next','Return',
'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12',
}
MOD_KEYS={
'Lcontrol','Lmenu','Lwin','Rcontrol','Rmenu','Rwin',
}
SHIFT={'Lshift','Rshift'}
NICKNAME={
'Up': '↑', 'Down': '↓', 'Left': '←', 'Right': '→', 'Tab': '⇥',
'Return': '⏎', 'Space': '⎵', 'Back': '◁', 'Delete': '◀',
'Escape': 'Esc', 'Snapshot': 'PrtSc', 'Prior': 'PgUp', 'Next': 'PgDn',
'Lcontrol': 'Ctrl', 'Lmenu': 'Alt', 'Lwin': 'Win',
'Rcontrol': 'Ctrl', 'Rmenu': 'Alt', 'Rwin': 'Win',
'Oem_Minus': '-', 'Oem_Plus': '=', 'Oem_Comma': ',', 'Oem_Period': '.',
'Oem_5': '\\', 'Oem_3': '`', 'Oem_2': '/', 'Oem_1': ';', 'Oem_7': "'", 'Oem_4': '[', 'Oem_6': ']',
}
holdkey=set()
last_time=0
paused=False
current_id=None
status='idle'
buggy=False
def ti(*_):
t['state']='normal'
t.insert(*_)
t['state']='disabled'
def hooker():
def wrapper(f):
def sub(*__,**_):
try:
return f(*__,**_)
except:
global buggy
buggy=True
raise
return sub
hm=pyHook.HookManager()
hm.SubscribeKeyDown(wrapper(keydown))
hm.SubscribeKeyUp(wrapper(keyup))
hm.HookKeyboard()
while not buggy:
pythoncom.PumpWaitingMessages()
ti(END,u'键盘hook已退出\n','info')
def keydown(event):
def proc():
return NICKNAME.get(event.Key)
global status
global last_time
global current_id
holdkey.add(event.Key)
if paused or event.Key in SHIFT:
return True
if current_id!=event.Window:
current_id=event.Window
if status!='idle':
status='idle'
ti(END,'\n')
ti(END,u'\n✪ %s\n'%event.WindowName.decode('gbk','ignore'),'title')
if status=='string':
if event.Key in SPECIAL_KEYS or time.time()-last_time>5:
status='idle'
ti(END,'\n')
if status=='idle':
if event.Key in SPECIAL_KEYS:
status='modkey'
else:
status='string'
ti(END,'""','info')
if status=='string':
ti('end - 2 chars',proc() or (chr(event.Ascii) if event.Ascii else '⍰'),'string')
if event.Key=='Return':
status='idle'
ti(END,'\n')
else: #status=='modkey'
if any((s in holdkey for s in SHIFT)):
ti('end',' Shift ','modkey')
for s in SHIFT:
holdkey.discard(s)
ti('end',' %s '%(proc() or event.Key),'modkey' if event.Key in MOD_KEYS else 'key')
t.see('end - 2 chars')
last_time=time.time()
return True
def keyup(event):
global status
holdkey.discard(event.Key)
if status=='modkey' and not holdkey:
status='idle'
ti('end','\n')
return True
def pause(*_):
global paused
paused=not paused
pausebtn['text']='已暂停' if paused else '暂停'
def clear(*_):
t['state']='normal'
t.delete(1.0,'end - 2 chars linestart')
t['state']='disabled'
pausebtn=Button(tk,text='暂停',command=pause)
pausebtn.grid(row=0,column=0,sticky='we')
Button(tk,text='清空',command=clear).grid(row=0,column=1,sticky='we')
t=Text(tk,font='Consolas -18',state='disabled')
t.grid(row=1,column=0,columnspan=2,sticky='nswe')
t.tag_config('warning',foreground='#fff',background='#f00')
t.tag_config('string',foreground='#00f')
t.tag_config('info',foreground='#aaa')
t.tag_config('modkey',foreground='#000',background='#ff0')
t.tag_config('key',foreground='#fff',background='#444')
t.tag_config('title',foreground='#444',font='黑体 -12')
threading.Thread(target=hooker).start()
ti(1.0,'注意:输入密码时请暂停记录按键\n','warning')
mainloop()