主要功能是帮你快速打开你需要的软件,所以叫做启动器(launcher),相当于简易版的Listary
下载方法:
- 确保你的电脑上安装了AutoHotKey这个软件,可以从官网链接下载安装包
- 把源代码保存为一个叫做 Launcher.ahk 的文件
- 双击运行(注意,不会显示窗口,只会产生托盘图标,等你按下快捷键才会出现窗口)
使用方法:
- 按快捷键 Ctrl + Alt + L,会弹出一个搜索框。
- 打字搜索:比如你想开 EmEditor,就直接打 emed 就行,大小写无所谓,甚至打 em 就能看到了。它会把名称匹配的程序都列出来。
- 选择启动:选中你想开的那个,用鼠标双击就行。
实现非常简单,每次你按快捷键,它都会把你电脑的开始菜单、Program Files这些地方的软件和快捷方式都扫一遍,你打字的时候,它就在这些记录里帮你找。没做什么优化,响应速度还行,能用。
完整代码如下:
#NoEnv ; Recommended for performance and compatibility
#SingleInstance Force ; Ensures only one instance runs
SetWorkingDir %A_ScriptDir% ; Consistent starting directory
; Global variables
global programList := []
global maxResults := 15
; GUI control variables are automatically global within their GUI's context,
; but explicitly declaring them can sometimes aid clarity.
global SearchBox
global ResultList
; Main hotkey (Ctrl+Alt+L)
^!L::ShowLauncher()
; ==================== FUNCTIONS ====================
InitializePrograms() {
global programList ; Ensure we are modifying the global variable
programList := []
; Scan common program locations
ScanDirectory(A_StartMenu, 2)
ScanDirectory(A_StartMenuCommon, 2)
ScanDirectory("C:\Program Files", 1)
ScanDirectory("C:\Program Files (x86)", 1)
ScanDirectory("C:\Programs", 2)
}
ScanDirectory(directory, maxDepth, currentDepth := 0) {
global programList
if !FileExist(directory)
return
; Find shortcuts and executables
Loop, Files, %directory%\*.lnk, F
{
program := {}
program.name := RegExReplace(A_LoopFileName, "\.lnk$")
program.path := A_LoopFileFullPath
program.type := "shortcut"
programList.Push(program)
}
Loop, Files, %directory%\*.exe, F
{
program := {}
program.name := RegExReplace(A_LoopFileName, "\.exe$")
program.path := A_LoopFileFullPath
program.type := "executable"
programList.Push(program)
}
; Recurse through subdirectories up to max depth
if (currentDepth < maxDepth) {
Loop, Files, %directory%\*, D
{
ScanDirectory(A_LoopFileFullPath, maxDepth, currentDepth + 1)
}
}
}
ShowLauncher() {
InitializePrograms()
; Create GUI with a name
Gui, Launcher:New, +AlwaysOnTop +ToolWindow, Quick Launcher
Gui, Launcher:Color, FFFFFF
Gui, Launcher:Font, s11, Segoe UI
Gui, Launcher:Add, Edit, w500 h30 vSearchBox gUpdateSearch
Gui, Launcher:Add, ListView, r%maxResults% w500 NoSort -Multi vResultList gResultListClick, Name|Path
LV_ModifyCol(1, "400") ; Adjust column widths
LV_ModifyCol(2, "AutoHdr")
; Position in center-top of screen
Gui, Launcher:Show, Center y200
; Focus on search box
GuiControl, Launcher:Focus, SearchBox
}
UpdateSearch() {
; _FIX_: Set the default GUI for this thread
Gui, Launcher:Default
Gui, Submit, NoHide
GuiControlGet, query, , SearchBox
; Clear previous results
LV_Delete()
if (Trim(query) = "")
return
; Search through program list
matches := []
query := Format("{:L}", query) ; Convert to lowercase
for _, program in programList {
programName := Format("{:L}", program.name)
if (InStr(programName, query)) {
matches.Push(program)
}
}
; Display results
for i, match in matches {
if (i > maxResults)
break
LV_Add("", match.name, match.path)
}
; Select first result
if (LV_GetCount() > 0)
LV_Modify(1, "Select Focus")
}
ResultListClick() {
; This function is called by a GUI event, so context is already set.
if (A_GuiEvent = "DoubleClick") {
LaunchSelectedProgram()
}
}
LaunchSelectedProgram() {
; _FIX_: Set the default GUI for this thread
Gui, Launcher:Default
row := LV_GetNext()
if (row > 0) {
LV_GetText(name, row, 1)
LV_GetText(path, row, 2)
Gui, Destroy
Run, %path%
}
}
; ==================== CONTROLS ====================
#IfWinActive Quick Launcher
; _FIX_: Use $ prefix to hook the keypress before the Edit control gets it.
$Up::
; _FIX_: Set the default GUI context for this hotkey thread.
Gui, Launcher:Default
if (LV_GetCount() = 0)
return
row := LV_GetNext()
if (row = 0)
LV_Modify(LV_GetCount(), "Select Focus") ; Select last if nothing is selected
else if (row > 1)
LV_Modify(row - 1, "Select Focus")
else
LV_Modify(LV_GetCount(), "Select Focus") ; Wrap to bottom
return
$Down::
; _FIX_: Set the default GUI context for this hotkey thread.
Gui, Launcher:Default
if (LV_GetCount() = 0)
return
row := LV_GetNext()
if (row = 0)
LV_Modify(1, "Select Focus") ; Select first if nothing is selected
else if (row < LV_GetCount())
LV_Modify(row + 1, "Select Focus")
else
LV_Modify(1, "Select Focus") ; Wrap to top
return
$Enter::
LaunchSelectedProgram()
return
Escape::
Gui, Launcher:Destroy
return
#IfWinActive