Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Crushthebug
Posts: 18
Joined: 07 Mar 2017, 04:30

Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 21:09

So let's say the trigger for my thread is:

Code: Select all

1 & 9::
I only want my thread to trigger when these 2 are pressed, but sometimes I'm unable to press them both at the same time when I'd like them to trigger.

So my question is, is it possible to make so that if they are pressed within a certain timeframe of each other (say half a second), it will trigger the thread anyway?
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 21:39

You shouldn't be trying to press them down at the same time. You should be pressing and holding the 1 key first and then pressing the 9 key second, like how you hold shift down and then press keys that you want to capitalize.
Crushthebug
Posts: 18
Joined: 07 Mar 2017, 04:30

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 22:27

Osprey wrote:You shouldn't be trying to press them down at the same time. You should be pressing and holding the 1 key first and then pressing the 9 key second, like how you hold shift down and then press keys that you want to capitalize.
I understand but doing it either way doesn't do anything to solve my issue. The issue is that sometimes, while I'm pressing a lot of other keys, I'll accidentally release 1 before I press 9 and miss out on triggering my thread. Basically, I want to trigger my thread even if I make a slight mistake (as long as the mistake is slight). So I'd like to trigger my thread as long as I press 1 and 9 within .5-1 second of each other, for example.
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 22:53

You can use KeyWait with the option T0.5
Please excuse my spelling I am dyslexic.
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 23:10

Crushthebug wrote:I understand but doing it either way doesn't do anything to solve my issue. The issue is that sometimes, while I'm pressing a lot of other keys, I'll accidentally release 1 before I press 9 and miss out on triggering my thread. Basically, I want to trigger my thread even if I make a slight mistake (as long as the mistake is slight). So I'd like to trigger my thread as long as I press 1 and 9 within .5-1 second of each other, for example.
In that case, it sounds like you need hotkeys for 1 and 9 separately, like this:

Code: Select all

~1::
  If (A_PriorHotkey = "~9") and (A_TimeSincePriorHotkey < 1000)
    Goto, MySubroutine
Return

~9::
  If (A_PriorHotkey = "~1") and (A_TimeSincePriorHotkey < 1000)
    Goto, MySubroutine
Return

MySubroutine:
; Code that you originally triggered with "1 & 9"
Return
Crushthebug
Posts: 18
Joined: 07 Mar 2017, 04:30

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

28 Jan 2018, 23:53

Osprey wrote:
Crushthebug wrote:I understand but doing it either way doesn't do anything to solve my issue. The issue is that sometimes, while I'm pressing a lot of other keys, I'll accidentally release 1 before I press 9 and miss out on triggering my thread. Basically, I want to trigger my thread even if I make a slight mistake (as long as the mistake is slight). So I'd like to trigger my thread as long as I press 1 and 9 within .5-1 second of each other, for example.
In that case, it sounds like you need hotkeys for 1 and 9 separately, like this:

Code: Select all

~1::
  If (A_PriorHotkey = "~9") and (A_TimeSincePriorHotkey < 1000)
    Goto, MySubroutine
Return

~9::
  If (A_PriorHotkey = "~1") and (A_TimeSincePriorHotkey < 1000)
    Goto, MySubroutine
Return

MySubroutine:
; Code that you originally triggered with "1 & 9"
Return
That's actually genius and works perfectly, thanks a ton. I have one other problem not exactly related to this but related to the same script that I'm setting up. Would you mind giving it a look?

Here is my current code:

Code: Select all

#MaxThreadsPerHotkey 2

~1::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine
Return

~9::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine2
Return

Space::
  If (A_PriorHotkey = "~1") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine
  If (A_PriorHotkey = "~9") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine2
Return

MySubroutine:
	toggle := !toggle
	Loop
	{
	; a bunch of "Send events", "Sleeps" and "if (!toggle) breaks"
	Toggle := false
	}
	until !toggle || (a_index = 1)
Return	
So I start MySubroutine with 1/9 & Space (which is what I want) and then if I decide that I want to break my loop, I have to press the same combination (1 or 9 with Space), when I would prefer to simply press Space to break it instead. Is there a way that I can specify inside of MySubroutine or inside of the "Space:: hotkey" that I want Space alone to break the loop but still have 1/9 & Space to start the loop?

Does that make sense? Sorry if it doesn't I'm kinda bad with explanations!

Thanks again for the Sub Routine idea it's great :)
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

29 Jan 2018, 00:23

Try this:

Code: Select all

#MaxThreadsPerHotkey 2

~1::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine
Return

~9::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine
Return

Space::
  If (A_PriorHotkey = "~1") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine
  Else If (A_PriorHotkey = "~9") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine
  Else toggle := false
Return

MySubroutine:
  toggle := true
  Loop
  {
    ; a bunch of "Send events", "Sleeps" and "if (!toggle) breaks"
  } Until !toggle
Return	
Crushthebug
Posts: 18
Joined: 07 Mar 2017, 04:30

Re: Trigger thread as long as "key1" & "key2" were pressed within "x" time of each other?

29 Jan 2018, 01:14

Osprey wrote:Try this:

Code: Select all

#MaxThreadsPerHotkey 2

~1::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine
Return

~9::
  If (A_PriorHotkey = "Space") and (A_TimeSincePriorHotkey < 20)
    Goto, MySubroutine
Return

Space::
  If (A_PriorHotkey = "~1") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine
  Else If (A_PriorHotkey = "~9") and (A_TimeSincePriorHotkey < 800)
    Goto, MySubroutine
  Else toggle := false
Return

MySubroutine:
  toggle := true
  Loop
  {
    ; a bunch of "Send events", "Sleeps" and "if (!toggle) breaks"
  } Until !toggle
Return	

Well that was simple lol... thanks a ton for the help everything works exactly as I'd like now :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: apeironn, madensuyu1, peter_ahk and 332 guests