生活随笔
收集整理的這篇文章主要介紹了
读取剪贴板英语转换为国际莫斯码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
| Python全代碼如下 執(zhí)行代碼之后會(huì)自動(dòng)檢測按鍵,按下Ctrl+Z之后程序讀取剪貼板然后轉(zhuǎn)換為莫斯碼再次寫入剪貼板 使用第三方模塊: pynput win32clipboard 文章尾部附帶國際莫斯碼表 示例: Caesar openly defied the Senate's authority by?crossing the Rubicon?and marching towards Rome at the head of an army.
.-? .? ...? .-? .-.? ? ?---? .--.? .? -.? .-..? -.--? ? ?-..? .? ..-.? ..? .? -..? ? ?-? ....? .? ? ?.? -.? .-? -? .? ...? ? ?.-? ..-? -? ....? ---? .-.? ..? -? -.--? ? ?-...? -.--? ? ?-.-.? .-.? ---? ...? ...? ..? -.? --.? ? ?-? ....? .? ? ?..-? -...? ..? -.-.? ---? -.? ? ?.-? -.? -..? ? ?--? .-? .-.? -.-.? ....? ..? -.? --.? ? ?-? ---? .--? .-? .-.? -..? ...? ? ?---? --? .? ? ?.-? -? ? ?-? ....? .? ? ?....? .? .-? -..? ? ?---? ..-.? ? ?.-? -.? ? ?.-? .-.? --? -.--? .-.-.-?? |
'''
英語->國際莫斯碼轉(zhuǎn)換器執(zhí)行代碼之后會(huì)自動(dòng)檢測按鍵,按下Ctrl+Z之后程序讀取剪貼板然后轉(zhuǎn)換為莫斯碼再次寫入剪貼板使用第三方模塊:
pynput
win32clipboard
'''import win32clipboard
import time
from pynput.keyboard import Key, Listener, HotKey
from multiprocessing import Process
from threading import Threadclass Detect_key():'''實(shí)時(shí)檢測鍵盤輸入'''def __init__(self,keys):self.lisener=Nonedef 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個(gè)空格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):'''單個(gè)按鍵按下'''if key == Key.esc:self.lisener.stop()self.l.stop()def on_release(self,key):'''單個(gè)按鍵抬起'''passdef 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('<ctrl>+z')#輸入組合按鍵
?
總結(jié)
以上是生活随笔為你收集整理的读取剪贴板英语转换为国际莫斯码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。