From 09276fa2f4279b6a379007620cbf8aaa22e9a42b Mon Sep 17 00:00:00 2001 From: ShowerX Date: Wed, 3 Feb 2021 15:06:46 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0codermate.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- codermate.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 codermate.py diff --git a/README.md b/README.md index 219fcad..4920d8a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # Python -收集的一些python代码供学习 \ No newline at end of file +收集的一些python代码供学习 + +## 1.python_test +>输入//时自动切换中英文输入法 \ No newline at end of file diff --git a/codermate.py b/codermate.py new file mode 100644 index 0000000..4859c91 --- /dev/null +++ b/codermate.py @@ -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()