Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hiahkforum
Posts: 47
Joined: 08 Feb 2024, 04:21

Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by hiahkforum » 03 Mar 2024, 06:45

Is this possible to use #MaxThreadsPerHotkey and KeyWait simultaneously? I want to run my script in multiple threads, but using KeyWait for double pressing function instead of having another hotkey.

Code: Select all

#MaxThreadsPerHotkey 6
$w::{
	KeyWait 'w'
	if KeyWait('w', 'D T0.1') {
		MsgBox '2'
	}
	else {
		MsgBox '1'
	}
}

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by mikeyww » 03 Mar 2024, 07:01

What happened when you tried it? What, specifically in an example, should happen instead?

hiahkforum
Posts: 47
Joined: 08 Feb 2024, 04:21

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by hiahkforum » 03 Mar 2024, 07:16

With #MaxThreadsPerHotkey 1 everything works as expected - it shows MsgBox 1 when pressed once and MsgBox 2 when pressed twice. When you increase the #MaxThreadsPerHotkey value to 2 or higher, it always shows MsgBox 1 for any number of clicks. There's logic to this, but I'm wondering if there's a way around it.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by mikeyww » 03 Mar 2024, 07:23

You answered question #1. How about question #2?

hiahkforum
Posts: 47
Joined: 08 Feb 2024, 04:21

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by hiahkforum » 03 Mar 2024, 08:48

At the moment I have 2 scripts with hotkeys W and Shift + W, which I run in several threads simultaneously. My goal is to make the controls more convenient so that Shift + W becomes W x2, but at the same time I want to maintain the ability to run 2 of these different codes at the same time.

Code: Select all

#MaxThreadsPerHotkey 6
w::{
	MsgBox '1'
}
+w::{
	MsgBox '2'
}
I only know the implementation via KeyWait, but since 2 of my codes become part of one hotkey, at first glance this does not really fit with #MaxThreadsPerHotkey 6.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by mikeyww » 03 Mar 2024, 10:03

I think you want a timer class. It's a bit beyond me, but the forum likely has posts that show it.
Last edited by mikeyww on 03 Mar 2024, 10:17, edited 1 time in total.

hiahkforum
Posts: 47
Joined: 08 Feb 2024, 04:21

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by hiahkforum » 03 Mar 2024, 10:16

This works:

Code: Select all

#MaxThreadsPerHotkey 2
w::{
	MsgBox '1'
}
+w::{
	MsgBox '2'
}
This works:

Code: Select all

#MaxThreadsPerHotkey 1
w::{
	KeyWait 'w'
	if KeyWait('w', 'D T0.1') {
		MsgBox '2'
	}
	else {
		MsgBox '1'
	}
}
This doesn't work and instead of MsgBox 2 on double press it shows 2 windows with MsgBox 1.

Code: Select all

#MaxThreadsPerHotkey 2
w::{
	KeyWait 'w'
	if KeyWait('w', 'D T0.1') {
		MsgBox '2'
	}
	else {
		MsgBox '1'
	}
}
I know there is no mistake in this behavior. But I'm trying to figure out whether it is possible to implement the launch of two or more MsgBox 1 on a single press and the launch of two or more MsgBox 2 on a double press at the same time. With KeyWait double-pressing works, but it only launches either MsgBox 1 or MsgBox 2 at the same time, but I need 2+ MsgBox 1 and 2+ MsgBox 2 to be launched at once. I don't know how to explain it even more clearly. KeyWait is all I know, so it's not a problem if it's implemented differently.

I have no problem with interruptions and waiting for the last thread to complete.
Last edited by hiahkforum on 03 Mar 2024, 10:28, edited 1 time in total.

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by mikeyww » 03 Mar 2024, 10:20

Since MsgBox must be resolved before the script proceeds, you could use GUI instead of MsgBox.

Code: Select all

#Requires AutoHotkey v2.0

w:: {
 Static presses := 0
 presses++
 SetTimer go, -300
 go() {
  Switch presses {
   Case 1: Send 1
   Case 2: makeGUI
  }
  presses := 0
 }
}

makeGUI() {
 g := Gui('+AlwaysOnTop')
 g.AddText 'w300', 'abc'
 g.Show 'NoActivate x' Random(1, 800) ' y' Random(1, 800)
}

hiahkforum
Posts: 47
Joined: 08 Feb 2024, 04:21

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by hiahkforum » 03 Mar 2024, 11:01

Thank you for trying to help. I used MsgBox in the example only for ease of understanding, in fact there is no MsgBoxes in my code, only one InputBox and many SendEvents. I don't know if your example will work for me, I'll try to understand the code tomorrow. Here's a more precise example of my current code:

Code: Select all

#MaxThreadsPerHotkey 6
w::{
	Title := WinGetTitle()
	OnError((*) => 1)
	SetTimer () => WinSetAlwaysOnTop(1, 'ahk_class #32770'), -50
	if 'OK' = (Input := InputBox('Input something:', Title, 'w330 h129')).Result {
		WinActivate Title
		WinWaitActive Title
		SetKeyDelay 20, 20
		SendEvent '123{Enter}'
		Sleep 100
		SendEvent Input
	}
}
+w::{
	Basically the same, but with different string stored in Input and different SendEvents. Want to double press W instead of Shift+W.
}

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Simultaneous using of #MaxThreadsPerHotkey and KeyWait for double pressing function.

Post by mikeyww » 03 Mar 2024, 11:08

Different needs may have different solutions, so describing a goal with MsgBox that is not actually your real goal might not be so useful. In any case, if you want multiple simultaneous input forms, GUI can be used for that as well.

Post Reply

Return to “Ask for Help (v2)”