AutoHotkey 实用快捷键教程
AutoHotkey 是一款功能强大的自动化脚本语言,可以帮助你创建各种快捷键、自动化任务和提高工作效率。本教程汇总了最实用的 AutoHotkey 快捷键和功能,帮助你快速提升工作效率。
基础快捷键设置
; 基本语法: 快捷键::动作
; 应用程序启动
!n::Run "C:\Program Files\Joplin\Joplin.exe" ; Alt+N 启动Joplin
!l::Run "C:\Users\58425\AppData\Local\Logseq\Logseq.exe" ; Alt+L 启动Logseq
!c::Run "calc.exe" ; Alt+C 打开计算器
!b::Run "chrome.exe" ; Alt+B 打开Chrome浏览器
!e::Run "explorer.exe" ; Alt+E 打开文件资源管理器
; 窗口管理
^Space::Winset, AlwaysOnTop, Toggle, A ; Ctrl+Space 窗口置顶/取消置顶
#m::WinMinimize, A ; Win+M 最小化当前窗口
#+m::WinMaximize, A ; Win+Shift+M 最大化当前窗口
!m::WinRestore, A ; Alt+M 还原窗口
日期和时间插入
; 插入当前时间 (Ctrl+Shift+T)
^+t::
FormatTime, CurrentDateTime,, yyyy-MM-dd HH:mm:ss
SendInput %CurrentDateTime%
return
; 插入当前日期 (Ctrl+Shift+D)
^+d::
FormatTime, CurrentDate,, yyyy-MM-dd
SendInput %CurrentDate%
return
; 插入Unix时间戳 (Ctrl+Shift+Z)
^+z::
timestamp := A_Now
EnvSub, timestamp, 19700101000000, Seconds
SendInput %timestamp%
return
文本处理增强
; 文本大小写转换
^+u:: ; Ctrl+Shift+U 将选中文本转为大写
ClipSaved := ClipboardAll
Clipboard := ""
Send ^c
ClipWait, 1
StringUpper, Clipboard, Clipboard
Send ^v
Sleep 50
Clipboard := ClipSaved
ClipSaved := ""
return
^+l:: ; Ctrl+Shift+L 将选中文本转为小写
ClipSaved := ClipboardAll
Clipboard := ""
Send ^c
ClipWait, 1
StringLower, Clipboard, Clipboard
Send ^v
Sleep 50
Clipboard := ClipSaved
ClipSaved := ""
return
; 常用文本替换/缩写展开
::btw::by the way
::afaik::as far as I know
::brb::be right back
; 自动填充表单
^+f:: ; Ctrl+Shift+F 填充常用表单信息
SendInput 张三{Tab}
SendInput 123456789@qq.com{Tab}
SendInput 13800138000{Tab}
SendInput 北京市海淀区
return
; 代码片段展开
:*:html5::<!DOCTYPE html>`n<html lang="en">`n<head>`n`t<meta charset="UTF-8">`n`t<meta name="viewport" content="width=device-width, initial-scale=1.0">`n`t<title>Document</title>`n</head>`n<body>`n`t`n</body>`n</html>
网络搜索和翻译
; 快速搜索选中文本
^+g:: ; Ctrl+Shift+G 使用Google搜索选中的文本
Send, ^c
Sleep, 50
Run, https://www.google.com/search?q=%clipboard%
return
^+b:: ; Ctrl+Shift+B 用百度搜索选中内容
Send, ^c
Sleep, 50
Run, https://www.baidu.com/s?wd=%clipboard%
return
; 快速翻译选中文本
^+t:: ; Ctrl+Shift+T 翻译选中文本
Send, ^c
Sleep, 50
Run, https://translate.google.com/?sl=auto&tl=zh-CN&text=%clipboard%&op=translate
return
窗口排列和管理
; 窗口排列
#1:: ; Win+1 将窗口移至左半屏
WinGetActiveTitle, Title
WinRestore, %Title%
WinMove, %Title%,, 0, 0, A_ScreenWidth/2, A_ScreenHeight
return
#2:: ; Win+2 将窗口移至右半屏
WinGetActiveTitle, Title
WinRestore, %Title%
WinMove, %Title%,, A_ScreenWidth/2, 0, A_ScreenWidth/2, A_ScreenHeight
return
; 窗口拖动(无需使用标题栏)
!LButton:: ; Alt+左键拖动窗口
CoordMode, Mouse, Screen
MouseGetPos, MouseStartX, MouseStartY, WindowUnderMouseID
WinGetPos, WindowStartX, WindowStartY, , , ahk_id %WindowUnderMouseID%
loop
{
if !GetKeyState("LButton", "P")
break
MouseGetPos, MouseX, MouseY
WinMove, ahk_id %WindowUnderMouseID%, , WindowStartX + MouseX - MouseStartX, WindowStartY + MouseY - MouseStartY
Sleep, 10
}
return
剪贴板增强
; 简易剪贴板管理器
^!1:: ; Ctrl+Alt+1 保存到剪贴板1
Clipboard1 := Clipboard
ToolTip, 已保存到剪贴板1
SetTimer, RemoveToolTip, 1000
return
^1:: ; Ctrl+1 粘贴剪贴板1内容
Clipboard := Clipboard1
Send ^v
return
; 高级剪贴板历史记录
#Persistent
OnClipboardChange("ClipChanged")
ClipChanged(Type) {
static ClipHistory := []
if (Type == 1) { ; 1 = 文本
ClipHistory.Push(Clipboard)
if (ClipHistory.Length() > 20) ; 保留最近20条记录
ClipHistory.RemoveAt(1)
}
}
^+h:: ; Ctrl+Shift+H 显示剪贴板历史
Menu, ClipMenu, Add
Menu, ClipMenu, DeleteAll
for i, clip in ClipHistory {
shortClip := SubStr(clip, 1, 30)
if (StrLen(clip) > 30)
shortClip .= "..."
Menu, ClipMenu, Add, %i%: %shortClip%, PasteClip
}
Menu, ClipMenu, Show
return
PasteClip:
RegExMatch(A_ThisMenuItem, "^\d+", clipNum)
Clipboard := ClipHistory[clipNum]
Send ^v
return
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
系统控制
; 音量控制
^!Up::Send {Volume_Up} ; Ctrl+Alt+上箭头 增加音量
^!Down::Send {Volume_Down} ; Ctrl+Alt+下箭头 降低音量
^!m::Send {Volume_Mute} ; Ctrl+Alt+M 静音切换
; 鼠标手势 - 右键+滚轮
RButton & WheelUp::Send {Volume_Up} ; 右键+滚轮上 增加音量
RButton & WheelDown::Send {Volume_Down} ; 右键+滚轮下 减小音量
RButton & MButton::Send {Media_Play_Pause} ; 右键+中键 播放/暂停媒体
; 系统操作
^!l::DllCall("LockWorkStation") ; Ctrl+Alt+L 锁定计算机
^!r::Shutdown, 2 ; Ctrl+Alt+R 重启计算机 (需管理员权限)
^!s::Shutdown, 1 ; Ctrl+Alt+S 关闭计算机 (需管理员权限)
实用工具
; 快速计算器
^+c:: ; Ctrl+Shift+C 快速计算
InputBox, Expression, 快速计算器, 请输入计算表达式:, , 300, 130
if (!ErrorLevel) { ; 用户点击了确定
Result := Eval(Expression)
MsgBox, 结果: %Result%
Clipboard := Result
}
return
; 计算函数 (简易版)
Eval(expression) {
ComObjCreate("ScriptControl").Language := "JScript"
ComObjCreate("ScriptControl").ExecuteStatement("result = " . expression)
return ComObjCreate("ScriptControl").Eval("result")
}
; 屏幕颜色拾取器
^+p:: ; Ctrl+Shift+P 拾取屏幕颜色
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, RGB
Clipboard := color
ToolTip, 颜色代码 %color% 已复制到剪贴板, %MouseX%, %MouseY%
Sleep, 1500
ToolTip
return
; 定时提醒
^!t:: ; Ctrl+Alt+T 设置定时提醒
InputBox, Minutes, 定时提醒, 请输入多少分钟后提醒:, , 300, 130
if (!ErrorLevel) { ; 用户点击了确定
InputBox, Message, 定时提醒, 请输入提醒内容:, , 300, 130
if (!ErrorLevel) { ; 用户点击了确定
SetTimer, ShowReminder, % Minutes * 60000
global ReminderMessage := Message
MsgBox, 已设置 %Minutes% 分钟后提醒!
}
}
return
ShowReminder:
SetTimer, ShowReminder, Off
MsgBox, 4096, 定时提醒, %ReminderMessage%
return
; 快速记事本
^!n:: ; Ctrl+Alt+N 打开快速记事本
InputBox, Note, 快速记事本, 请输入要保存的内容:, , 400, 200
if (!ErrorLevel) { ; 用户点击了确定
FormatTime, TimeStamp,, yyyy-MM-dd HH:mm:ss
FileAppend, `n[%TimeStamp%] %Note%, %A_MyDocuments%\QuickNotes.txt
MsgBox, 笔记已保存!
}
return
应用程序特定快捷键
; 仅在特定应用程序中生效的快捷键
#IfWinActive ahk_exe chrome.exe
^q::Send ^w ; 在Chrome中,Ctrl+Q关闭当前标签页
!1::Send ^1 ; 在Chrome中,Alt+1切换到第一个标签
!2::Send ^2 ; 在Chrome中,Alt+2切换到第二个标签
#IfWinActive
#IfWinActive ahk_exe explorer.exe
^b::Send !{Up} ; 在文件资源管理器中,Ctrl+B返回上一级目录
^f::Send !d ; 在文件资源管理器中,Ctrl+F聚焦到地址栏
#IfWinActive
高级鼠标手势
; 鼠标手势 - 按住右键并移动
RButton::
MouseGetPos, start_x, start_y
KeyWait, RButton
MouseGetPos, end_x, end_y
x_distance := end_x - start_x
y_distance := end_y - start_y
if (abs(x_distance) < 10 && abs(y_distance) < 10) {
; 普通右键点击
Send {RButton}
return
}
if (abs(x_distance) > abs(y_distance)) {
if (x_distance > 50)
Send !{Right} ; 向右手势 - 前进
else if (x_distance < -50)
Send !{Left} ; 向左手势 - 后退
} else {
if (y_distance > 50)
Send {End} ; 向下手势 - 到底部
else if (y_distance < -50)
Send {Home} ; 向上手势 - 到顶部
}
return
自动化任务
; 定期自动保存当前工作
SetTimer, AutoSave, 300000 ; 每5分钟自动保存一次
AutoSave:
IfWinActive, ahk_exe notepad.exe
Send ^s
IfWinActive, ahk_exe WINWORD.EXE
Send ^s
IfWinActive, ahk_exe EXCEL.EXE
Send ^s
return
; 快速启动多个应用程序(工作环境设置)
^!w:: ; Ctrl+Alt+W 启动工作环境
Run "chrome.exe"
Sleep 500
Run "outlook.exe"
Sleep 500
Run "code.exe" ; VS Code
return
使用技巧
-
避免快捷键冲突:创建新快捷键前,确保它不会与常用应用程序中的现有快捷键冲突。
-
修饰键符号:
^
代表 Ctrl+
代表 Shift!
代表 Alt#
代表 Windows 键
-
脚本启动:
- 将脚本保存为
.ahk
文件 - 双击运行,或放入启动文件夹实现开机自启动
- 系统托盘区会显示 AutoHotkey 图标
- 将脚本保存为
-
调试技巧:
- 使用
MsgBox
显示变量值 - 使用
ToolTip
临时显示信息
- 使用
-
常见问题:
- 如果脚本不工作,检查是否以管理员身份运行
- 某些应用程序可能会阻止热键,尝试使用
#IfWinActive
创建应用程序特定的热键
结语
AutoHotkey 是一个非常强大的工具,可以大大提高你的工作效率。本教程只是介绍了一些基本和常用的功能,你可以根据自己的需求进一步定制和扩展。随着使用经验的积累,你可以创建更复杂和强大的脚本来自动化各种任务。
祝你使用愉快!