Disable a key after long press

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Unknovvn
Posts: 5
Joined: 18 Sep 2017, 09:47

Disable a key after long press

15 Jan 2018, 19:36

Hi there,

I want to disable a key after I press certain times but I cant make it work. I am not sure if I have logical error.

Code: Select all

$6::
	Test("6", "o")
return

Code: Select all

Global f
Test(key1, key2) {
	f := 0

	While GetKeyState(key1, "p")
	{
		Send, {%key2%}
		ToolTip % f, 1200, 600, 20
		f := f + 1
		delay := 10

		if (f >= 20) {
			Hotkey, %key1%, Label, On

			if (GetKeyState(original, "p") = 0)
				Hotkey, %key1%, Label, Off
		}
	}
}

Code: Select all

Label:
return
What I want is blocking key1 after we long press for 20 times incase the flooding of keys until we release it.

I tried to swap the position of On and Off. From the documentation, I think "Off" should be disabling the key
https://www.autohotkey.com/docs/commands/Hotkey.htm

However, this example is using "On" to turn off the key.
https://autohotkey.com/board/topic/9557 ... -keyboard/

I am not quite sure so I tried either way.

Thanks!
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Disable a key after long press

16 Jan 2018, 00:11

You would need a label, in this case, Label: that does only the return command. And yes, you want the hotkey on. An on hotkey will "hijack" the native function, and in this case, AHK will make it do nothing.

Code: Select all

key1:="f" ; you should be able to use your function, but this is standalone code
Hotkey, %key1%, Label, On
return

Label:
return
As to the logical error.. First off, delay is doing nothing. You keep defining the delay variable, but never using it.

However, it won't stop a currently running a hotkey, even if it prevents the hotkey form firing. That is to say, you still need to physically release the key to stop it from doing the While loop.

I don't think your logic is sound on this project.

You want to accomplish that while you hold "6", you send "o" ≤20 times. If the limit is reached, the hotkey should no longer do anything.

Code: Select all

$6::
While GetKeyState("6","p")
{
Send o
Sleep 10 ; or whatever delay you want
If A_Index=20 ; on the 20th iteration of the loop
   Break ; jumps out of the While loop
}
KeyWait, 6 ; waits for 6 to be released before completing the hotkey
return
This should be translatable to a function if you wish.

You may also be interested in using SubStr() and A_ThisHotkey to get the key name from the Hotkey itself instead of hardcoding each instance. But then you'd also want to explore arrays to pair up 6 with o and 7 with p and 8 with q or whatever you need.
Unknovvn
Posts: 5
Joined: 18 Sep 2017, 09:47

Re: Disable a key after long press

16 Jan 2018, 08:14

Exaskryz wrote: This should be translatable to a function if you wish.
This is exactly what I need, thank you very much! :)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5, mikeyww, RandomBoy, wpulford and 373 guests