【问题标题】:Does this autokey Python script require concurency?这个自动键 Python 脚本是否需要并发?
【发布时间】:2012-06-02 09:59:27
【问题描述】:

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

我需要做的: 当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。

【问题讨论】:

    标签: python ubuntu concurrency hotkeys autokey


    【解决方案1】:

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

    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
    

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

    【讨论】:

    • 完美!谢谢:)今晚我会试一试,然后告诉你进展如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2018-10-22
    相关资源
    最近更新 更多