本文目录导读:

我来帮你写一个自动切换输入法的脚本,这里提供几个常用的方案:
方案1:Python 脚本(Windows)
import subprocess
import time
import win32gui
import win32con
from ctypes import *
import os
class InputMethodSwitcher:
def __init__(self):
# 常用输入法代码
self.IM_ENGLISH = "0409:00000409" # 美式键盘
self.IM_CHINESE = "0804:00000804" # 微软拼音
def switch_to(self, language_code):
"""切换到指定的输入法"""
try:
# 使用Windows API切换输入法
subprocess.run(f'powershell -Command "Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.InputLanguage]::CurrentInputLanguage = [System.Windows.Forms.InputLanguage]::FromCulture([System.Globalization.CultureInfo]::GetCultureInfo(\'{language_code}\'))"', shell=True)
return True
except Exception as e:
print(f"切换输入法失败: {e}")
return False
def switch_to_chinese(self):
"""切换到中文输入法"""
return self.switch_to(self.IM_CHINESE)
def switch_to_english(self):
"""切换到英文输入法"""
return self.switch_to(self.IM_ENGLISH)
# 使用示例
switcher = InputMethodSwitcher()
# 根据条件自动切换
def auto_switch_input(based_on_app):
"""根据当前应用程序自动切换输入法"""
current_app = get_current_application()
if current_app in ["notepad", "Word", "WeChat"]:
switcher.switch_to_chinese()
elif current_app in ["cmd", "Visual Studio Code", "PyCharm"]:
switcher.switcher_to_english()
def get_current_application():
"""获取当前活动的应用程序"""
import psutil
import pygetwindow as gw
current_window = gw.getActiveWindow()
if current_window:
return current_window.title
return ""
方案2:AutoHotkey 脚本(Windows)
; 自动切换输入法脚本
#Persistent
SetTimer, CheckAndSwitch, 1000 ; 每秒检查一次
CheckAndSwitch:
WinGetActiveTitle, ActiveTitle
WinGetClass, ActiveClass, A
; 根据窗口标题或类判断
if (ActiveTitle contains "微信" or ActiveTitle contains "QQ")
{
; 切换到中文输入法(微软拼音)
Send, {LWin}{Space}
Sleep, 100
Send, {Down}{Down}{Enter}
}
else if (ActiveTitle contains "Visual Studio" or ActiveTitle contains "notepad")
{
; 切换到英文输入法
Send, {LWin}{Space}
Sleep, 100
Send, {Up}{Enter}
}
return
; 快捷键手动切换
^!c:: ; Ctrl+Alt+C 切换
Send, {LWin}{Space}
return
^!e:: ; Ctrl+Alt+E 切换到英文
Send, {LWin}{Space}
Sleep, 100
Send, {Down}{Enter}
return
方案3:PowerShell 脚本
# 自动切换输入法脚本
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# 获取当前活跃窗口
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class WinAPI {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hWnd, System.Text.StringBuilder text, int count);
}
"@
function Switch-InputMethod {
param([string]$LanguageCode)
$inputLanguage = [System.Windows.Forms.InputLanguage]::FromCulture(
[System.Globalization.CultureInfo]::GetCultureInfo($LanguageCode)
)
[System.Windows.Forms.InputLanguage]::CurrentInputLanguage = $inputLanguage
}
# 主循环
while ($true) {
$hwnd = [WinAPI]::GetForegroundWindow()
$title = New-Object System.Text.StringBuilder 256
[WinAPI]::GetWindowText($hwnd, $title, 256) | Out-Null
$windowTitle = $title.ToString()
# 根据窗口标题判断
if ($windowTitle -match "微信|QQ|Word|Excel") {
Switch-InputMethod "zh-CN" # 中文
}
elseif ($windowTitle -match "Visual Studio|notepad|cmd") {
Switch-InputMethod "en-US" # 英文
}
Start-Sleep -Seconds 2
}
方案4:macOS 脚本(AppleScript)
-- 自动切换输入法(macOS)
use AppleScript version "2.4"
use scripting additions
-- 获取当前应用程序
global currentApp
on checkCurrentApp()
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
return frontApp
end tell
end checkCurrentApp
on switchInputMethod(languageName)
tell application "System Events"
-- 使用快捷方式切换输入法(根据你的系统设置调整)
if languageName is "Pinyin" then
key code 49 using {control down, space down}
else
key code 49 using {control down, space down}
end if
end tell
end switchInputMethod
-- 主循环
repeat
set currentApp to checkCurrentApp()
if currentApp is "微信" or currentApp is "QQ" then
switchInputMethod("Pinyin")
else if currentApp is "Xcode" or currentApp is "Terminal" then
switchInputMethod("ABC")
end if
delay 2
end repeat
使用建议
-
选择适合你的方案:
- Windows用户推荐使用AutoHotkey(轻量)或Python(功能强大)
- macOS用户使用AppleScript
-
自定义规则:
- 修改脚本中的窗口识别条件
- 调整检测频率(默认1-2秒)
-
安装依赖(Python方案需要):
pip install pywin32 psutil
-
运行方式:
- AutoHotkey:双击运行.ahk文件
- Python:保存为.py文件运行
- PowerShell:以管理员身份运行
需要根据你的具体使用场景调整,比如特定的应用程序、输入法类型等,有问题欢迎继续问我!