59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
|
# !/usr/bin/env python
|
|||
|
# -*- coding: utf-8 -*-
|
|||
|
|
|||
|
from pynput import keyboard
|
|||
|
from pynput.keyboard import Key, Controller
|
|||
|
|
|||
|
|
|||
|
def on_press(key):
|
|||
|
try:
|
|||
|
# print('alphanumeric key {0} pressed'.format(key.char))
|
|||
|
pass
|
|||
|
except AttributeError:
|
|||
|
# print('special key {0} pressed'.format(key))
|
|||
|
pass
|
|||
|
|
|||
|
|
|||
|
def on_release(key):
|
|||
|
global VirguleCnt, InputType
|
|||
|
global keytemp
|
|||
|
global VirguleCnt
|
|||
|
|
|||
|
try:
|
|||
|
# print('alphanumeric key {0} pressed'.format(key.char))
|
|||
|
if key.char == '/':
|
|||
|
VirguleCnt += 1
|
|||
|
# print(VirguleCnt)
|
|||
|
if ((VirguleCnt == 2) & (InputType == 0)):
|
|||
|
print("连续监测到// + 英文状态,切换到中文")
|
|||
|
InputType = 1
|
|||
|
keytemp.press(Key.shift)
|
|||
|
keytemp.release(Key.shift)
|
|||
|
else:
|
|||
|
VirguleCnt = 0
|
|||
|
except AttributeError:
|
|||
|
# print('special key {0} pressed'.format(key))
|
|||
|
VirguleCnt = 0
|
|||
|
if key == keyboard.Key.esc: # ESC,停止监听
|
|||
|
return False
|
|||
|
# elif key == keyboard.Key.shift: # 模拟和手动shift,都会发生执行此段程序
|
|||
|
# VirguleCnt = 10
|
|||
|
elif key == keyboard.Key.enter: # 换行
|
|||
|
if (InputType == 1): # 中文
|
|||
|
print("换行 + 中文状态,切换到英文")
|
|||
|
InputType = 0
|
|||
|
keytemp.press(Key.shift)
|
|||
|
keytemp.release(Key.shift)
|
|||
|
else:
|
|||
|
pass
|
|||
|
|
|||
|
|
|||
|
# 写程序注释无缝超顺滑切换的python小程序
|
|||
|
if __name__ == "__main__":
|
|||
|
VirguleCnt = 0
|
|||
|
InputType = 0 # 默认初始为英文输入状态
|
|||
|
keytemp = Controller()
|
|||
|
|
|||
|
with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
|
|||
|
listener.join()
|