【问题标题】:How to program hotstrings in python like in autohotkey如何像在 autohotkey 中一样在 python 中编写热字符串
【发布时间】:2021-04-23 01:54:06
【问题描述】:

我想在 python 中制作热字串,在经过一些处理后将输入的单词转换为另一个单词,因为 AHK 在确定要输入的单词时非常有限。现在,我在 ahk 中使用了一个热字符串,它在命令行上运行代码,该代码运行一个 python 脚本,其中包含我输入的单词作为参数。然后我使用 pyautogui 输入单词。但是,这非常慢,并且在快速打字时不起作用。我正在寻找一种方法来使用 python 和没有 ahk 来完成这一切,但我还没有找到在 python 中执行热字符串的方法。例如,每次我输入“test”这个词时,它都会用“testing”代替它。谢谢你的帮助。如果对任何人有用的话,我正在运行最新版本的 Python 和 Windows 10。

【问题讨论】:

  • “因为 AHK 在确定要键入的单词时非常有限” 我假设您只是不知道该怎么做。我怀疑您是否能想到在 Python 中可能的热字符串替换,但在 AHK 中却不行。您能否分享您尝试做的事情的 AHK 代码?
  • 为了选择要输入的单词,它将使用机器学习算法和从非常复杂的矩阵中进行选择,而这在 AHK 中是无法做到的。
  • 好的,那是新的。我想我无法评论这样做的可能性,因为我不知道这个机器学习算法是什么。也许一个值得注意的选项是在您的 AHK 脚本和您拥有的任何可以处理机器学习算法的东西之间进行通信。 OnMessage()documentation 显示了一种接收自定义消息的简单方式。
  • 感谢您的帮助。我会看看那个。不过,理想情况下,这一切都将在 Python 中完成,因为 Python 对于 NLP 来说要好得多,而且延迟会更少。

标签: python python-3.x autohotkey


【解决方案1】:

(如果您想在输入每个字母时处理它(t,te,tes,test),您应该编辑您的问题)

我使用 ahk 热键调用我的 SymPy 函数。我将 python 脚本注册为 COM 服务器并使用 ahk 加载它。
我没有注意到任何延迟。

你需要pywin32,但不要使用pip install pywin32下载
https://github.com/mhammond/pywin32/releases下载
否则它对 AutoHotkeyU64.exe 不起作用,它只对 AutoHotkeyU32.exe 起作用。
一定要下载amd64,(我下载的是pywin32-300.win-amd64-py3.8.exe)
原因如下:how to register a 64bit python COM server

toUppercase COM server.py

class BasicServer:
    # list of all method names exposed to COM
    _public_methods_ = ["toUppercase"]

    @staticmethod
    def toUppercase(string):
        return string.upper()
        
if __name__ == "__main__":
    import sys

    if len(sys.argv) < 2:
        print("Error: need to supply arg (""--register"" or ""--unregister"")")
        sys.exit(1)
    else:
        import win32com.server.register
        import win32com.server.exception

        # this server's CLSID
        # NEVER copy the following ID 
        # Use "print(pythoncom.CreateGuid())" to make a new one.
        myClsid="{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}"
        # this server's (user-friendly) program ID
        myProgID="Python.stringUppercaser"
        
        import ctypes
        def make_sure_is_admin():
            try:
                if ctypes.windll.shell32.IsUserAnAdmin():
                    return
            except:
                pass
            exit("YOU MUST RUN THIS AS ADMIN")
        
        if sys.argv[1] == "--register":
            make_sure_is_admin()
                
            import pythoncom
            import os.path
            realPath = os.path.realpath(__file__)
            dirName = os.path.dirname(realPath)
            nameOfThisFile = os.path.basename(realPath)
            nameNoExt = os.path.splitext(nameOfThisFile)[0]
            # stuff will be written here
            # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\${myClsid}
            # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}
            # and here
            # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\${myProgID}
            # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.stringUppercaser
            win32com.server.register.RegisterServer(
                clsid=myClsid,
                # I guess this is {fileNameNoExt}.{className}
                pythonInstString=nameNoExt + ".BasicServer", #toUppercase COM server.BasicServer
                progID=myProgID,
                # optional description
                desc="return uppercased string",
                #we only want the registry key LocalServer32
                #we DO NOT WANT InProcServer32: pythoncom39.dll, NO NO NO
                clsctx=pythoncom.CLSCTX_LOCAL_SERVER,
                #this is needed if this file isn't in PYTHONPATH: it tells regedit which directory this file is located
                #this will write HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}\PythonCOMPath : dirName
                addnPath=dirName,
            )
            print("Registered COM server.")
            # don't use UseCommandLine(), as it will write InProcServer32: pythoncom39.dll
            # win32com.server.register.UseCommandLine(BasicServer)
        elif sys.argv[1] == "--unregister":
            make_sure_is_admin()

            print("Starting to unregister...")

            win32com.server.register.UnregisterServer(myClsid, myProgID)

            print("Unregistered COM server.")
        else:
            print("Error: arg not recognized")

