เสื้อคลุม Python ที่พิมพ์อย่างเต็มที่รอบ Autohotkey
pip install ahk
ต้องใช้ Python 3.8+
รองรับ Autohotkey V1 และ V2 ดูเพิ่มเติมที่: การพึ่งพาไม่ใช่ Python
from ahk import AHK
ahk = 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 move
print ( ahk . mouse_position ) # (150, 150)ตัวอย่างที่ไม่ครบถ้วนสมบูรณ์ของฟังก์ชั่นบางอย่างที่มีอยู่ในแพ็คเกจนี้ ดูเอกสารฉบับเต็มสำหรับการอ้างอิง API ที่สมบูรณ์และคุณสมบัติเพิ่มเติม
Hotkeys สามารถกำหนดค่าให้เรียกใช้ฟังก์ชั่น Python เป็นโทรกลับ
ตัวอย่างเช่น:
from ahk import AHK
def my_callback ():
print ( 'Hello callback!' )
ahk = AHK ()
# when WIN + n is pressed, fire `my_callback`
ahk . add_hotkey ( '#n' , callback = my_callback )
ahk . start_hotkeys () # start the hotkey process thread
ahk . block_forever () # not strictly needed in all scripts -- stops the script from exiting; sleep forever ตอนนี้เมื่อใดก็ตามที่คุณกด + n , ฟังก์ชันการโทรกลับ my_callback จะถูกเรียกในเธรดพื้นหลัง
นอกจากนี้คุณยังสามารถเพิ่มตัวจัดการข้อยกเว้นสำหรับการโทรกลับของคุณ:
from ahk import AHK
ahk = AHK ()
def go_boom ():
raise Exception ( 'boom!' )
def my_ex_handler ( hotkey : str , exception : Exception ):
print ( 'exception with callback for hotkey' , hotkey , 'Here was the error:' , exception )
ahk . add_hotkey ( '#n' , callback = go_boom , ex_handler = my_ex_handler )นอกจากนี้ยังมีวิธีการกำจัดคีย์ลัด:
# ...
ahk . remove_hotkey ( '#n' ) # remove a hotkey by its keyname
ahk . clear_hotkeys () # remove all hotkeysโปรดทราบว่า:
ahk.start_hotkeys() )ahk.stop_hotkeys() (จะไม่หยุดเรียกใช้การโทรกลับอย่างแข็งขัน)ดูเอกสาร AHK ที่เกี่ยวข้องด้วย
Hotstrings ยังสามารถเพิ่มลงในเธรดกระบวนการฮอตคีย์
นอกเหนือจาก HotStrings ที่รองรับการเปลี่ยนสตริง AHK ปกติแล้วคุณยังสามารถให้การเรียกกลับ Python (พร้อมตัวจัดการข้อยกเว้นเสริม) เพื่อตอบสนองต่อการกระตุ้น Hotstrings
from ahk import AHK
ahk = AHK ()
def my_callback ():
print ( 'hello callback!' )
ahk . add_hotstring ( 'btw' , 'by the way' ) # string replacements
ahk . add_hotstring ( 'btw' , my_callback ) # call python function in response to the hotstringคุณยังสามารถลบ Hotstrings:
ahk . remove_hotstring ( 'btw' ) # remove a hotstring by its trigger sequence
ahk . clear_hotstrings () # remove all registered hotstrings from ahk import AHK
ahk = AHK ()
ahk . mouse_position # Returns a tuple of mouse coordinates (x, y) (relative to active window)
ahk . get_mouse_position ( coord_mode = 'Screen' ) # get coordinates relative to the screen
ahk . mouse_move ( 100 , 100 , speed = 10 , relative = True ) # Moves the mouse reletave to the current position
ahk . mouse_position = ( 100 , 100 ) # Moves the mouse instantly to absolute screen position
ahk . click () # Click the primary mouse button
ahk . click ( 200 , 200 ) # Moves the mouse to a particular position and clicks (relative to active window)
ahk . click ( 100 , 200 , coord_mode = 'Screen' ) # click relative to the screen instead of active window
ahk . click ( button = 'R' , click_count = 2 ) # Clicks the right mouse button twice
ahk . right_click () # Clicks the secondary mouse button
ahk . mouse_drag ( 100 , 100 , relative = True ) # Holds down primary button and moves the mouse from ahk import AHK
ahk = AHK ()
ahk . type ( 'hello, world!' ) # Send keys, as if typed (performs string escapes for you)
ahk . send_input ( 'Hello, {U+1F30E}{!}' ) # Like AHK SendInput
# Unlike `type`, control sequences must be escaped manually.
# For example the characters `!^+#=` and braces (`{` `}`) must be escaped manually.
ahk . key_state ( 'Control' ) # Return True or False based on whether Control key is pressed down
ahk . key_state ( 'CapsLock' , mode = 'T' ) # Check toggle state of a key (like for NumLock, CapsLock, etc)
ahk . key_press ( 'a' ) # Press and release a key
ahk . key_down ( 'Control' ) # Press down (but do not release) Control key
ahk . key_up ( 'Control' ) # Release the key
ahk . set_capslock_state ( "On" ) # Turn CapsLock on
if ahk . key_wait ( 'x' , timeout = 3 ): # wait for a key to be pressed; returns a boolean
print ( 'X was pressed within 3 seconds' )
else :
print ( 'X was not pressed within 3 seconds' )คุณสามารถทำสิ่งต่าง ๆ กับ Windows ได้เช่นกัน
from ahk import AHK
ahk = AHK ()
win = ahk . active_window # Get the active window
win = ahk . win_get ( title = 'Untitled - Notepad' ) # by title
all_windows = ahk . list_windows () # list of all windows
win = ahk . win_get_from_mouse_position () # the window under the mouse cursor
win = ahk . win_get ( title = 'ahk_pid 20366' ) # get window from pid
# Wait for a window
try :
# wait up to 5 seconds for notepad
win = ahk . win_wait ( title = 'Untitled - Notepad' , timeout = 5 )
# see also: win_wait_active, win_wait_not_active
except TimeoutError :
print ( 'Notepad was not found!' ) from ahk import AHK
ahk = AHK ()
ahk . run_script ( 'Run Notepad' ) # Open notepad
win = ahk . find_window ( title = 'Untitled - Notepad' ) # Find the opened window; returns a `Window` object
# Window object methods
win . send ( 'hello' , control = 'Edit1' ) # Send keys directly to the window (does not need focus!)
# OR ahk.control_send(title='Untitled - Notepad', control='Edit1')
win . move ( x = 200 , y = 300 , width = 500 , height = 800 )
win . activate () # Give the window focus
win . close () # Close the window
win . hide () # Hide the window
win . kill () # Kill the window
win . maximize () # Maximize the window
win . minimize () # Minimize the window
win . restore () # Restore the window
win . show () # Show the window
win . disable () # Make the window non-interactable
win . enable () # Enable it again
win . to_top () # Move the window on top of other windows
win . to_bottom () # Move the window to the bottom of the other windows
win . get_class () # Get the class name of the window
win . get_minmax () # Get the min/max status
win . get_process_name () # Get the process name (e.g., "notepad.exe")
win . process_name # Property; same as `.get_process_name()` above
win . is_always_on_top () # Whether the window has the 'always on top' style applied
win . list_controls () # Get a list of controls (list of `Control` objects)
win . redraw () # Redraw the window
win . set_style ( "-0xC00000" ) # Set a style on the window (in this case, removing the title bar)
win . set_ex_style ( "^0x80" ) # Set an ExStyle on the window (in this case, removes the window from alt-tab list)
win . set_region ( "" ) # See: https://www.autohotkey.com/docs/v2/lib/WinSetRegion.htm
win . set_trans_color ( "White" ) # Makes all pixels of the chosen color invisible inside the specified window.
win . set_transparent ( 155 ) # Makes the specified window semi-transparent (or "Off" to turn off transparency)
win . always_on_top = 'On' # Make the window always on top
# or
win . set_always_on_top ( 'On' )
for window in ahk . list_windows (): # list all (non-hidden) windows -- ``detect_hidden_windows=True`` to include hidden
print ( window . title )
# Some more attributes
print ( window . text ) # window text -- or .get_text()
print ( window . get_position ()) # (x, y, width, height)
print ( window . id ) # the ahk_id of the window
print ( window . pid ) # process ID -- or .get_pid()
print ( window . process_path ) # or .get_process_path()
if win . active : # or win.is_active()
...
if win . exist : # or win.exists()
...
# Controls
edit_control = win . list_controls ()[ 0 ] # get the first control for the window, in this case "Edit1" for Notepad
edit_control . get_text () # get the text in Notepad
edit_control . get_position () # returns a `Postion` namedtuple: e.g. Position(x=6, y=49, width=2381, height=1013) วิธีการหน้าต่างต่าง ๆ สามารถเรียกได้โดยตรงโดยไม่ต้องสร้างวัตถุ Window ก่อนโดยใช้วิธี win_* พื้นฐานในคลาส AHK ตัวอย่างเช่นแทนที่จะเป็น win.close() ดังกล่าวข้างต้นเราสามารถเรียก ahk.win_close(title='Untitled - Notepad') แทน
from ahk import AHK
ahk = AHK ()
ahk . image_search ( 'C: \ path \ to \ image.jpg' ) # Find an image on screen
# Find an image within a boundary on screen
ahk . image_search ( 'C: \ path \ to \ image.jpg' , upper_bound = ( 100 , 100 ), # upper-left corner of search area
lower_bound = ( 400 , 400 )) # lower-right corner of search area
ahk . pixel_get_color ( 100 , 100 ) # Get color of pixel located at coords (100, 100)
ahk . pixel_search ( color = '0x9d6346' , search_region_start = ( 0 , 0 ), search_region_end = ( 500 , 500 )) # Get coords of the first pixel with specified color รับ/ตั้งค่าข้อมูล Clipboard
from ahk import AHK
ahk = AHK ()
ahk . set_clipboard ( 'hello N{EARTH GLOBE AMERICAS} ' ) # set clipboard text contents
ahk . get_clipboard () # get clipboard text contents
# 'hello ?'
ahk . set_clipboard ( "" ) # Clear the clipboard
ahk . clip_wait ( timeout = 3 ) # Wait for clipboard contents to change (with text or file(s))
ahk . clip_wait ( timeout = 3 , wait_for_any_data = True ) # wait for _any_ clipboard contents คุณอาจได้รับ/ตั้งค่า ClipboardAll - อย่างไรก็ตามคุณไม่ควรลองเรียก set_clipboard_all ด้วยข้อมูลอื่น ๆ นอกเหนือจาก ที่ ส่งคืนโดย get_clipboard_all หรือปัญหาที่ไม่คาดคิดอาจเกิดขึ้น
from ahk import AHK
ahk = AHK ()
# save all clipboard contents in all formats
saved_clipboard = ahk . get_clipboard_all ()
ahk . set_clipboard ( 'something else' )
...
ahk . set_clipboard_all ( saved_clipboard ) # restore saved content from earlier คุณยังสามารถตั้งค่าการโทรกลับเพื่อเรียกใช้งานเมื่อเนื้อหาคลิปบอร์ดเปลี่ยน เช่นเดียวกับวิธีฮอตคีย์ที่กล่าวถึงข้างต้นคุณสามารถตั้งค่าตัวจัดการข้อยกเว้นได้ เช่นเดียวกับสายฮอตการเรียกกลับ on_clipboard_change ยังต้องการ .start_hotkeys() ที่จะเรียกให้มีผล
ฟังก์ชั่นการโทรกลับจะต้องยอมรับอาร์กิวเมนต์ตำแหน่งหนึ่งซึ่งเป็นจำนวนเต็มที่ระบุว่าข้อมูลคลิปบอร์ด
from ahk import AHK
ahk = AHK ()
def my_clipboard_callback ( change_type : int ):
if change_type == 0 :
print ( 'Clipboard is now empty' )
elif change_type == 1 :
print ( 'Clipboard has text contents' )
elif change_type == 2 :
print ( 'Clipboard has non-text contents' )
ahk . on_clipboard_change ( my_clipboard_callback )
ahk . start_hotkeys () # like with hotkeys, must be called at least once for listening to start
# ...
ahk . set_clipboard ( "hello" ) # will cause the message "Clipboard has text contents" to be printed by the callback
ahk . set_clipboard ( "" ) # Clears the clipboard, causing the message "Clipboard is now empty" to be printed by the callback from ahk import AHK
ahk = AHK ()
ahk . sound_play ( 'C: \ path \ to \ sound.wav' ) # Play an audio file
ahk . 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 device
ahk . set_volume ( 50 , device_number = 1 ) # Set volume of a device
ahk . sound_get ( device_number = 1 , component_type = 'MASTER' , control_type = 'VOLUME' ) # Get sound device property
ahk . sound_set ( 50 , device_number = 1 , component_type = 'MASTER' , control_type = 'VOLUME' ) # Set sound device property คำแนะนำเครื่องมือ/traytips
import time
from ahk import AHK
ahk = AHK ()
ahk . show_tooltip ( "hello4" , x = 10 , y = 10 )
time . sleep ( 2 )
ahk . hide_tooltip () # hide the tooltip
ahk . show_info_traytip ( "Info" , "It's also info" , silent = False , blocking = True ) # Default info traytip
ahk . show_warning_traytip ( "Warning" , "It's a warning" ) # Warning traytip
ahk . show_error_traytip ( "Error" , "It's an error" ) # Error trytipกล่องโต้ตอบ
from ahk import AHK , MsgBoxButtons
ahk = AHK ()
ahk . msg_box ( text = 'Do you like message boxes?' , title = 'My Title' , buttons = MsgBoxButtons . YES_NO )
ahk . input_box ( prompt = 'Password' , title = 'Enter your password' , hide = True )
ahk . file_select_box ( title = 'Select one or more mp3 files' , multi = True , filter = '*.mp3' , file_must_exist = True )
ahk . folder_select_box ( prompt = 'Select a folder' ) คุณสามารถเปลี่ยนสถานะทั่วโลกต่าง ๆ เช่น CoordMode , DetectHiddenWindows ฯลฯ ดังนั้นคุณไม่จำเป็นต้องส่งพารามิเตอร์เหล่านี้โดยตรงไปยังการเรียกใช้ฟังก์ชัน
from ahk import AHK
ahk = AHK ()
ahk . set_coord_mode ( 'Mouse' , 'Screen' ) # set default Mouse CoordMode to be relative to Screen
ahk . set_detect_hidden_windows ( True ) # Turn on detect hidden windows by default
ahk . set_send_level ( 5 ) # Change send https://www.autohotkey.com/docs/v1/lib/SendLevel.htm
ahk . set_title_match_mode ( 'Slow' ) # change title match speed and/or mode
ahk . set_title_match_mode ( 'RegEx' )
ahk . set_title_match_mode (( 'RegEx' , 'Slow' )) # or both at the same time
ahk . set_send_mode ( 'Event' ) # change the default SendMode คุณสามารถเพิ่มคำสั่งที่จะเพิ่มลงในสคริปต์ที่สร้างขึ้นทั้งหมด ตัวอย่างเช่นเพื่อป้องกันไม่ให้ AHK Trayicon ปรากฏตัวคุณสามารถเพิ่มคำสั่ง notrayicon
from ahk import AHK
from ahk . directives import NoTrayIcon
ahk = AHK ( directives = [ NoTrayIcon ])โดยค่าเริ่มต้นมีการเพิ่มคำสั่งบางอย่างโดยอัตโนมัติเพื่อให้แน่ใจว่าฟังก์ชั่นและถูกรวมเข้ากับคำสั่งที่ผู้ใช้ให้
คำสั่งไม่ได้ใช้สำหรับกระบวนการ AHK ที่ใช้สำหรับการจัดการสายลัดและ hotstrings (กล่าวถึงด้านล่าง) โดยค่าเริ่มต้น ในการใช้คำสั่งกับกระบวนการลัดโดยใช้อาร์กิวเมนต์คำหลัก apply_to_hotkeys_process=True :
from ahk import AHK
from ahk . directives import NoTrayIcon
directives = [
NoTrayIcon ( apply_to_hotkeys_process = True )
]
ahk = AHK ( directives = directives )ตามที่กล่าวไว้ข้างต้นคุณสามารถซ่อนไอคอนถาดหากต้องการ นอกจากนี้ยังมีวิธีการบางอย่างสำหรับการปรับแต่งไอคอนถาด
from ahk import AHK
ahk = AHK ()
# change the tray icon (in this case, using a builtin system icon)
ahk . menu_tray_icon ( 'Shell32.dll' , 174 )
# revert it back to the original:
ahk . menu_tray_icon ()
# change the tooltip that shows up when hovering the mouse over the tray icon
ahk . menu_tray_tooltip ( 'My Program Name' )
# Hide the tray icon
ahk . menu_tray_icon_hide ()
# Show the tray icon that was previously hidden by ``NoTrayIcon`` or ``menu_tray_icon_hide``
ahk . menu_tray_icon_show ()คุณสามารถอ่าน/เขียน/ลบคีย์รีจิสทรี:
from ahk import AHK
ahk = AHK ()
ahk . reg_write ( 'REG_SZ' , r'HKEY_CURRENT_USERSOFTWAREmy-software' , value = 'test' )
ahk . reg_write ( 'REG_SZ' , r'HKEY_CURRENT_USERSOFTWAREmy-software' , value_name = 'foo' , value = 'bar' )
ahk . reg_read ( r'HKEY_CURRENT_USERSOFTWAREmy-software' ) # 'test'
ahk . reg_delete ( r'HKEY_CURRENT_USERSOFTWAREmy-software' )หากคีย์ไม่มีอยู่หรือมีปัญหาอื่น ๆ เกิดขึ้นข้อยกเว้นจะถูกยกขึ้น
วิธีการส่วนใหญ่ในไลบรารีนี้ให้อินเทอร์เฟซที่ไม่ปิดกั้นดังนั้นสคริปต์ Python ของคุณสามารถดำเนินการต่อได้ในขณะที่สคริปต์ AHK ของคุณทำงาน
โดยค่าเริ่มต้นการโทรทั้งหมดจะ ปิดกั้น - แต่ละฟังก์ชั่นจะดำเนินการอย่างสมบูรณ์ก่อนที่ฟังก์ชั่นถัดไปจะรัน
อย่างไรก็ตามบางครั้งคุณอาจต้องการเรียกใช้รหัสอื่นในขณะที่ AHK เรียกใช้รหัสบางอย่าง เมื่ออาร์กิวเมนต์คำหลัก blocking พร้อมกับ False การเรียกใช้ฟังก์ชันจะกลับมาทันทีในขณะที่ฟังก์ชั่น AHK จะถูกดำเนินการในพื้นหลัง
ตัวอย่างเช่นคุณสามารถย้ายเมาส์ช้าๆและรายงานตำแหน่งของมันในขณะที่มันเคลื่อนที่:
import time
from ahk import AHK
ahk = AHK ()
ahk . mouse_position = ( 200 , 200 ) # Moves the mouse instantly to the start position
start = time . time ()
# move the mouse very slowly
ahk . mouse_move ( x = 100 , y = 100 , speed = 30 , blocking = False )
# This code begins executing right away, even though the mouse is still moving
while True :
t = round ( time . time () - start , 4 )
position = ahk . mouse_position
print ( t , position ) # report mouse position while it moves
if position == ( 100 , 100 ):
break เมื่อคุณระบุ blocking=False คุณจะได้รับวัตถุพิเศษ FutureResult (หรือ AsyncFutureResult Object ใน Async API ซึ่งกล่าวถึงด้านล่าง) ซึ่งช่วยให้คุณรอฟังก์ชันที่จะเสร็จสิ้นและดึงค่าคืนผ่านฟังก์ชัน get_result แม้ว่าฟังก์ชั่นจะส่ง None โดยปกติแล้วสิ่งนี้จะเป็นประโยชน์เพื่อให้แน่ใจว่า AHK ได้ดำเนินการฟังก์ชั่นเสร็จแล้ว
การโทรไม่ปิดกั้น:
set_coord_mode หรือคล้ายกัน) - สิ่งนี้อาจเปลี่ยนแปลงในเวอร์ชันในอนาคตFutureResult (หรือ AsyncFutureResult Object ใน Async API ซึ่งกล่าวถึงด้านล่าง) ซึ่งช่วยให้คุณรอฟังก์ชั่นเพื่อให้เสร็จสมบูรณ์และดึงค่าคืนผ่านฟังก์ชั่น result แม้ว่าฟังก์ชั่นจะส่ง None โดยปกติแล้วสิ่งนี้จะเป็นประโยชน์เพื่อให้แน่ใจว่า AHK ได้ดำเนินการฟังก์ชั่นเสร็จแล้ว from ahk import AHK
ahk = AHK ()
future_result = ahk . mouse_move ( 100 , 100 , speed = 40 , blocking = False )
...
# wait on the mouse_move to finish
future_result . result ( timeout = 10 ) # timeout keyword is optional ASYNC API มีให้เพื่อให้สามารถเรียกใช้ฟังก์ชั่นโดยใช้ async / await วิธีการเดียวกันทั้งหมดจาก API แบบซิงโครนัสมีอยู่ใน ASYNC API
from ahk import AsyncAHK
import asyncio
ahk = AsyncAHK ()
async def main ():
await ahk . mouse_move ( 100 , 100 )
x , y = await ahk . get_mouse_position ()
print ( x , y )
asyncio . run ( main ())Async API นั้นเหมือนกับ API ปกติโดยมีความแตกต่างที่น่าสังเกตเล็กน้อย:
.mouse_position หรือ .title สำหรับ windows) สามารถ await ได้วิธีการเพิ่มเติม (เช่น get_mouse_position() และ get_title() ) ได้รับการเพิ่มสำหรับ API ที่ใช้งานง่ายมากขึ้นและแนะนำให้ใช้คุณสมบัติahk.mouse_postion = (200, 200) ) ไม่ได้รับอนุญาตใน ASYNC API (เพิ่ม RunTimeError) ตัวตั้งค่าคุณสมบัติยังคงมีอยู่ใน SYNC APIAsyncFutureResult Objects (ส่งคืนเมื่อระบุ blocking=False ) ทำงานเหมือนกับวัตถุ FutureResult ใน SYNC API ยกเว้นคำหลัก timeout ไม่รองรับวิธี result )โปรดทราบว่า:
AsyncAHK เดียวจะไม่ทำงานพร้อมกัน คุณต้องใช้ blocking=False เช่นเดียวกับใน SYNC API หรือใช้หลายอินสแตนซ์ของ AsyncAHK ไลบรารีนี้พิมพ์แบบพิมพ์ได้อย่างสมบูรณ์ช่วยให้คุณใช้ประโยชน์จากเครื่องมือเช่น mypy เพื่อช่วยตรวจสอบความถูกต้องของรหัสของคุณ IDEs ที่ใช้คุณสมบัติการตรวจสอบประเภทยังสามารถใช้ประโยชน์จากคำแนะนำประเภทเพื่อช่วยให้แน่ใจว่ารหัสของคุณปลอดภัย
นอกจากนี้คุณยังสามารถเรียกใช้รหัส AutoHotkey โดยพลการไม่ว่าจะเป็นไฟล์สคริปต์ .ahk หรือเป็นสตริงที่มีรหัส AHK
from ahk import AHK
ahk = AHK ()
my_script = '''
MouseMove, 100, 100
; etc...
'''
ahk . run_script ( my_script ) from ahk import AHK
ahk = AHK ()
script_path = r'C:PathTomyscript.ahk'
ahk . run_script ( script_path ) ในการใช้แพ็คเกจนี้คุณต้องใช้งาน AutoHotkey (เช่น AutoHotkey.exe ) คาดว่าจะอยู่บนเส้นทางโดยค่าเริ่มต้นหรือในตำแหน่งการติดตั้งเริ่มต้น ( C:Program FilesAutoHotkeyAutoHotkey.exe สำหรับ v1 หรือ C:Program FilesAutoHotkeyv2AutoHotkey64.exe สำหรับ v2)
Autohotkey V1 และ V2 ได้รับการสนับสนุนอย่างเต็มที่แม้ว่าความแตกต่างของพฤติกรรมบางอย่างจะเกิดขึ้นขึ้นอยู่กับรุ่นที่คุณใช้ ดูหมายเหตุด้านล่าง
วิธีที่แนะนำในการจัดหา Autohotkey Binary (สำหรับทั้ง V1 และ V2) คือการติดตั้ง binary พิเศษสำหรับแพ็คเกจนี้ สิ่งนี้จะให้การดำเนินการที่จำเป็นและช่วยให้แน่ใจว่าพวกเขาวางอยู่บนเส้นทางอย่างถูกต้อง
pip install "ahk[binary]"
หรือคุณอาจให้เส้นทางในรหัส:
from ahk import AHK
ahk = AHK ( executable_path = 'C: \ path \ to \ AutoHotkey.exe' ) นอกจากนี้คุณยังสามารถใช้ตัวแปรสภาพแวดล้อม AHK_PATH เพื่อระบุตำแหน่งที่ปฏิบัติการได้
set AHK_PATH=C:PathToAutoHotkey.exe
python myscript.py โดยค่าเริ่มต้นเมื่อไม่มีการตั้งค่าพารามิเตอร์ executable_path (หรือตัวแปรสภาพแวดล้อม AHK_PATH ) จะถูกตั้งค่าเฉพาะชื่อไบนารี AutoHotkey V1 เท่านั้นที่จะค้นหาบนเส้นทางหรือตำแหน่งการติดตั้งเริ่มต้น พฤติกรรมนี้อาจเปลี่ยนแปลงในรุ่นอนาคตเพื่อให้ V2 สามารถใช้งานได้โดยค่าเริ่มต้น
หากต้องการใช้ AutoHotKey เวอร์ชัน 2 คุณสามารถทำสิ่งใด ๆ ต่อไปนี้:
executable_path พร้อมตำแหน่งของ AutoHotkey V2 BinaryAHK_PATH ด้วยตำแหน่งของ AutoHotKey V2 Binaryversion ด้วย Value v2 ซึ่งเปิดใช้งานการค้นหาที่เรียกใช้งานโดยใช้ชื่อไบนารี AutoHotkey V2 และตำแหน่งการติดตั้งเริ่มต้นตัวอย่างเช่น:
from ahk import AHK
ahk = AHK ( executable_path = r'C:Program FilesAutoHotkeyv2AutoHotkey64.exe' )
# OR
ahk = AHK ( version = 'v2' ) เมื่อคุณระบุอาร์กิวเมนต์คำหลักของ version (ด้วย "v1" หรือ "v2" ) การตรวจสอบจะดำเนินการเพื่อให้แน่ใจว่าไบนารีที่ให้มา (หรือค้นพบ) ตรงกับเวอร์ชันที่ร้องขอ เมื่อคีย์เวิร์ด version ถูกละเว้นเวอร์ชันจะถูกกำหนดโดยอัตโนมัติจากไบนารีปฏิบัติการที่ให้มาโดยอัตโนมัติ
API ของโครงการนี้ได้รับการออกแบบมาแล้วกับ Autohotkey V1 และลายเซ็นฟังก์ชั่นนั้นเหมือนกันแม้ว่าจะใช้ AutoHotkey V2 ในขณะที่พฤติกรรมส่วนใหญ่ยังคงเหมือนเดิมพฤติกรรมบางอย่างจะเปลี่ยนไปเมื่อใช้ AutoHotkey V2 เมื่อเทียบกับ V1 ส่วนใหญ่เกิดจากความแตกต่างพื้นฐานระหว่างสองเวอร์ชัน
ความแตกต่างที่น่าสังเกตบางอย่างที่คุณอาจพบเมื่อใช้ Autohotkey v2 กับไลบรารีนี้รวมถึง:
None (เช่นใน AutoHotkey v2 targetError จะถูกโยนลงในกรณีส่วนใหญ่ที่ไม่สามารถหาหน้าต่างหรือควบคุมได้)ControlSend ( ahk.control_send หรือ Window.send หรือ Control.send ) แตกต่างกันใน AutoHotkey V2 เมื่อไม่ได้ระบุพารามิเตอร์ control ใน V1 คีย์จะถูกส่งไปยังตัวควบคุมสูงสุดซึ่งมักจะเป็นพฤติกรรมที่ถูกต้อง ใน V2 คีย์จะถูกส่งไปยังหน้าต่างโดยตรง ซึ่งหมายความว่าในหลายกรณีคุณต้องระบุการควบคุมอย่างชัดเจนเมื่อใช้ V2secondstowait สำหรับ TrayTip ( ahk.show_traytip ) ถูกลบออกใน V2 การระบุพารามิเตอร์นี้ใน Wrapper Python จะทำให้คำเตือนถูกปล่อยออกมาและพารามิเตอร์จะถูกละเว้นInput แทนที่จะเป็น Event ใน V1 (ตัวอย่างเช่นพารามิเตอร์ความเร็วของเมาส์เป็น mouse_move และ mouse_drag จะถูกละเว้นใน V2 เว้นแต่ว่าโหมดส่งจะเปลี่ยนไป)2 ใน AutoHotkey V2 มันคือ 1 ใน autohotkey v1 ใช้อาร์กิวเมนต์คำหลัก title_match_mode เพื่อ win_get และวิธีอื่น ๆ ที่ยอมรับคำหลักนี้เพื่อควบคุมพฤติกรรมนี้หรือใช้ set_title_match_mode เพื่อเปลี่ยนพฤติกรรมเริ่มต้น (การโทรที่ไม่ปิดกั้นจะทำงานในกระบวนการแยก set_title_match_mode คุณสามารถพัฒนาส่วนขยายสำหรับการขยายฟังก์ชันการทำงานของ ahk - นั่นคือ: การเขียนรหัส autohotkey ของคุณเองและเพิ่มวิธีการเพิ่มเติมในคลาส AHK ดูเอกสารเพิ่มเติมสำหรับข้อมูลเพิ่มเติม
การบริจาคทั้งหมดได้รับการต้อนรับและชื่นชม
โปรดอย่าลังเลที่จะเปิดปัญหา GitHub หรือ PR สำหรับข้อเสนอแนะความคิดเห็นความคิดการร้องขอคุณสมบัติหรือคำถาม
เหล่านี้เป็นโครงการที่คล้ายกันที่ใช้กันทั่วไปสำหรับระบบอัตโนมัติกับ Python
keyboard การควบคุม เมาส์ Pure Python!