Python: Get caret position [closed]

纵然是瞬间 提交于 2021-02-19 16:35:54

问题


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?

Thanks Chris


回答1:


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


来源:https://stackoverflow.com/questions/19724360/python-get-caret-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!