吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2288|回复: 76
收起左侧

[Python 转载] 跟随鼠标的 大小写中文状态

  [复制链接]
暗夜硝烟 发表于 2026-3-16 15:37
本帖最后由 暗夜硝烟 于 2026-3-18 13:42 编辑

一个简单的小软件,找了好些类似要么设置比较多,也没看到带中文状态了。
给AI安排工作,让它写了一个。
运行后就跟随鼠标显示 输入是什么状态。没有设置,没有其他窗口。


screenshots.gif




使用发现有时候没有置顶,被其他窗口遮挡了,增加了更新置顶。

https://wwbhx.lanzout.com/iNPdJ3kwed4h
密码:dn7i





下面是源码,想修改或自己编译的随意哈!
[Python] 纯文本查看 复制代码
import tkinter as tk
import ctypes
from ctypes import wintypes
import threading
import time

class StatusWindow:
    def __init__(self):
        self.window = tk.Tk()
        self.window.overrideredirect(True)
        self.window.attributes('-topmost', True)
        self.window.attributes('-transparentcolor', 'magenta')
        self.window.configure(bg='magenta')
        
        # 你的原始参数
        self.window_size = 40
        self.window.geometry(f'{self.window_size}x{self.window_size}+0+0')
        self.window.resizable(False, False)
        
        # 背景色
        self.bg_color = '#E6F0FF'
        
        # 创建画布
        self.canvas = tk.Canvas(
            self.window,
            width=self.window_size,
            height=self.window_size,
            bg=self.bg_color,
            highlightthickness=0,
            bd=0
        )
        self.canvas.pack()
        self.canvas.pack_propagate(False)
        
        # 你的字体参数
        self.chinese_font = ('楷体', 26, 'bold')
        self.english_font = ('KaiTi', 28, 'bold')
        
        # 创建文字
        self.text_id = self.canvas.create_text(
            self.window_size//2,
            self.window_size//2,
            text='中',
            fill='#4CAF50',
            font=self.chinese_font,
            anchor='center'
        )
        
        # 状态变量
        self.current_text = '中'
        self.current_color = '#4CAF50'
        self.current_font = self.chinese_font
        self.running = True
        self.last_x = 0
        self.last_y = 0
        self.last_caps = 0
        self.last_ime = None
        
        # Windows API
        self.user32 = ctypes.windll.user32
        self.imm32 = ctypes.windll.imm32
        
        # 设置透明度
        self.set_transparency(180)
        
        # 启动
        self.start()
    
    def set_transparency(self, alpha):
        """设置透明度"""
        try:
            hwnd = self.user32.GetParent(self.window.winfo_id())
            style = self.user32.GetWindowLongW(hwnd, -20)
            self.user32.SetWindowLongW(hwnd, -20, style | 0x80000)
            self.user32.SetLayeredWindowAttributes(hwnd, 0, alpha, 0x00000002)
        except:
            pass
    
    def get_input_status(self):
        """获取输入法状态"""
        try:
            hwnd = self.user32.GetForegroundWindow()
            hIME = self.imm32.ImmGetDefaultIMEWnd(hwnd)
            
            if hIME:
                ime_status = self.user32.SendMessageW(hIME, 0x0283, 0x0005, 0)
            else:
                ime_status = 0
            
            caps_status = self.user32.GetKeyState(0x14) & 1
            
            return ime_status, caps_status
        except:
            return 0, 0
    
    def update_display(self):
        """更新显示"""
        ime, caps = self.get_input_status()
        
        if caps:
            display_text = 'A'
            text_color = '#FF9800'
            font = self.english_font
        else:
            if ime == 1:
                display_text = '中'
                text_color = '#4CAF50'
                font = self.chinese_font
            else:
                display_text = '英'
                text_color = '#2196F3'
                font = self.english_font
        
        if (display_text != self.current_text or 
            text_color != self.current_color or 
            font != self.current_font):
            
            self.canvas.itemconfig(
                self.text_id,
                text=display_text,
                fill=text_color,
                font=font
            )
            
            self.current_text = display_text
            self.current_color = text_color
            self.current_font = font
    
    def key_monitor(self):
        """按键检测 - 立即响应 (1ms轮询)"""
        ime, self.last_caps = self.get_input_status()
        self.last_ime = ime
        
        while self.running:
            try:
                ime, caps = self.get_input_status()
                
                if caps != self.last_caps or ime != self.last_ime:
                    self.window.after(1, self.update_display)
                    self.last_caps = caps
                    self.last_ime = ime
                
                time.sleep(0.001)  # 1ms轮询
            except:
                time.sleep(0.01)
    
    def timer_monitor(self):
        """定时检测 - 每秒更新一次(确保同步)"""
        while self.running:
            try:
                time.sleep(1)  # 每秒检测一次
                self.window.after(1, self.update_display)
            except:
                time.sleep(1)
    
    def follow_mouse(self):
        """鼠标跟随 - 无延迟"""
        class POINT(ctypes.Structure):
            _fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
        
        point = POINT()
        
        while self.running:
            try:
                if self.user32.GetCursorPos(ctypes.byref(point)):
                    x = point.x + 25
                    y = point.y + 20
                    self.window.geometry(f'+{x}+{y}')
                
                time.sleep(0.001)
            except:
                time.sleep(0.01)
    
    def topmost_enhancer(self):
        """增强置顶效果:每0.5秒提升窗口一次,确保不被其他置顶窗口遮挡"""
        while self.running:
            try:
                time.sleep(0.5)
                self.window.after(0, self.window.lift)
            except:
                time.sleep(0.5)
    
    def start(self):
        """启动所有检测"""
        self.update_display()
        
        # 按键检测线程 (1ms)
        threading.Thread(target=self.key_monitor, daemon=True).start()
        
        # 定时检测线程 (1秒)
        threading.Thread(target=self.timer_monitor, daemon=True).start()
        
        # 鼠标跟随线程
        threading.Thread(target=self.follow_mouse, daemon=True).start()
        
        # 新增:置顶增强线程
        threading.Thread(target=self.topmost_enhancer, daemon=True).start()
    
    def run(self):
        """运行"""
        try:
            self.window.mainloop()
        except KeyboardInterrupt:
            self.running = False
            self.window.destroy()

