[HELP] Stop a hotkey with another hotkey and execute it

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
thanhdatnguyen
Posts: 5
Joined: 03 Dec 2023, 07:52

[HELP] Stop a hotkey with another hotkey and execute it

Post by thanhdatnguyen » 03 Dec 2023, 09:11

I have a piece of code as follows and I want to pause one hotkey with another hotkey
Code:

Code: Select all

Numpad1::
{
SetKeyDelay, 1000, 100
	ControlSend,,{a down}, *222 - Notepad
	ControlSend,,{j down}, *222 - Notepad
	ControlSend,,{a up}, *222 - Notepad
	ControlSend,,{j up}, *222 - Notepad
	ControlSend,,{l down}, *222 - Notepad
	ControlSend,,{l up}, *222 - Notepad
	ControlSend,,{4 down}, *222 - Notepad
	ControlSend,,{4 up}, *222 - Notepad
return
}

Numpad0::
{
SetKeyDelay, 1000, 100
	ControlSend,,{p down}, *222 - Notepad
	ControlSend,,{p up}, *222 - Notepad
	ControlSend,,{u down}, *222 - Notepad
	ControlSend,,{u up}, *222 - Notepad
return
}
I want when I press hotkey "Numpad1" then hotkey "Numpad0" will stop immediately and execute hotkey "Numpad1"
If I press "Numpad0" and "Numpad1" in order, the result will be "ajl4pu"
However, I want when I press "Numpad0" to get the result "ajl", I immediately press "Numpad1" to get the result "ajlpu".
Thanks for your help

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

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by mikeyww » 03 Dec 2023, 10:03

Welcome to this AutoHotkey forum!

Although you cannot directly abort an entire thread, you can use variables to track what you wish. You can then access those variables to act accordingly.

thanhdatnguyen
Posts: 5
Joined: 03 Dec 2023, 07:52

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by thanhdatnguyen » 03 Dec 2023, 10:13

mikeyww wrote:
03 Dec 2023, 10:03
Welcome to this AutoHotkey forum!

Although you cannot directly abort an entire thread, you can use variables to track what you wish. You can then access those variables to act accordingly.
Can you suggest me a code? I want when I press "Numpad0" to get the result "ajl", I immediately press "Numpad1" to get the result "ajlpu".

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

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by mikeyww » 03 Dec 2023, 10:24

Your goal is unclear. Your script does not align with your description.

You may wish to use Google Translate if English is difficult. I suggest that you provide a clear description of each possible scenario: what you type, and what the script should then do.

thanhdatnguyen
Posts: 5
Joined: 03 Dec 2023, 07:52

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by thanhdatnguyen » 04 Dec 2023, 00:39

mikeyww wrote:
03 Dec 2023, 10:24
Your goal is unclear. Your script does not align with your description.

You may wish to use Google Translate if English is difficult. I suggest that you provide a clear description of each possible scenario: what you type, and what the script should then do.
If I press "Numpad1" the result will be: "ajl4".
Then I press "Numpad0" and the final result will be: "ajl4pu"
Now I want when I press Numpad1 until it only displays the first 3 characters "ajl" and right after that I press Numpad0 to execute the 2 characters "pu". And the final result will be "ajlpu" and not "ajl4pu". The main difference is in the last character of hotkey Numpad1
I know my English level is quite poor. I'm sorry. But my question is very clear, is there a command for another hotkey to take over the hotkey that is performing the action and completely stop that hotkey?
(For my example, my Numpad 1 will take 5 seconds to perform its action. Right after the 4th second I will press Numpad0 to completely stop Numpad1 and execute Numpad0)

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

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by mikeyww » 04 Dec 2023, 09:34

Yes, your new hotkey will interrupt the older one. When it completes, the interrupted subroutine will resume. Nonetheless, if you track which key is pressed, you can add conditional statements accordingly.

Code: Select all

#Requires AutoHotkey v1.1.33
SetKeyDelay 200

a::
SendEvent 1
If (A_ThisHotkey = "a")
 SendEvent 2
If (A_ThisHotkey = "a")
 SendEvent 3
If (A_ThisHotkey = "a")
 SendEvent 4
Return

b::
SendEvent 5
If (A_ThisHotkey = "b")
 SendEvent 6
If (A_ThisHotkey = "b")
 SendEvent 7
If (A_ThisHotkey = "b")
 SendEvent 8
Return

thanhdatnguyen
Posts: 5
Joined: 03 Dec 2023, 07:52

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by thanhdatnguyen » 06 Dec 2023, 06:14

Sincerely thank you for your help. I did it.
But I have one more question: is there a way that one hot key can execute two different commands? Is there any way to extract whether the "Caplock" state is on or not to execute different commands?

Code: Select all

