首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoKey缩写替代

AutoKey缩写替代
EN

Ask Ubuntu用户
提问于 2020-12-05 14:32:42
回答 1查看 448关注 0票数 0

我想要的是:编写@m将代替myemail@mail.com

问题:自动键对此不能很好地工作(您编写@m,它将添加myemail@mail.com,然后删除指向:@mmyemail@mail.c的两个字符)。这是一个古老的问题 of autoKey。

如何在没有autoKey的情况下做到这一点?

EN

回答 1

Ask Ubuntu用户

发布于 2020-12-05 14:32:42

我最终构建了一个python脚本来完成它。(在python3.8上用gnome测试了ubuntu 20.04 )。

脚本

代码语言:javascript
复制
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并将其粘贴到当前窗口中。

Notes

这个例子是使用字符@,这个字符很难用pyautogui打印。这就是我使用pyperclip的原因(参见这里那里)。对于非常简单的用例,您可以使用pyautogui.typewrite(abbrevations[input])代替最新脚本的两行。

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1297695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档