How do I fire a prefix key only if it's released within a period of time?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
nanocyte
Posts: 2
Joined: 19 May 2022, 08:46

How do I fire a prefix key only if it's released within a period of time?

Post by nanocyte » 19 May 2022, 10:02

I have a bunch of hotkeys using prefix keys. Is it possible to cleanly have the prefix key (when used alone) fire only if less than 1 second has passed between key down and key up?

This is so that if I press a key intending to use it as a modifier but then change my mind (or forget which key I assigned the hotkey to), it will function differently than if I tap the key quickly to deliberately trigger its single-key function.

So, just as an example to illustrate what I mean, I'm trying to do something like this:

Code: Select all

Capslock & j::down

Capslock::
    If (physical key down happened less than 1 second ago)
        Send, {escape}
    Else
        Don't do anything
Is there a clean way to do this while still using the prefix key syntax? Can I pull its key down event from key history or something?

Thanks!

braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: How do I fire a prefix key only if it's released within a period of time?

Post by braunbaer » 19 May 2022, 11:16

Code: Select all

Capslock & j::
      send down                                        
      Justpressed:=true
      settimer, forgetpressed, -1000   ; one second
      return 

forgetpressed:
     Justpressed := false
     return

#if Justpressed
Capslock::
     Send, {escape}
     Justpressed := false
     return
#if

nanocyte
Posts: 2
Joined: 19 May 2022, 08:46

Re: How do I fire a prefix key only if it's released within a period of time?

Post by nanocyte » 22 May 2022, 00:32

Thanks, but is there any way to do this while leaving my other hotkeys clean? All of the down events are in KeyHistory, so if I could even somehow grab the first down event for the key when it's released, it would be easy.

I guess this is fairly trivial for keys that don't naturally output anything.

So I guess I could do capslock with:

Code: Select all

~capslock::
    KeyWait, CapsLock
    If (A_PriorKey = "CapsLock" and A_TimeSinceThisHotkey < 400)
      Send, {Esc}
But that won't work for keys that send output, as ~enter would let the enter key through.

And it wouldn't help to assign a global variable inside one of my other hotkeys, because if they're called, Enter wont need to be called.

I guess I could set a variable tracking enter's state, start a timer there, have it fire its normal functions on key up if the conditions pass, and then setting a section of conditional hotkeys that are only active if enter is down.

But that seems kind of hacky, and I worry that having that many conditional hotkeys might be unreliable. But maybe I'm worrying about nothing.

Post Reply

Return to “Ask for Help (v1)”