我想要的是:编写@m将代替myemail@mail.com。
问题:自动键对此不能很好地工作(您编写@m,它将添加myemail@mail.com,然后删除指向:@mmyemail@mail.c的两个字符)。这是一个古老的问题 of autoKey。
如何在没有autoKey的情况下做到这一点?
发布于 2020-12-05 14:32:42
我最终构建了一个python脚本来完成它。(在python3.8上用gnome测试了ubuntu 20.04 )。
import pyautogui
import pyperclip
import tkinter as tk
import sys
import time
# Abbrevations (you can add any abbreviation you want)
abbreviations = {
"@m": "myemail@mail.com"
}
# Global var containing the abbreviation key
input = ""
# Binding functions
def onKeyPress(event):
global input
input = input + event.char
if input in abbreviations:
root.destroy()
def close(event):
root.destroy()
exit(0)
# Create a very small tkinter window
root = tk.Tk()
root.geometry('0x0') # key binding doesn't work with root.withdraw(), instead I used a 0*0 window
root.bind('<KeyPress>', onKeyPress) # catch keys and add them to input string
root.bind('<Escape>', close) # close tkinter on escape and end this script
root.mainloop()
# Check if the program ends with an existing abbreviation key
if not input in abbreviations:
exit(0)
time.sleep(0.25) # Give time to re-trigger the current window
# Paste the given string in the current window
pyperclip.copy(abbreviations[input])
pyautogui.hotkey("ctrl", "v")您需要使用pyautogui从pip和tkinter安装sudo apt-get install python-tks。
易于使用
使此脚本可执行:chmod +x myscript.py。然后,您可以添加一个gnome键盘快捷方式(比如alt+m)来执行您的脚本(/path/to/myscript.py)。
现在您可以按下运行脚本的alt+m。如果输入错误命令或更改主意,请按“转义”。否则,您可以按@m,脚本将向剪贴板添加myemail@mail.com并将其粘贴到当前窗口中。
这个例子是使用字符@,这个字符很难用pyautogui打印。这就是我使用pyperclip的原因(参见这里和那里)。对于非常简单的用例,您可以使用pyautogui.typewrite(abbrevations[input])代替最新脚本的两行。
https://askubuntu.com/questions/1297695
复制相似问题