Hold a key for "X" amount of time, then realease to activate script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SeeDRank_A
Posts: 5
Joined: 25 Sep 2022, 22:40

Hold a key for "X" amount of time, then realease to activate script

Post by SeeDRank_A » 25 Sep 2022, 22:48

im having a hard time figuring out how to trigger my script for my smart plug. So far i got the unofficial Google Assistant for PC installed and i want the ~ key to be my Key.

i need to have Shift + ` to bring up Google assisant, but i want to have "a (key)press for X time and release" as buffer condition so i dont accidently activate it. maybe 2000ms and start from there?

Code: Select all

`::
Send, ~
KeyWait ``
Send, ~
Sleep, 1200 
Send, Vape On{Enter} ; this turns on the smart plug
Return

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hold a key for "X" amount of time, then realease to activate script

Post by Rohwedder » 26 Sep 2022, 00:54

Hallo,
adapt this example:

Code: Select all

$q::
Send, q
KeyWait, q, T2 ;waits max. 2 Seconds
IF ErrorLevel
	SoundBeep
KeyWait, q
Return
Each time the Q key is pressed down, q is written once. If the key is held down for more than 2 seconds, Beep will sound.

SeeDRank_A
Posts: 5
Joined: 25 Sep 2022, 22:40

Re: Hold a key for "X" amount of time, then realease to activate script

Post by SeeDRank_A » 26 Sep 2022, 12:46

Thank you very much! How would I modify this script to hold for T5 seconds to activate (no release needed)? I would like to do

Send, Vape Off{Enter} ;with the modified script

Do I just take out the second KeyWait line?

Thanks for the help Roh!

SeeDRank_A
Posts: 5
Joined: 25 Sep 2022, 22:40

Re: Hold a key for "X" amount of time, then realease to activate script

Post by SeeDRank_A » 26 Sep 2022, 17:16

Ok re-reading your solution that is not what i am looking for.

What i am looking for is when is

Condition 1.)
" ~ " is pressed DOWN

Condition 2.)
HOLD "~" for a given duration

Condition 3.)
"~" is RELEASED

Trigger
Send, {Shift}+different key

this seems like a better breakdown what i am looking for

I need conditions 2.) and .3) also met because of accidental keypresses since this is for electricity

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hold a key for "X" amount of time, then realease to activate script

Post by Rohwedder » 27 Sep 2022, 02:33

With my German keyboard layout the tilde ~ is only selectable via AltGr. Since my driver for the English keyboard layout is unfortunately faulty regarding the key VK_OEM_3(~ `), (I get the characters from the German keyboard layout ^ °), I have to switch to other characters:

Code: Select all

$q:: ;1.) "q" is pressed DOWN
KeyWait, q ;2.)  you HOLD "q" for a given duration
;3.)  "q" is RELEASED
If A_TimeSinceThisHotkey < 2000
	Send, q ;Send native key
Else ; or
	Send, +w ;Send, {Shift}+different key
Return

SeeDRank_A
Posts: 5
Joined: 25 Sep 2022, 22:40

Re: Hold a key for "X" amount of time, then realease to activate script

Post by SeeDRank_A » 27 Sep 2022, 13:13

Rohwedder wrote:
27 Sep 2022, 02:33
With my German keyboard layout the tilde ~ is only selectable via AltGr. Since my driver for the English keyboard layout is unfortunately faulty regarding the key VK_OEM_3(~ `), (I get the characters from the German keyboard layout ^ °), I have to switch to other characters:

Code: Select all

$q:: ;1.) "q" is pressed DOWN
KeyWait, q ;2.)  you HOLD "q" for a given duration
;3.)  "q" is RELEASED
If A_TimeSinceThisHotkey < 2000
	Send, q ;Send native key
Else ; or
	Send, +w ;Send, {Shift}+different key
Return
so far i got

Code: Select all

$`::
KeyWait 1, ` ;2.)  you HOLD "q" for a given duration
;3.)  "q" is RELEASED
If A_TimeSinceThisHotkey < 2000
Send, ` ;Send native key
Else ; or
Send, ~ 
Sleep, 2000 
Send, Vape On{Enter} 
Return 
[Mod edit: [code][/code] tags added.]

Thank you for your response again Roh and good afternoon

I need to not repeat the command when i am holding down the key. i need to have all 3 conditions, DOWN,PRESS for X time, and UP to send one (1) command. " Vape On{Enter}

My second script would be DOWN, PRESS for 5 seconds, {script} to Power Off, or in this case Send, Vape Off{Enter}

Post Reply

Return to “Ask for Help (v1)”