import ctypes
import pystray
from PIL import Image
import threading
# 获取当前大写锁定状态
def get_caps_lock_state():
return ctypes.windll.user32.GetKeyState(0x14) & 0xFFFF != 0
# 创建图标
def create_icon(color):
width, height = 64, 64
image = Image.new('RGB', (width, height), color)
return image
# 切换图标和通知
def toggle_icon(icon):
if get_caps_lock_state():
icon.icon = create_icon("red")
else:
icon.icon = create_icon("green")
# 切换大小写状态
def toggle_caps_lock(icon, item):
ctypes.windll.user32.keybd_event(0x14, 0x45, 0x1, 0)
ctypes.windll.user32.keybd_event(0x14, 0x45, 0x3, 0)
# 创建菜单
def create_menu():
menu = pystray.Menu(
pystray.MenuItem('切换', toggle_caps_lock),
pystray.MenuItem('退出', lambda: icon.stop()),
)
return menu
# 主函数
def main():
global icon
icon = pystray.Icon("caps_lock_icon", create_icon("red"))
icon.menu = create_menu()
toggle_icon(icon)
# 创建定时器,每秒钟检查一次大写锁定状态
timer = threading.Timer(0.3, caps_lock_timer, args=(icon,))
timer.start()
icon.run(setup)
# 设置函数
def setup(icon):
icon.visible = True
icon.on_click = toggle_caps_lock
# 定时器回调函数,用于定期检查大写锁定状态
def caps_lock_timer(icon):
toggle_icon(icon)
# 继续设置定时器,实现循环检查
timer = threading.Timer(0.3, caps_lock_timer, args=(icon,))
timer.start()
if __name__ == '__main__':
main()
import pystray
from PIL import Image
import threading
# 获取当前大写锁定状态
def get_caps_lock_state():
return ctypes.windll.user32.GetKeyState(0x14) & 0xFFFF != 0
# 创建图标
def create_icon(color):
width, height = 64, 64
image = Image.new('RGB', (width, height), color)
return image
# 切换图标和通知
def toggle_icon(icon):
if get_caps_lock_state():
icon.icon = create_icon("red")
else:
icon.icon = create_icon("green")
# 切换大小写状态
def toggle_caps_lock(icon, item):
ctypes.windll.user32.keybd_event(0x14, 0x45, 0x1, 0)
ctypes.windll.user32.keybd_event(0x14, 0x45, 0x3, 0)
# 创建菜单
def create_menu():
menu = pystray.Menu(
pystray.MenuItem('切换', toggle_caps_lock),
pystray.MenuItem('退出', lambda: icon.stop()),
)
return menu
# 主函数
def main():
global icon
icon = pystray.Icon("caps_lock_icon", create_icon("red"))
icon.menu = create_menu()
toggle_icon(icon)
# 创建定时器,每秒钟检查一次大写锁定状态
timer = threading.Timer(0.3, caps_lock_timer, args=(icon,))
timer.start()
icon.run(setup)
# 设置函数
def setup(icon):
icon.visible = True
icon.on_click = toggle_caps_lock
# 定时器回调函数,用于定期检查大写锁定状态
def caps_lock_timer(icon):
toggle_icon(icon)
# 继续设置定时器,实现循环检查
timer = threading.Timer(0.3, caps_lock_timer, args=(icon,))
timer.start()
if __name__ == '__main__':
main()