How to program hotstrings in python like in autohotkey

和自甴很熟 提交于 2021-01-29 06:11:46

问题


I want to make hotstrings in python that converts one word when typed into another after some processing, since AHK is very limiting when it comes to determining which word to type. Right now, I am using a hotstring in ahk that runs code on the command line that runs a python script with the word that I typed as arguments. Then I use pyautogui to type the word. However, this is very slow and does not work when typing at speed. I'm looking for a way to do this all with python and without ahk, but I have not found a way to do hotstrings in python. For example, every time I type the word "test" it replaces it with "testing." Thanks for your help. I'm running the latest version of Python and Windows 10 if that is useful to anyone by the way.


回答1:


(if you want to process it as each letter is typed(t,te,tes,test), you should edit your question)

I call my SymPy functions using ahk hotkeys. I register the python script as a COM server and load it using ahk.
I do not notice any latency.

you'll need pywin32, but don't download using pip install pywin32
download from https://github.com/mhammond/pywin32/releases
OR ELSE IT WON'T WORK for AutoHotkeyU64.exe, it will only work for AutoHotkeyU32.exe.
make sure to download amd64, (I downloaded pywin32-300.win-amd64-py3.8.exe)

python COM server.py

class BasicServer:
    # note - these are comma-delimited
    # list of all method names exposed to COM
    _public_methods_ = ["toUppercase"]

    # list of all read-only exposed attributes
    # _readonly_attrs_ = ["transformations"]    

    # this server's CLS ID
    # NEVER copy the following ID 
    # Use "import pythoncom" first
    # Use "print(pythoncom.CreateGuid())" to make a new one.
    _reg_clsid_ = "{C70F3BF7-2947-4F87-B31E-9F5B8B13D24F}"

    # this server's (user-friendly) program ID
    _reg_progid_ = "Python.stringUppercaser"

    # optional description
    _reg_desc_ = "return uppercased string"

    # public_methods

    @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

        if sys.argv[1] == "--register":
            win32com.server.register.UseCommandLine(BasicServer)
        elif sys.argv[1] == "--unregister":
            import admin

            print("Starting to unregister...")

            # if not admin.isUserAdmin():
                # admin.runAsAdmin()
            win32com.server.register.UnregisterServer(BasicServer._reg_clsid_, BasicServer._reg_progid_)

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

you first need to register the python COM server:
first, get your own CLSID: just use a python shell.

import pythoncom
print(pythoncom.CreateGuid())

then, set your _reg_clsid_ to that output

to register:
python "python COM server.py" --register
to unregister:
python "python COM server.py" --unregister

use python COM server stringUppercaser.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("{C2474B5A-5E9D-484D-BDFD-20A100183426}") ;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

you can test the speed of hotstring hello world, it's very fast for me.
Edit def toUppercase(string): to your liking



来源:https://stackoverflow.com/questions/65780086/how-to-program-hotstrings-in-python-like-in-autohotkey

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