AHK技术咨询:热键修改#Options及热字符串与热键无重复结合
Solution for Combining Hotstrings with Hotkeys (No Duplication) + Dynamic #Options Modification
Great question—let’s tackle both parts of your request with clean, non-repetitive AutoHotkey code.
Part 1: Trigger Hotstrings with Normal Space (Keep Trailing Space) and Alt+Space (:o: Mode)
The key here is to avoid duplicating your hotstring definitions by using dynamic hotstring options and shared variables for replacement text. Here’s a practical implementation:
; Step 1: Store replacement text in variables to avoid repetition btwReplacement := "by the way" helloReplacement := "Hello there!" ; Add more hotstring replacements here as needed ; Step 2: Initialize base hotstrings with normal space trigger (trailing space preserved) ; *C = trigger on any end character (space here), C = keep the end character (space) Hotstring(":*C:btw::", btwReplacement) Hotstring(":*C:hello::", helloReplacement) ; Step 3: Save original hotstring options to restore later originalHotstringOpts := {} originalHotstringOpts["btw"] := Hotstring("::btw::", "Options") originalHotstringOpts["hello"] := Hotstring("::hello::", "Options") ; Step 4: Bind Alt+Space to trigger :o: mode (immediate replacement) !Space:: ; Temporarily switch all hotstrings to :o: mode (no end character required) for hotstringKey, opts in originalHotstringOpts { Hotstring("::" hotstringKey "::", ":o:" . opts) } ; Send a space to trigger the hotstring (works with the updated mode) SendInput {Space} ; Restore original hotstring settings right after for hotstringKey, opts in originalHotstringOpts { Hotstring("::" hotstringKey "::", opts) } return
How this works:
- Normal space trigger: Typing
btw+ space replaces it withby the way(trailing space stays intact thanks to theCoption). - Alt+Space trigger: Pressing
Alt+Spacetemporarily swaps your hotstrings to:o:mode (immediate replacement without needing an end character), triggers the replacement with a space, then reverts back to original settings. No duplicated hotstring code required!
Part 2: General Method to Modify #Options via Hotkeys
Most #Options in AutoHotkey have corresponding runtime commands that let you adjust them dynamically. Here are common examples you can adapt:
Example 1: Toggle Hotstring Global Options
; Ctrl+Alt+H to toggle whether hotstrings keep their end character (C option) ^!h:: currentGlobalOpts := Hotstring("Options") if (InStr(currentGlobalOpts, "C")) { Hotstring("Options", StrReplace(currentGlobalOpts, "C", "C0")) ; Disable keep end character } else { Hotstring("Options", StrReplace(currentGlobalOpts, "C0", "C")) ; Enable keep end character } ToolTip, Hotstring global options updated: %Hotstring("Options")% SetTimer, ToolTip, -1500 ; Hide tooltip after 1.5 seconds return
Example 2: Toggle Single Instance Mode
; Ctrl+Alt+S to toggle #SingleInstance behavior ^!s:: currentMode := SingleInstance() if (currentMode = "On") { SingleInstance Off } else { SingleInstance On } ToolTip, SingleInstance set to %SingleInstance()% SetTimer, ToolTip, -1500 return
Example 3: Adjust Max Threads Per Hotkey
; Ctrl+Alt+M to switch between 1 and 2 max threads per hotkey ^!m:: currentVal := MaxThreadsPerHotkey() newVal := (currentVal = 1) ? 2 : 1 MaxThreadsPerHotkey %newVal% ToolTip, MaxThreadsPerHotkey set to %newVal% SetTimer, ToolTip, -1500 return
General Rule:
For any #Option, check the AutoHotkey documentation to find its matching runtime command (most #Options have one). Use that command in your hotkey to read the current value, modify it, and apply the change on the fly.
内容的提问来源于stack exchange,提问作者Ádám Bukovinszki
相关产品推荐
相关产品推荐

