Skip to main content
 首页 » 编程设计

python之这个 autokey Python 脚本需要并发吗

2025年05月04日8rubylouvre

我最近切换到 Ubuntu,我想念 autohotkey。我正在使用 autokey重新创建我的热键环境。我重新映射了 CapsLock至 F13使用 Xmodmap .

我需要做的: 当F13被点击,返回<Esc> . 当F13与键一起使用,触发热键。 当F13按住 1 秒以上,无热键释放,不返回任何内容。

Autokey 使用 Python 环境。这是我的计划:

    F13 is pressed 
        Start a timer 
        Start a thread listening for <CapsLock up> and if true,  
            if timer is less than 1 second && no hotkey was pressed 
                exit script after returning <Esc>  
            exit script 
 
 
        Start a thread that loops forever 
            Listen for hotkey 
                Play hotkey's function 

脚本在 CapsLock 时结束发行了。

示例:我按 CapsLock然后j输出是Down arrow .

我在开始编码之前的问题是,我真的需要为此使用多线程(并发)吗?这是最好的方法吗?我觉得有一种更简单的方法,而且我也从未使用并发编码。

编辑:我愿意接受任何方法来实现这一点,即使它不是使用 autokey 或 python。

请您参考如下方法:

在这种情况下你绝对不需要使用线程。你可以做这样的事情:

F13 is pressed 
Start timer 
While True: 
    Listen for hotkey and capslock up 
    if capslock up: 
        if timer < 1:  
            return <Esc> and exit 
        else: just exit 
    elif hotkey: 
        Execute hotkey function and exit 

我们退出的唯一两种方式是释放大写锁定或按下热键,这两种方式中只有一种是我们需要担心的事件,因此我们可以在同一个线程中监听这两种方式。