首先需要注册python COM服务器:
首先,获取您自己的 CLSID:只需使用 python shell。

import pythoncom
print(pythoncom.CreateGuid())

然后,将myClsid 设置为该输出

注册:
python "toUppercase COM server.py" --register
注销:
python "toUppercase COM server.py" --unregister

hotstring python toUppercase.ahk

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance, force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
SetBatchLines, -1
#KeyHistory 0
ListLines Off
#Persistent
#MaxThreadsPerHotkey 4

pythonComServer:=ComObjCreate("Python.stringUppercaser")
; OR
; pythonComServer:=ComObjCreate("{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}") ;use your own CLSID


; * do not wait for string to end
; C case sensitive
:*:hello world::

savedHotstring:=A_ThisHotkey

;theActualHotstring=savedHotstring[second colon:end of string]
theActualHotstring:=SubStr(savedHotstring, InStr(savedHotstring, ":",, 2) + 1)
send, % pythonComServer.toUppercase(theActualHotstring)


return



f3::Exitapp

你可以测试一下hotstringhello world的速度,对我来说非常快。
根据自己的喜好编辑def toUppercase(string):

【讨论】:

  • 感谢您的深入建议。但是,我遇到了第 11 行和第 13 行(具有 ComObjCreate 的行)的问题。当我执行代码时出现错误,“找不到指定的模块”。我在下面的 ahk 论坛 autohotkey.com/boards/viewtopic.php?t=29394 上找到了一些关于该主题的信息(ctrl+f “我的测试服务器的服务器代码是”以到达论坛中的正确位置)。但是,我对 ComObjCreate 不够熟悉,无法理解那里给出的解决方案。如果您知道此错误的解决方案,请告诉我。谢谢。
  • 你需要创建一个名为python COM server.py的文件,虽然你可以更改文件名,但你必须在任何地方更改它
    并将代码复制粘贴到里面
    然后INSIDE文件@987654336 @,将_reg_clsid_ 的值更改为您自己的CLSID,我告诉您如何生成
    然后运行使用命令行(我使用cmd)python "python COM server.py" --register
    您这样做了吗?成功了吗? (cmd应该说Registered: Python.stringUppercaser
  • 我不会编辑答案,我希望你编辑它,所以像你这样的人会理解。编辑不够清晰/明确的部分。我不知道如何解释它,你最清楚
    我的代码是从 [这个确切的地方][1] 得到的,我不得不稍微编辑一下代码。我应该在该线程上发布此代码以帮助像我这样的人... [1]:autohotkey.com/boards/viewtopic.php?t=29394
  • 感谢您的帮助。你的回答很明确。我按照说明操作了两次,仍然遇到同样的错误。我按照说明命名了程序,在 python 和 AHK 程序中生成并更改了 clsid,使用 cmd 成功注册了 python 程序(cd 到正确的文件夹位置),但我仍然收到该错误。我认为这可能是 AHK 脚本本身的问题,而不是与 python 程序有关,因为我在运行 AHK 程序时立即收到错误消息。也许您可以尝试您发送的确切代码以验证它是否有效。谢谢
  • 非常感谢,这对我有用!唯一的事情是我不得不注释掉import admin 和其他对管理员的引用。显然,它默认不是 Python 的一部分?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2019-06-06
  • 1970-01-01
相关资源
最近更新 更多