python - 如何从事件窗口中检索选定的文本

标签 python windows winapi pywin32

我正在尝试使用 Python 为窗口创建一个简单的开源实用程序,它可以对当前事件窗口的选定文本执行用户定义的操作。该实用程序应使用预定义的键盘快捷键激活。

以下示例部分概述了用法:

  1. 用户使用鼠标或键盘选择一些文本(在任何应用程序窗口中)
  2. 用户按下预定义的键盘快捷键
  3. 所选文本由我们的实用程序检索或复制到剪贴板(这两种方法都应该没问题)
  4. 对所选文本执行依赖于键盘快捷键的操作

令我困惑的是第 3 步如何从事件窗口中检索选定的文本。这应该适用于所有应用程序。

我使用的是 pywin32 模块。

预先感谢您的回答和提示。

更新#1:

事实证明,有两种方法可以完成任务:

  1. 找到事件窗口,然后向其发送消息/击键 (Ctrl-C),以便将所选文本复制到剪贴板。然后该实用程序可以通过使用剪贴板相关功能访问文本来处理文本。
  2. 找到事件窗口,然后直接检索选定的文本(不将其复制到剪贴板)。这似乎比第一种方法更难。

作为起点:

获取事件窗口 ID,正如 Anurag Uniyal 在他的 reply 中指出的那样.

或者用下面的代码获取窗口对象:

import win32ui
wnd = win32ui.GetForegroundWindow()
print wnd.GetWindowText()

最佳答案

下面的代码只适用于简单的文本框(只是在 VB6 中完成,并移植到 python)

编辑:仅在 python 2.6 上测试过

from ctypes import *
import win32gui
import win32api
import win32con


user32 = windll.user32
kernel32 = windll.kernel32

class RECT(Structure):
 _fields_ = [
     ("left", c_ulong),
     ("top", c_ulong),
     ("right", c_ulong),
     ("bottom", c_ulong)
 ]

class GUITHREADINFO(Structure):
 _fields_ = [
     ("cbSize", c_ulong),
     ("flags", c_ulong),
     ("hwndActive", c_ulong),
     ("hwndFocus", c_ulong),
     ("hwndCapture", c_ulong),
     ("hwndMenuOwner", c_ulong),
     ("hwndMoveSize", c_ulong),
     ("hwndCaret", c_ulong),
     ("rcCaret", RECT)
 ]



def get_selected_text_from_front_window(): # As String
    ''' vb6 to python translation '''

    gui = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
    txt=''
    ast_Clipboard_Obj=None
    Last_Clipboard_Temp = -1


    user32.GetGUIThreadInfo(0, byref(gui))

    txt = GetCaretWindowText(gui.hwndCaret, True)

    '''
    if Txt = "" Then
        LastClipboardClip = ""
        Last_Clipboard_Obj = GetClipboard
        Last_Clipboard_Temp = LastClipboardFormat
        SendKeys "^(c)"
        GetClipboard
        Txt = LastClipboardClip
        if LastClipboardClip <> "" Then Txt = LastClipboardClip
        RestoreClipboard Last_Clipboard_Obj, Last_Clipboard_Temp
        print "clbrd: " + Txt
    End If
    '''    
    return txt



def GetCaretWindowText(hWndCaret, Selected = False): # As String

    startpos =0
    endpos =0

    txt = ""

    if hWndCaret:

        buf_size = 1 + win32gui.SendMessage(hWndCaret, win32con.WM_GETTEXTLENGTH, 0, 0)
        if buf_size:
            buffer = win32gui.PyMakeBuffer(buf_size)
            win32gui.SendMessage(hWndCaret, win32con.WM_GETTEXT, buf_size, buffer)
            txt = buffer[:buf_size]

        if Selected and buf_size:
            selinfo  = win32gui.SendMessage(hWndCaret, win32con.EM_GETSEL, 0, 0)
            endpos   = win32api.HIWORD(selinfo)
            startpos = win32api.LOWORD(selinfo)
            return txt[startpos: endpos]

    return txt

if __name__ == '__main__':
    print get_selected_text_from_front_window()

关于python - 如何从事件窗口中检索选定的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007185/

相关文章:

Python 套接字错误 : [Errno 104] Connection reset by peer

c# - 在 C# 中使用 Socket 改进 TCP 转发器

windows - CertCreateCertificateContext 返回 CRYPT_E_ASN1_BADTAG/8009310b

c++ - ConvertStringSecurityDescriptorToSecurityDescriptor() 阻止我的程序正常结束

python - 组合/合并排序的 pandas 数据框中的时间间隔行

python - 使用python根据变量正确打印字符

python - 为什么我的查询变成 bool 值,如何防止这种情况发生,以便我可以迭代它?

windows - 将环境变量从命令行传递给 yarn

windows - "conda: command not found",Windows 上的 Bash

c++ - 如何从另一个登录用户启动登录用户内部的应用程序?