Double press = Hotkey

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Skribe
Posts: 13
Joined: 30 May 2023, 23:37

Double press = Hotkey

Post by Skribe » 30 May 2023, 23:43

I'm really new to all this and have spent a good chunk of time already reading posts that have asked this same question to see what the replies have said. Unfortunately given how new I am a lot of it goes way over my head.

Basically I'm trying to press Caps Lock twice in a row which will trigger Alt+F12

Saiapatsu
Posts: 17
Joined: 11 Jul 2019, 15:02

Re: Double press = Hotkey

Post by Saiapatsu » 31 May 2023, 04:01

I've been meaning to write a script like this to prove a point to someone.
You don't need to fully understand it, this script abuses an AHK feature in an unusual way.

Code: Select all

; When CapsLock is pressed, call PressedCaps(). Still toggles CapsLock
; because PressedCaps() returns 0, meaning the actual hotkey itself
; (the return) will not run and the original key passes through.
#HotIf PressedCaps()
CapsLock::return
#HotIf

; If this function was called twice within 200 milliseconds, send Alt-F12.
PressedCaps()
{
	static lastPress := 0
	if A_TickCount < lastPress + 200
	{
		lastPress := 0
		; We can't actually Send "!{F12}" here because sending another key
		; in a HotIf expression causes horrible jank. Instead, schedule the
		; Alt-F12 to be sent as soon as possible, but not immediately.
		SetTimer SendX, -1
	} else {
		lastPress := A_TickCount
	}
	; Prevent the hotkey from running.
	return 0
	; Change that 0 to 1 to make the hotkey run. In this case, the hotkey
	; is return, so pressing CapsLock will do nothing else (return without
	; doing anything)
}

SendX()
{
	; This is AutoHotkey's key sending syntax at work. ! stands for "send
	; the next key while pressing Alt" and {} is needed around the F12
	; because the key's name is not just one character.
	Send "!{F12}"
}

neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

Re: Double press = Hotkey

Post by neogna2 » 31 May 2023, 05:55

This less advanced code might work well in this case because pressing twice keeps the previous CapsLock toggle state (uppercase/lowercase).

Code: Select all

~CapsLock:: 
{
    if (A_PriorHotkey = A_ThisHotkey) && (A_TimeSincePriorHotkey < 500)
        Send("!{F12}")
}

Post Reply

Return to “Ask for Help (v2)”