Windows 快速启动器

在日常工作和学习中,我们经常需要快速访问某些功能或资源,例如打开常用网站、启动应用程序或执行特定命令。Linux 用户可以通过 alias 快速实现这些操作,但在 Windows 上缺乏类似的原生支持。本文将介绍如何使用 Python 开发一个 隐藏式文本输入框程序,模拟 Linux 的 alias 功能,实现快速启动和访问。


功能概述

该程序的核心功能如下:

  1. 隐藏式界面:默认情况下,程序界面隐藏,按下热键后显示。

  2. 快速输入:通过键盘输入关键字,按下回车键后执行相应操作。

  3. 自定义映射:通过配置文件(如 store.txt)定义关键字与操作的映射关系。

  4. 快速打开网站:输入关键字后,自动打开对应的 URL。

  5. 打开文件夹:输入关键字后,打开指定的文件夹。

  6. 启动应用程序:输入关键字后,启动指定的应用程序。

  7. 执行系统命令:输入关键字后,执行系统命令(如打开服务管理器)。

  8. 单实例运行:确保程序只运行一个实例,避免重复启动。


实现原理

1. 隐藏式界面

使用 tkinter 创建一个无标题栏的文本输入框界面,默认隐藏。通过全局热键(如 Alt + Win + Z)显示界面,并将焦点设置到输入框。

2. 关键字映射

通过读取配置文件(如 store.txt),将关键字与操作(如 URL、文件夹路径、应用程序路径或系统命令)关联起来。例如:

 
google https://www.google.com u python https://www.python.org u ubunt D:ubunt f services %SystemRoot%system32services.msc o notepad C:WindowsSystem32notepad.exe o

 


3. 快速执行

输入关键字后,按下回车键,程序会根据配置文件执行相应操作(如打开浏览器访问 URL、打开文件夹、启动应用程序或执行系统命令)。

4. 单实例运行

使用 psutil 检查程序是否已经运行,避免重复启动。


代码实现

以下是完整的 Python 代码:

python
复制

import tkinter as tk import keyboard import ctypes import sys import os import psutil import webbrowser import subprocess import time  class StoreItem:     def __init__(self, keyword, content, type_):         self.keyword = keyword         self.content = content         self.type_ = type_  class Store:     def __init__(self, file_path):         self.file_path = file_path         self.items = {}         self.last_modified = 0         self.load()      def load(self):         if not os.path.exists(self.file_path):             print(f"错误:未找到文件 {self.file_path},请确保 store.txt 和脚本在同一目录下。")             return          self.last_modified = os.path.getmtime(self.file_path)         self.items.clear()          with open(self.file_path, "r", encoding="utf-8") as file:             for line in file:                 columns = line.strip().split()                 if len(columns) == 3:                     keyword, content, type_ = columns                     self.items[keyword] = StoreItem(keyword, content, type_)      def get_item(self, keyword):         if os.path.getmtime(self.file_path) > self.last_modified:             self.load()         return self.items.get(keyword)  def is_already_running():     current_pid = os.getpid()     for proc in psutil.process_iter(['pid', 'name']):         try:             if proc.info['name'] == os.path.basename(sys.argv[0]) and proc.info['pid'] != current_pid:                 return True         except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):             pass     return False  if is_already_running():     print("程序已经在运行中,退出当前实例。")     sys.exit(0)  user32 = ctypes.windll.user32 imm32 = ctypes.windll.imm32  def show_window():     if not root.winfo_viewable():         root.deiconify()     root.lift()     root.focus_force()     screen_width = root.winfo_screenwidth()     screen_height = root.winfo_screenheight()     window_width = root.winfo_reqwidth()     window_height = root.winfo_reqheight()     x = screen_width - window_width - 20     y = 20     root.geometry(f"+{x}+{y}")     text_entry.focus_set()     text_entry.delete(0, tk.END)     switch_to_english()  def hide_window():     root.withdraw()  def on_enter(event):     input_text = text_entry.get()     if input_text.strip():         ontext(input_text)     text_entry.delete(0, tk.END)     hide_window()  def handle_action(item):     if item.type_ == "u":         webbrowser.open(item.content)         print(f"已打开 URL: {item.content}")     elif item.type_ == "f":         os.startfile(item.content)         print(f"已打开文件夹: {item.content}")     elif item.type_ == "o":         subprocess.run(item.content, shell=True)         print(f"已执行命令: {item.content}")  def ontext(key):     item = store.get_item(key)     if item:         handle_action(item)     else:         print(f"未找到关键字为 {key} 的记录。")  def switch_to_english():     hwnd = user32.GetForegroundWindow()     himc = imm32.ImmGetContext(hwnd)     if himc:         imm32.ImmSetOpenStatus(himc, False)         imm32.ImmReleaseContext(hwnd, himc)  root = tk.Tk() root.overrideredirect(True) root.configure(bg="white")  text_entry = tk.Entry(     root,     width=20,     font=("Arial", 12),     bd=0,     highlightthickness=1,     justify="left" ) text_entry.pack(padx=10, pady=10)  root.withdraw()  text_entry.bind("<Return>", on_enter) keyboard.add_hotkey("alt+windows+z", show_window) keyboard.add_hotkey("esc", hide_window) text_entry.bind("<FocusIn>", lambda event: switch_to_english())  script_dir = os.path.dirname(os.path.abspath(__file__)) store_path = os.path.join(script_dir, "store.txt") store = Store(store_path)  root.mainloop()

 


配置文件示例

在 store.txt 文件中添加以下内容:

 

google https://www.google.com u python https://www.python.org u ubunt D:ubunt f services %SystemRoot%system32services.msc o notepad C:WindowsSystem32notepad.exe o

 

 



功能说明

  1. 打开 URL

    • 输入 google,打开 https://www.google.com

    • 输入 python,打开 https://www.python.org

  2. 打开文件夹

    • 输入 ubunt,打开 D:ubunt 文件夹。

  3. 启动应用程序或执行系统命令

    • 输入 services,打开服务管理器。

    • 输入 notepad,启动记事本。


使用方法

  1. 将 Python 脚本和 store.txt 文件放在同一目录下。

  2. 运行 Python 脚本。

  3. 按下 Alt + Win + Z 显示界面,输入关键字后按 Enter 键。

  4. 程序会根据配置文件执行相应操作。


打包为可执行文件

使用 PyInstaller 将脚本打包为 Windows 可执行文件:

bash
复制

pyinstaller --onefile --windowed main.py

生成的 main.exe 文件位于 dist 文件夹中。


总结

通过抽象 StoreItem 和 Store 类,代码逻辑更加清晰和模块化。快速启动器 不仅支持打开网址,还能打开文件夹、启动应用程序和执行系统命令。你可以根据需要在 store.txt 中添加更多映射关系,实现快速访问常用资源的目标。希望这个工具能帮助你提高工作效率!

 

 

发表评论

评论已关闭。

相关文章