import
win32gui
import
win32con
import
time
from
tkinter
import
*
import
pyWinhook as PyHook3
import
pythoncom
lst
=
[]
hwnd_title
=
dict
()
key_states
=
{
"Ctrl"
:
False
,
"Alt"
:
False
,
"Shift"
:
False
}
toggle_key
=
"F7"
exit_key
=
"F12"
def
get_all_hwnd(hwnd, mouse):
if
win32gui.IsWindow(hwnd)
and
win32gui.IsWindowEnabled(hwnd)
and
win32gui.IsWindowVisible(hwnd):
hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)})
def
close_windows(aimLists):
win32gui.ShowWindow(
int
(aimLists), win32con.SW_HIDE)
def
open_windows(aimLists):
win32gui.ShowWindow(
int
(aimLists), win32con.SW_SHOW)
n
=
0
def
onKeyboardEvent(event):
global
n, toggle_key, exit_key
key
=
event.Key
if
key
=
=
"Lcontrol"
or
key
=
=
"Rcontrol"
:
key_states[
"Ctrl"
]
=
event.MessageName
=
=
"key down"
elif
key
=
=
"Lmenu"
or
key
=
=
"Rmenu"
:
key_states[
"Alt"
]
=
event.MessageName
=
=
"key down"
elif
key
=
=
"Lshift"
or
key
=
=
"Rshift"
:
key_states[
"Shift"
]
=
event.MessageName
=
=
"key down"
if
event.MessageName
=
=
"key down"
:
if
check_key_combination(event, toggle_key):
n
+
=
1
if
n
%
2
!
=
0
:
for
i
in
range
(
len
(lst)):
close_windows(lst[i].split(
','
)[
0
])
else
:
for
i
in
range
(
len
(lst)):
open_windows(lst[i].split(
','
)[
0
])
if
check_key_combination(event, exit_key):
win32gui.PostQuitMessage(
0
)
return
True
def
check_key_combination(event, key_combination):
keys
=
key_combination.lower().split(
"+"
)
if
len
(keys)
=
=
1
:
return
event.Key.lower()
=
=
keys[
0
]
elif
len
(keys)
=
=
2
:
return
key_states[keys[
0
].capitalize()]
and
event.Key.lower()
=
=
keys[
1
]
elif
len
(keys)
=
=
3
:
return
key_states[keys[
0
].capitalize()]
and
key_states[keys[
1
].capitalize()]
and
event.Key.lower()
=
=
keys[
2
]
return
False
def
select_windows():
for
i
in
range
(listb.size()):
if
listb.select_includes(i):
lst.append(listb.get(i))
root.destroy()
def
set_shortcuts():
global
toggle_key, exit_key
toggle_key
=
entry_toggle_key.get()
exit_key
=
entry_exit_key.get()
win32gui.EnumWindows(get_all_hwnd,
0
)
for
key
in
list
(hwnd_title):
if
hwnd_title[key]
=
=
"":
del
hwnd_title[key]
root
=
Tk()
root.geometry(
"600x400"
)
listb
=
Listbox(root, selectmode
=
MULTIPLE, width
=
80
, height
=
10
)
for
i, j
in
hwnd_title.items():
if
j !
=
"":
listb.insert(
"end"
,
str
(i)
+
','
+
j)
listb.pack()
button_select
=
Button(root, text
=
"选中"
, command
=
select_windows)
button_select.pack()
label_toggle_key
=
Label(root, text
=
"显示/隐藏的快捷键(ctrl,alt,shift)"
)
label_toggle_key.pack()
entry_toggle_key
=
Entry(root)
entry_toggle_key.pack()
entry_toggle_key.insert(
0
,
"F7"
)
label_exit_key
=
Label(root, text
=
"退出的快捷键(ctrl,alt,shift)"
)
label_exit_key.pack()
entry_exit_key
=
Entry(root)
entry_exit_key.pack()
entry_exit_key.insert(
0
,
"F12"
)
button_set_shortcuts
=
Button(root, text
=
"设置快捷键"
, command
=
set_shortcuts)
button_set_shortcuts.pack()
hm
=
PyHook3.HookManager()
hm.KeyDown
=
onKeyboardEvent
hm.KeyUp
=
onKeyboardEvent
hm.HookKeyboard()
root.mainloop()
pythoncom.PumpMessages()