AutoHotkey 介绍
AutoHotkey 是 Windows 上一款非常强大的脚本工具,可以通过自定义的一个快捷键触发一系列的动作,可以通过命令调用系统接口、其他程序或是编写可视化界面。
AutoHotkey 程序本身不做任何事情; 它需要一个脚本来告诉它该做什么. 脚本只是一个简单的以 .ahk 作为扩展名的文本文件, 其中包含了程序的指令, 像配置文件, 但功能更强大。
使用
快捷键改写
^q::Send "!{F4}" ; ctrl + q 用于关闭窗口
打开网址
#z::Run "https://www.autohotkey.com" ; Win+Z
运行程序
^!n:: ; Ctrl+Alt+N
{
if WinExist("无标题 - Notepad")
WinActivate
else
Run "Notepad"
}
!1::Run "explorer"
Run('jiejian64.exe /script lib\ChangeBrightness.ahk')
启动文件夹
!d::Run "D:"
文本操作
; 插入 email
:C*:xem::acc8227@abc.com
; 插入 qq
:C*:xqq::133459846
; 快捷操作-插入双引号 ctrl + shift + "
^+"::Send '""{Left}'
窗口管理
!q::WinClose "A" ; 关闭窗口
; alt + f fullscreen 最大化或还
!f::{
minMax := WinGetMinMax("A")
if minMax = 1
WinRestore "A"
else
WinMaximize "A"
}
鼠标增强
#HotIf mouseIsOverTaskBarOrLeftEdge()
WheelUp::Send "{Volume_Up}"
WheelDown::Send "{Volume_Down}"
; 鼠标在左侧边缘或者在任务栏上
mouseIsOverTaskBarOrLeftEdge() {
MouseGetPos &OutputVarX,, &Win
return OutputVarX == 0 or WinExist("ahk_class Shell_TrayWnd" " ahk_id " Win)
}
热字符串替换
C 区分大小写,*
表示不需要额外键入终止符
:C*:xwx::😄 ; 微笑
:C*:xnb::很牛呀
其他
禁用快捷键
^v::return
dll 使用示例
WhichButton := DllCall("MessageBox", "Int", 0, "Str", "Press Yes or No", "Str", "Title of box", "Int", 4)
MsgBox "You pressed button #" WhichButton
动态创建热键
HotIfWinActive "ahk_exe Code.exe"
Hotkey "!n", MyFunc ; 创建一个只在记事本中工作的热键.
MyFunc(ThisHotkey)
{
MsgBox "You pressed " ThisHotkey
}
一些 ahk 内置函数
Suspend 禁用或启用所有的或选择的热键和热字串
Pause 暂停脚本的当前线程 感觉不怎么能用到
左键辅助
在鼠标左键按下的情况在 再按下 a 键
#HotIf GetKeyState("LButton", "P")
a::{
; 没有获取到文字直接返回,否则若选中的是网址则打开,否则进行百度搜索
text := GetSelectedText()
if (text) {
if (text ~= "i)^(?:https?://)?([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$") {
if not InStr(text, 'http') {
text := "http://" . text
}
Run text
} else {
Run "https://www.baidu.com/s?wd=" . text
}
}
}
#HotIf
闭包的使用
app_hotkey2(app_title) {
isActivate() ; 这是 app_title 和 app_path 的闭包.
{
return WinActive(app_title)
}
return isActivate
}
成果
自己基于 Autohotkey2 开发的按键映射 / 快捷键增强工具。为简化 windows 键鼠操作而生。
项目概览 - 捷键 - GitCode
https://gitcode.com/acc8226/jiejian/overview