如何写一个脚本快捷输入

wen 实用脚本 1

本文目录导读:

如何写一个脚本快捷输入

  1. 键盘快捷键脚本(AutoHotkey - Windows)
  2. macOS 快捷文字(系统内置)
  3. 终端/Shell 快捷命令
  4. Python 桌面工具(跨平台)
  5. 网页端快捷输入
  6. 实用技巧和建议
  7. 使用建议

我来介绍几种脚本快捷输入的方法,涵盖不同平台和使用场景:

键盘快捷键脚本(AutoHotkey - Windows)

基础文本展开

; 常用信息快速输入
::邮箱::user@example.com
::电话::138-0000-0000
::地址::北京市朝阳区XX路XX号
; 带当前日期
::日期::
FormatTime, CurrentDateTime,, yyyy-MM-dd
SendInput %CurrentDateTime%
return
; 多行文本
::签名::
(
此致
敬礼
张三
)
return

快捷键触发

; Ctrl+Alt+E 打开邮箱
^!e::
Run, mailto:user@example.com
return
; Win+Q 快速搜索
#q::
InputBox, SearchTerm, 快速搜索, 输入搜索内容
Run, https://www.baidu.com/s?wd=%SearchTerm%
return

macOS 快捷文字(系统内置)

系统设置方法

  1. 打开 系统设置键盘文本替换
  2. 点击 "+" 添加新条目
  3. 设置替换快捷键和对应文本

使用 Alfred/ Raycast(第三方工具)

-- Alfred 工作流示例
on alfred_script(q)
  if q is "email" then
    return "user@example.com"
  else if q is "date" then
    return do shell script "date +%Y-%m-%d"
  end if
end alfred_script

终端/Shell 快捷命令

Bash/Zsh 别名

# ~/.bashrc 或 ~/.zshrc 中添加
alias gs='git status'
alias gl='git log --oneline'
alias ..='cd ..'
# 快速打开常用目录
alias work='cd ~/Documents/Projects/Work'
alias docs='cd ~/Documents'
# 快速发送文本
alias email='echo "user@example.com" | pbcopy'  # macOS
alias email='echo "user@example.com" | clip'     # Windows

自定义脚本目录

# 创建脚本目录
mkdir -p ~/scripts
# 创建快捷脚本 ~/scripts/send_email.sh
#!/bin/bash
# 快速发送常用文本
echo "user@example.com" | clip  # Windows
# 或 echo "user@example.com" | pbcopy  # macOS
# 添加到PATH
echo 'export PATH="$HOME/scripts:$PATH"' >> ~/.bashrc

Python 桌面工具(跨平台)

# quick_input.py
import pyautogui
import pyperclip
import time
def quick_type(text, delay=0.5):
    """模拟键盘输入"""
    pyperclip.copy(text)
    pyautogui.hotkey('ctrl', 'v')  # Windows
    # pyautogui.hotkey('command', 'v')  # macOS
# 常用输入
def email():
    quick_type("user@example.com")
def date():
    from datetime import datetime
    quick_type(datetime.now().strftime("%Y-%m-%d"))
# 主程序 - 根据参数执行
import sys
if __name__ == "__main__":
    command = sys.argv[1] if len(sys.argv) > 1 else "email"
    eval(command + "()")

网页端快捷输入

书签小工具(JavaScript)

// 创建书签,内容为以下代码
javascript:(function(){
  var templates = {
    'email': 'user@example.com',
    'address': '北京市朝阳区XX路',
    'note': '【重要】请尽快回复'
  };
  var key = prompt('输入关键词:');
  if (templates[key]) {
    document.execCommand('insertText', false, templates[key]);
  }
})();

油猴脚本示例

// ==UserScript==
// @name         快捷输入
// @match        https://*
// @grant        none
// ==/UserScript==
(function() {
    // 监听键盘事件
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.shiftKey && e.key === '1') {
            insertText('user@example.com');
        }
    });
    function insertText(text) {
        const activeElement = document.activeElement;
        if (activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
            const start = activeElement.selectionStart;
            const end = activeElement.selectionEnd;
            activeElement.value = activeElement.value.substring(0, start) + 
                                   text + 
                                   activeElement.value.substring(end);
        }
    }
})();

实用技巧和建议

创建快捷启动菜单

@echo off
:: Windows 批处理 - shortcuts.bat
echo ========== 快捷输入菜单 ==========
echo 1. 常用邮箱
echo 2. 手机号码
echo 3. 当前日期
echo 4. 公司地址
set /p choice="请选择: "
if "%choice%"=="1" echo user@example.com | clip
if "%choice%"=="2" echo 13800000000 | clip
if "%choice%"=="3" echo %date% | clip
if "%choice%"=="4" echo XX公司地址内容 | clip

统一管理配置

创建配置文件 .quick_inputs.json

{
  "email": "user@example.com",
  "phone": "13800000000",
  "address": "北京市朝阳区",
  "company": "XX科技有限公司",
  "bank": "招商银行 6225********1234"
}

使用建议

  1. 分类管理:按用途分类(个人信息、工作、常用语)
  2. 安全考虑:密码等敏感信息不建议存储
  3. 组合使用:配合系统快捷键触发
  4. 测试验证:首次使用先测试是否正常工作
  5. 备份配置:定期备份脚本配置

选择哪种方案取决于你的主要使用场景和操作系统,如果需要在多个平台使用相同的快捷输入,建议使用跨平台的Python方案,如果主要在Windows下使用,AutoHotkey是最灵活的,MacOS用户可以优先考虑系统内置功能。

抱歉,评论功能暂时关闭!