昨晚我用【AHK-v2-script-converter】试图把dog的曹操快切转换成v2,结果一直报错,改得我人都麻了。
改得太痛苦不如重写,所以,我今天花了一天时间一边学习v2,一边参考dog分享的源码,重写了曹操快切,目前是残血v2版本。
使用方法:
①下载AutoHotkey v2.0,安装运行。
②新建一个后缀为.ahk的文件,把下面的代码放进文件里,运行这个文件。
③默认Alt+;【alt+分号】,运行,可自行修改
总结
; 脚本名称:曹操快切 残血版 AutoHotkey v2
; 脚本版本:1.2
; 曹操快切的原作者【dog】发布地址:https://meta.appinn.net/t/topic/34203
; 因为我用网上的v1 转 v2 的脚本【AHK-v2-script-converter】转换曹操快切,搞了一晚上失败
; 没办法,我只好一边搜索【wyagd001.github.io】AutoHotkey v2版的资料,一边参考曹操快切的源码,从零开始重写V2版本
; 万分感谢【dog】提供的源码,以及wyagd001的中文教程,以及网友分享的代码
; 郑重提示,我不是专业程序员,几乎没学习过编程(我只有过抄别人代码的经历),下面的注释纯属个人见解,仅供参考,对错自辩
; 【num_arr是键位排序,可自定义】
global num_arr := ["1","2","3","4","5","q","w","e","r","t","a","s","d","f","g","6","7","8","9","0","y","u","i","o","p","h","j","k","l","z","x","c","v","b","n","m",]
; 【hotkey1是运行热键,默认“Alt+;”】
; 在ahk中,“Alt”等于“!”,
hotkey1:="!;"
; 尝试运行热键1,失败后运行默认“Alt+;”
try
{
Hotkey(hotkey1, create_activity_menu)
}
Catch
{
hotkey1:="!;"
Hotkey(hotkey1, create_activity_menu)
}
A_TrayMenu.Delete() ; 删除官方的托盘菜单项
A_TrayMenu.Add() ; 创建分隔线.
; 创建托盘的菜单项
A_TrayMenu.Add("重新加载", reload_app)
A_TrayMenu.Add("退出", exit_app)
; 托盘右键菜单函数
; 重新加载
reload_app(ItemName, ItemPos, MyMenu)
{
Reload()
}
; 退出
exit_app(ItemName, ItemPos, MyMenu)
{
ExitApp()
}
; 对纯数字的数组进行排序
SortNumArray(arr) {
str := ""
for k, v in arr
str .= v . "`n"
str := Sort(RTrim(str, "`n"), "N")
return StrSplit(str, "`n")
}
; 创建活动程序菜单
create_activity_menu(ThisHotkey){
FileMenu := Menu()
FileMenu.Delete()
; 获取目前处于焦点的程序信息
; active_id := WinGetID("A")
; active_exe := WinGetProcessName("A")
; active_name := WinGetTitle("A")
; MsgBox(active_id active_exe active_name)
; 获取所有活动的程序,得到id数组
ids := WinGetList(,, "Program Manager")
; 给ids排序,这可以有效固定程序列表
ids := SortNumArray(ids)
; num_arr := ["1","2","3","4","5","q","w","e","r","t","a","s","d","f","g","6","7","8","9","0","y","u","i","o","p","h","j","k","l","z","x","c","v","b","n","m",]
; 【new_ids】用来存放程序id之类的信息,方便之后的【MenuHandler】调用
global new_ids :=[]
; 【all_activity_info_arr】用来方便复制到剪切板
global all_activity_info_arr :=[]
count := 0
for this_id in ids
{
; WinActivate this_id
; 根据id获取程序的各种信息
this_class := WinGetClass("ahk_id" this_id)
this_title := WinGetTitle("ahk_id" this_id)
this_path := WinGetProcessPath("ahk_id" this_id)
this_name := WinGetProcessName("ahk_id" this_id)
; MsgBox(A_Index)
; 确保标题和路径有效
if (this_title != "" && this_path != "")
{
count := count+1
key := num_arr[count]
FileMenu.Add(key . " " . StrReplace(this_title, "&", " "), MenuHandler)
FileMenu.SetIcon(key . " " . StrReplace(this_title, "&", " "), this_path)
; 将程序详情按顺序添加进数组【new_ids】,方便之后的【MenuHandler】调用id
new_ids.Push(this_id)
new_ids[count]:=[]
new_ids[count].Push(this_id)
new_ids[count].Push(this_title)
new_ids[count].Push(this_path)
new_ids[count].Push(this_class)
new_ids[count].Push(this_name)
; 将程序详情添加到数组,方便之后复制
all_activity_info_arr.Push(key . " " . this_id . " " . this_path . " " . this_name . " " . this_title)
}
; 到最后一个值时,添加写入剪切板的选项,方便获取程序信息,加入排除选项
if (A_Index == ids.Length){
global all_activity_info := ""
For i, v in all_activity_info_arr{
all_activity_info .= "`n" . v
}
; MsgBox all_activity_info
FileMenu.Add("?" . " 点击复制以上所有程序的详情", copy_info)
; FileMenu.SetIcon("?" . " 点击复制以上所有程序的详情", this_path)
}
}
FileMenu.Show()
return
}
; 下面函数主要用来激活窗口
MenuHandler(ItemName, ItemPos, MyMenu) {
selection_id := new_ids[ItemPos][1]
WinActivate("ahk_id" selection_id)
; MsgBox("`n" . "id:" . new_ids[ItemPos][1] . "`n" . "title:" . new_ids[ItemPos][2] . "`n" . "path:" . new_ids[ItemPos][3] . "`n" . "class:" . new_ids[ItemPos][4] . "`n" . "name:" . new_ids[ItemPos][5] )
}
; 复制所有程序详情进入剪切板
copy_info(ItemName, ItemPos, MyMenu) {
A_Clipboard := all_activity_info
}