ahk的python包装器
ahk的Python项目详细描述
一个python包装器啊。
安装
pip install ahk
需要python 3.6+
另请参见非python依赖项
用法
fromahkimportAHKahk=AHK()ahk.mouse_move(x=100,y=100,blocking=True)# Blocks until mouse finishes moving (the default)ahk.mouse_move(x=150,y=150,speed=10,blocking=True)# Moves the mouse to x, y taking 'speed' seconds to moveprint(ahk.mouse_position)# (150, 150)
示例
此软件包提供的某些功能的非详尽示例。即将提供完整的文档!
鼠标
fromahkimportAHKahk=AHK()ahk.mouse_position# Returns a tuple of mouse coordinates (x, y)ahk.mouse_move(100,100,speed=10,relative=True)# Moves the mouse reletave to the current positionahk.mouse_position=(100,100)# Moves the mouse instantly to absolute screen positionahk.click()# Click the primary mouse buttonahk.double_click()# Clicks the primary mouse button twiceahk.click(200,200)# Moves the mouse to a particular position and clicksahk.right_click()# Clicks the secondary mouse buttonahk.mouse_drag(100,100,relative=True)# Holds down primary button and moves the mouse
键盘
fromahkimportAHKahk=AHK()ahk.type('hello, world!')# Send keys, as if typed (performs ahk string escapes)ahk.send_input('Hello`, World{!}')# Like AHK SendInput, must escape strings yourself!ahk.key_state('Control')# Return True or False based on whether Control key is pressed downahk.key_state('CapsLock',mode='T')# Check toggle state of a key (like for NumLock, CapsLock, etc)ahk.key_press('a')# Press and release a keyahk.key_down('Control')# Press down (but do not release) Control keyahk.key_up('Control')# Release the keyahk.key_wait('a',timeout=3)# Wait up to 3 seconds for the "a" key to be pressed. NOTE: This throws # a TimeoutError if the key isn't pressed within the timeout window
窗口
你也可以用windows做一些事情。
获取窗口
fromahkimportAHKfromahk.windowimportWindowahk=AHK()win=ahk.active_window# Get the active windowwin=ahk.win_get(title='Untitled - Notepad')# by titlewin=list(ahk.windows())# list of all windowswin=Window(ahk,ahk_id='0xabc123')# by ahk_idwin=Window.from_mouse_position(ahk)# the window under the mouse cursorwin=Window.from_pid('20366')# by process ID
使用Windows
fromahkimportAHKahk=AHK()ahk.run_script('Run Notepad')# Open notepadwin=ahk.find_window(title=b'Untitled - Notepad')# Find the opened windowwin.send('hello')# Send keys directly to the window (does not need focus!)win.move(x=200,y=300,width=500,height=800)win.activate()# Give the window focuswin.disable()# Make the window non-interactablewin.enable()# Enable it againwin.to_top()# Move the window on top of other windowswin.to_bottom()# Move the window to the bottom of the other windowswin.always_on_top=True# Make the window always on topwin.close()# Close the windowforwindowinahk.windows():print(window.title)# Some more attributesprint(window.text)print(window.rect)# (x, y, width, height)print(window.id)# ahk_idprint(window.pid)print(window.process)
屏幕
fromahkimportAHKahk=AHK()ahk.image_search('C:\\path\\to\\image.jpg')# Find an image on screen# Find an image within a boundary on screenahk.image_search('C:\\path\\to\\image.jpg',upper_bound=(100,100),# upper-left corner of search arealower_bound=(400,400))# lower-right corner of search areaahk.pixel_get_color(100,100)# Get color of pixel located at coords (100, 100)ahk.pixel_search('0x9d6346')# Get coords of the first pixel with specified color
声音
fromahkimportAHKahk=AHK()ahk.sound_play('C:\\path\\to\\sound.wav')# Play an audio fileahk.sound_beep(frequency=440,duration=1000)# Play a beep for 1 second (duration in microseconds)ahk.get_volume(device_number=1)# Get volume of a deviceahk.set_volume(50,device_number=1)# Set volume of a deviceahk.sound_get(device_number=1,component_type='MASTER',control_type='VOLUME')# Get sound device propertyahk.sound_set(50,device_number=1,component_type='MASTER',control_type='VOLUME')# Set sound device property
非阻塞模式
对于某些功能,您还可以选择非阻塞接口,以便在运行ahk脚本时执行其他操作。
importtimefromahkimportAHKahk=AHK()ahk.mouse_position=(200,200)# Moves the mouse instantly to the start positionstart=time.time()ahk.mouse_move(x=100,y=100,speed=30,blocking=False)whileTrue:# report mouse position while it movest=round(time.time()-start,4)position=ahk.mouse_positionprint(t,position)ifposition==(100,100):break
您应该看到类似于
pip install ahk
0
运行任意自动热键脚本
pip install ahk
1
从ahk到python的数据通信
如果要编写自己的ahk脚本以用于此库,则可以使用fileappend
和*
参数将数据从ahk脚本获取到python中。
假设您有一个这样的脚本
pip install ahk
2
pip install ahk
3
如果您的自动热键返回无法解码的内容,请添加关键字参数decode=false
在这种情况下,您将返回一个completedprocess
对象,其中stdout(和stderr)是字节,您可以根据自己的选择处理它。
pip install ahk
4
实验功能
实验特性是功能性最低(甚至更高)的东西,甚至 对于次要版本。
提供GitHub问题是为了方便收集对这些功能的反馈。
热键
热键现在有一个基本实现。给它一个热键(一个与ahk脚本中相同的字符串,没有:
)
以及作为对热键的响应而执行的ahk脚本的主体。
pip install ahk
5
此时,热键处于活动状态。
如果按+n,将执行脚本运行记事本
。
不需要向提供的脚本添加return
,因为它是由模板提供的。
要停止热键,请调用stop()
methOD.
pip install ahk
6
另请参见相关的AHK文档
动作链
actionchain
s允许您定义一组要在以后按顺序执行的操作。
它们的工作方式与ahk
类一样,只是在调用perform
方法之前,操作被延迟。
提供了一个附加的方法sleep
以允许在两个操作之间等待。
pip install ahk
7
与其他地方一样,同时运行的脚本可能会相互冲突,因此使用阻塞接口是
一般推荐。目前,在actionchains中与windows交互的支持有限,您可能希望使用win_set
)
查找窗口/查找窗口方法
现在,这些是通过遍历所有窗口句柄并使用python进行过滤来实现的。
它们在未来可能会得到优化。
ahk.find\u windows
根据作为关键字参数提供的属性返回生成器筛选结果。ahk.find_window
类似,但返回第一个匹配的窗口,而不是所有匹配的窗口。
有两个便利功能,但不确定这些功能是否会保留下来,或者我们可能会根据反馈添加更多功能。
- 按标题查找窗口
- 按标题查找窗口
- 按文本查找窗口
- 按文本查找窗口
错误和调试
您可以启用调试日志记录,它将在执行之前输出脚本文本,以及其他一些可能有用的 调试信息。
pip install ahk
8
(有关更多信息,请参见日志模块文档)
另外请注意,目前,运行ahk脚本的错误通常会悄无声息地过去。在未来,更好的错误处理 将被添加。
< >非python依赖项
默认情况下,它应该在路径上。您还可以使用ahk\u path
环境变量指定可执行文件的位置。
或者,您可以在代码中提供路径
pip install ahk
9
贡献
欢迎并感谢所有的贡献。
请随时打开GitHub问题或PR以获取反馈、想法、功能请求或问题。
在实施方面还有一些工作要做。理想的界面仍有待确定 您的帮助将是无价的。
我们的愿景是以pythonic的方式提供对autohotkey api最有用功能的访问。
类似项目
这些是一些常用于python自动化的类似项目。
- pyautogui-al-sweigart的跨平台自动化创建
- pywinauto-使用python在windows平台上实现自动化。
- 键盘-纯python跨平台键盘挂钩/控制键和热键!
- 鼠标-来自
键盘
的创建者,纯python鼠标控件! - pynput-键盘和鼠标控制