| Python全代码如下 文章尾部附带国际莫斯码表 示例: |
'''英语->国际莫斯码转换器执行代码之后会自动检测按键,按下Ctrl+Z之后程序读取剪贴板然后转换为莫斯码再次写入剪贴板使用第三方模块:pynputwin32clipboard'''import win32clipboardimport timefrom pynput.keyboard import Key, Listener, HotKeyfrom multiprocessing import Processfrom threading import Threadclass Detect_key(): ''' 实时检测键盘输入 ''' def __init__(self,keys): self.lisener=None def handle_s(): with Listener(on_press = self.on_press,on_release = self.on_release) as self.lisener: self.lisener.join() def handle_c(): with Listener(on_press=self.for_canonical(hotkey.press), on_release=self.for_canonical(hotkey.release)) as self.l: self.l.join() lis_1=Thread(target=handle_s) lis_1.start() hotkey = HotKey(HotKey.parse(keys),self.on_activate) lis_2=Thread(target=handle_c) lis_2.start() #莫斯码表,换行换做为3个空格 self.eng_morse={'a' : '.-', 'b' : '-...', 'c' : '-.-.', 'd' : '-..', 'e' : '.', 'f' : '..-.', 'g' : '--.', 'h' : '....', 'i' : '..', 'j' : '.---', 'k' : '-.-', 'l' : '.-..', 'm' : '--', 'n' : '-.', 'o' : '---', 'p' : '.--.', 'q' : '--.-', 'r' : '.-.', 's' : '...', 't' : '-', 'u' : '..-', 'v' : '...-', 'w' : '.--', 'x' : '-..-', 'y' : '-.--', 'z' : '--..', '.' : '.-.-.-', '?' : '..--..', ',' : '--..--', '1':'.----','2':'..---','3':'...--','4':'....-','5':'.....','6':'-....','7':'--....','8':'---..', '9':'----.','0':'-----',' ':' ',chr(10):' '} def on_press(self,key): ''' 单个按键按下 ''' if key == Key.esc: self.lisener.stop() self.l.stop() def on_release(self,key): ''' 单个按键抬起 ''' pass def on_activate(self,): ''' 组合键按下 ''' win32clipboard.OpenClipboard() #打开剪贴板 try: result = win32clipboard.GetClipboardData() except TypeError: result = '' #非文本 morse='' for char in result: if char in self.eng_morse: morse += self.eng_morse[char] morse += ' ' #加入空格 print(morse) win32clipboard.EmptyClipboard() win32clipboard.SetClipboardText(morse) win32clipboard.CloseClipboard() def for_canonical(self,f):#pynput模块连接 return lambda k: f(self.l.canonical(k))s_key=Detect_key('+z')#输入组合按键