if __name__ == '__main__':
    # 进程守护:自动重启
    while True:
        try:
            app = StatusWindow()
            app.run()
        except Exception as e:
            print(f"程序异常退出: {e},5秒后重启...")
            time.sleep(5)
        except KeyboardInterrupt:
            print("用户中断,退出程序")
            break
        finally:
            # 确保旧实例的线程能退出
            try:
                app.running = False
            except:
                pass
            time.sleep(1)



免费评分

参与人数 10吾爱币 +11 热心值 +5 收起 理由
pyjiujiu + 1 用心讨论,共获提升!
yuanhd2 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
justfate + 1 + 1 用心讨论,共获提升!
yanglinman + 1 谢谢@Thanks!
cydlongzhe + 1 + 1 谢谢@Thanks!
DarkStar7 + 1 我很赞同!
RobinMaas + 2 用心讨论,共获提升!
ABuSiDeLuoYin + 1 + 1 谢谢@Thanks!
THMZ + 1 鼓励转贴优秀软件安全工具和文档!
wjbg2022 + 1 + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wjbg2022 发表于 2026-3-16 15:57
这玩意,好像没有退出窗口了
N2Yan 发表于 2026-3-16 16:02
感谢分享,正需要这东西。之前github看到一个,自己配置起来不太好用。试一下这个

https://github.com/abgox/InputTip
兮兮曦 发表于 2026-4-2 10:44
本帖最后由 兮兮曦 于 2026-4-30 10:32 编辑

04.gif
让AI改的可以系统托盘显示、调节透明度、更改大小、位置等,可能有bug,就是文件有些大。
https://wudongqingcheng.lanzouq.com/b014x8ggza
密码:gjqw
jmlcx123 发表于 2026-3-16 15:44
感谢分享,学习一下。
benmagic 发表于 2026-3-16 15:52
这个怎么用,看输入法有没切换吗
7R903 发表于 2026-3-16 15:52
学习一下
 楼主| 暗夜硝烟 发表于 2026-3-16 15:54
benmagic 发表于 2026-3-16 15:52
这个怎么用,看输入法有没切换吗

是的呢!
aloser1 发表于 2026-3-16 15:56
感觉简中可以用红中,繁中可以用发财,英语可以用白板
beoimlife 发表于 2026-3-16 15:59
感谢分享,提供了新思路和代码!
wilist 发表于 2026-3-16 16:20
切换英文状态没跟随显示,中文和大写有。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-5-6 20:17

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表