增加codermate.py

This commit is contained in:
ShowerX 2021-02-03 15:06:46 +08:00
parent f909724592
commit 09276fa2f4
2 changed files with 62 additions and 1 deletions

View File

@ -1,3 +1,6 @@
# Python # Python
收集的一些python代码供学习 收集的一些python代码供学习
## 1.python_test
>输入//时自动切换中英文输入法

58
codermate.py Normal file
View File

@ -0,0 +1,58 @@
# !/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()