Numpad7::			; 1, 2, 2 - Left
{
SetKeyDelay, 50, 100)
	{
		Sleep, 50
		ControlSend,,{s up}, VKM.exe
		ControlSend,,{space up}, VKM.exe
		If (A_ThisHotkey = "Numpad7")
		ControlSend,,{1 down}, VKM.exe
		ControlSend,,{1 up}, VKM.exe
		ControlSend,,{2 down}, VKM.exe
		ControlSend,,{2 up}, VKM.exe
		Sleep, 300
		ControlSend,,{2 down}, VKM.exe
		ControlSend,,{2 up}, VKM.exe
return
}

Numpad8::			; 2, 1, 2 - Left
{
SetKeyDelay, 50, 100)
	{
		Sleep, 50
		ControlSend,,{s up}, VKM.exe
		ControlSend,,{space up}, VKM.exe
		If (A_ThisHotkey = "Numpad8")
		ControlSend,,{2 down}, VKM.exe
		ControlSend,,{2 up}, VKM.exe
		ControlSend,,{1 down}, VKM.exe
		ControlSend,,{1 up}, VKM.exe
		Sleep, 300
		ControlSend,,{2 down}, VKM.exe
		ControlSend,,{2 up}, VKM.exe
return
Like my above code, "Numpad7 = 122" and "Numpad8 = 212". In the program I use, there are not only these 2 codes but about 6-7 hotkeys. But there was a period of time when the key orders changed symmetrically like "122-212" as above. It only changes the order of the keys. Is there a way to set up a hotkey that can automatically re-map the default keys on the keyboard so I can use 2 commands at the same time with just a single hotkey?

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

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by mikeyww » 06 Dec 2023, 07:25

Yes, but you'll first need to fix the syntax errors in your script. Attend to parentheses and braces. Bounding braces in your script are extraneous and should be removed. To check a key's state, you can use :arrow: GetKeyState(). Your script can then act accordingly.

thanhdatnguyen
Posts: 5
Joined: 03 Dec 2023, 07:52

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by thanhdatnguyen » 06 Dec 2023, 08:42

Code: Select all

Numpad4::			; F2, 4, 3, DF3
{
SetKeyDelay, 50, 100
space := GetKeyState("space", "P")
s := GetKeyState("s", "P")
s := GetKeyState("s", "P")
CapsLock := GetKeyState("CapsLock", "T")
	if (space = 1) & (s = 1) & (CapsLock = 0)
	{
		Sleep, 50
		ControlSend,,{s up}, Mortal Kombat 11
		ControlSend,,{space up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 1) & (s = 0) & (CapsLock = 0)
	{
		Sleep, 50
		ControlSend,,{space up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 0) & (s = 1) & (CapsLock = 0)
	{
		Sleep, 50
		ControlSend,,{s up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 0) & (s = 0) & (CapsLock = 0)
	{
		Sleep, 50
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 1) & (s = 1) & (CapsLock = 1)
	{
		Sleep, 50
		ControlSend,,{s up}, Mortal Kombat 11
		ControlSend,,{space up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 1) & (s = 0) & (CapsLock = 1)
	{
		Sleep, 50
		ControlSend,,{space up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 0) & (s = 1) & (CapsLock = 1)
	{
		Sleep, 50
		ControlSend,,{s up}, Mortal Kombat 11
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
	if (space = 0) & (s = 0) & (CapsLock = 1)
	{
		Sleep, 50
		If (A_ThisHotkey = "Numpad4")
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{i down}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{i up}, Mortal Kombat 11
		ControlSend,,{l down}, Mortal Kombat 11
		ControlSend,,{l up}, Mortal Kombat 11
		Sleep, 500
		ControlSend,,{d down}, Mortal Kombat 11
		ControlSend,,{a down}, Mortal Kombat 11
		ControlSend,,{k down}, Mortal Kombat 11
		ControlSend,,{d up}, Mortal Kombat 11
		ControlSend,,{a up}, Mortal Kombat 11
		ControlSend,,{k up}, Mortal Kombat 11
	}
return
}
I use this code and use Capslock state to toggle. When Capslock is off it works effectively. when Capslock is enabled, it still executes the command correctly when the Capslock state is off but immediately after that the Capslock signal light on my physical keyboard flashes 3 times and turns off. (i.e. my Casplock state has gone off) I honestly don't understand why it's like that ??? Can I use remap key in this case?

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

Re: [HELP] Stop a hotkey with another hotkey and execute it

Post by mikeyww » 06 Dec 2023, 11:09

I would shorten your script to just two or three lines so that you can test your idea more simply, and get your script working. AHK does not necessarily accommodate pressing four keys at the same time (or even three; noted in the documentation). Some keyboards also will not accommodate that in terms of the hardware.

Expanding a working script may be easier and faster than working with a long broken script. You can quickly add lines and retest iteratively. You will then come to know the exact cause of any problem.

Code: Select all

#Requires AutoHotkey v1.1.33

Numpad4::
If GetKeyState("CapsLock", "T")
     MsgBox 1
Else MsgBox 2
Return

Post Reply

Return to “Ask for Help (v1)”