本帖最后由 tuoluo348 于 2024-5-20 20:07 编辑
以前需要老板键功能,百度下载了好几个 还中了病毒 后来我就用python写了一个
在列表选择要隐藏的窗口后点击下面的选中按钮
F7显示/隐藏 F12退出软件
添加自定义快捷键 支持组合键(ctrl,alt,shift)
输入自定义快捷键后点击设置快捷键按钮 然后选择要隐藏的窗口 点击选中按钮
[Python] 纯文本查看 复制代码 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]
# 创建Tkinter窗口
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()
下载地址:https://wwf.lanzouq.com/ipHw71zagoyb |