Python:获取插入符号位置 [英] Python: Get caret position

查看:90
本文介绍了Python:获取插入符号位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 中获取插入符号位置.我尝试使用 win32gui.GetCaretPos() 但它总是返回 0,0.

I'm trying to get the caret position in Python. I tried using win32gui.GetCaretPos() but it always returns 0,0.

您有什么想法可以让它发挥作用吗?

Do you have any ideas how to make it work?

谢谢克里斯

推荐答案

如果插入符号在另一个线程创建的窗口中,则需要调用 AttachThreadInput.假设你想要前景窗口的插入符号,你可以像这样:

If the caret is in a window created by another thread, you need to call AttachThreadInput. Assuming you want the caret of the foreground window, you can get to it like this:

import win32gui
import win32process
import win32api

fg_win = win32gui.GetForegroundWindow()
fg_thread, fg_process = win32process.GetWindowThreadProcessId(fg_win)
current_thread = win32api.GetCurrentThreadId()
win32process.AttachThreadInput(current_thread, fg_thread, True)
try:
    print win32gui.GetCaretPos()
finally:
    win32process.AttachThreadInput(current_thread, fg_thread, False) #detach

这篇关于Python:获取插入符号位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