HELP: Hold a button for 0,5s to trigger action Topic is solved

Ask gaming related questions (AHK v1.1 and older)
WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 06:30

When I hold RButton+LButton for 0,5s then it will Send, {RAlt 2}, and if I don't hold long enough nothing happens.Sometimes I hold RButton and tap fast LButton and at that point I don't want it to trigger (maybe even 0,3s will work better)

Code: Select all

~RButton & ~LButton::
	Send, {RAlt 2}
	While GetKeyState("RButton") && GetKeyState("LButton")
	Continue
Return

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 10:32

Here is an example that you can test in Notepad, and then make changes to match your needs.

Code: Select all

a::a
a & b::
KeyWait, a, T.3
Send % ErrorLevel & GetKeyState("b", "P") ? "long" : "short"
KeyWait, a
KeyWait, b
Return

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 11:48

mikeyww wrote:
17 Jan 2022, 10:32
Here is an example that you can test in Notepad, and then make changes to match your needs.

Code: Select all

a::a
a & b::
KeyWait, a, T.3
Send % ErrorLevel & GetKeyState("b", "P") ? "long" : "short"
KeyWait, a
KeyWait, b
Return
Hmm, ok, let me know if I am going the right way? PS I really don't know what I am doing XD
I have made this code after 100 times of trying one or another until I found a working one.

Code: Select all

RButton::a
LButton::b
~a & ~b::
KeyWait, a, T.3
Send % ErrorLevel & GetKeyState("b", "P") ? "long" : "short"
KeyWait, a
KeyWait, b
Return
But where should I put

Code: Select all

Send, {RAlt 2}
While GetKeyState("RButton") && GetKeyState("LButton")

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 12:07

Below is an idea to test. Check KeyHistory as needed.

Code: Select all

RButton::RButton
RButton & LButton::
KeyWait, LButton, T.4
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return
keyHistory.png
Key history
keyHistory.png (12.09 KiB) Viewed 1628 times

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 13:05

mikeyww wrote:
17 Jan 2022, 12:07
Below is an idea to test. Check KeyHistory as needed.

Code: Select all

RButton::RButton
RButton & LButton::
KeyWait, LButton, T.4
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return

keyHistory.png
There is no delay. It works the same as the old one. I tried to set higher T but still no delay :(
Could it be because I am testing in game?

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 13:14

Test it in Notepad before testing in your game. In Notepad, try the "a, b" script that I posted first. Does it work there? Next, try the newer script.

One issue with my own mouse is that it cannot always detect a button release if two buttons are held at the same time.

https://autohotkey.com/board/topic/111737-how-to-make-ahk-work-in-most-games-the-basics/

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 13:18

@mikeyww
I am very sorry, it works! But now I am back to other problem. My RButton is not working (aim down sight in game is not working - while script works as I wanted, pings enemy after the KeyWait).

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 13:22

Try with the tilde.

Code: Select all

RButton::RButton
~RButton & LButton::
KeyWait, LButton, T.4
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 13:48

OK this seems to work, but sometimes it triggers one more time after I don't hold any buttons to trigger it.

Code: Select all

~RButton & ~LButton::
KeyWait, LButton, T.2
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return
Can you tell me why this work and is this correct?

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 13:53

I would first try the last script that I posted, to see if that one works.

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 13:56

mikeyww wrote:
17 Jan 2022, 13:22
Try with the tilde.

Code: Select all

RButton::RButton
~RButton & LButton::
KeyWait, LButton, T.4
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return
Trigger works but now LButton don't work while holding RButton. We are getting really close XD

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action  Topic is solved

Post by WekizZ » 17 Jan 2022, 14:11

This one does work. But don't know how fluent it is.

Code: Select all

RButton::RButton
~RButton & ~LButton::
KeyWait, LButton, T.4
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 17 Jan 2022, 14:40

@mikeyww
thanks for the help, I think I can work with this now, how can I commend you?

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 17 Jan 2022, 16:10

No problem. I am glad that you have it working. :)

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 24 Jan 2022, 04:50

Hey @mikeyww can you help me a little more with my code :idea: :angel: :roll: ?

I want to change some things and add more, but I also want to know if this code is well made. It works but, now I know how to make it work even better 8-)

Code: Select all

#If WinActive("ahk_exe your_game.exe")	;; your game exe here so the script works only in game

M:: ;; key to stop the script on hold
    Suspend, On
    Pause, On
Return

M UP:: ;; key to continue the script
    Suspend, Off
    Pause, Off
Return

RButton::RButton ;; auto ping enemy while holding Rmouse+Lmouse (cooldown for 2-3s after ping)
~RButton & ~LButton::
KeyWait, LButton, T.1 ;; change to a cooldown of 2-3s after {RAlt 2} was clicked
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : ""
Return

;; a place for one more script slide/cancel.
;; while hold C slide, let go will cancel slide

#If

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 24 Jan 2022, 07:16

It looks good to me, especially if it works! Enjoy it.

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 24 Jan 2022, 07:31

@mikeyww

I want to fix this part:

Code: Select all

RButton::RButton 
~RButton & ~LButton::
KeyWait, LButton, T.1 ;; no time delay
Send % ErrorLevel & GetKeyState("RButton", "P") ? "{RAlt 2}" : "" ;; this line should go on to 2-3 seconds cooldown after trigger
Return
Right now I need to hold ~RButton & ~LButton:: for T.1 time to trigger {RAlt 2}.
What I want is, trigger {RAlt 2} instantly with ~RButton & ~LButton::, but right after, this {RAlt 2} was executed it should go on 2-3s cooldown (should not be triggable - sorry for my English :roll: ).

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 24 Jan 2022, 07:59

An example is below. You can change the keys to what you need.

Code: Select all

F3::F3
~F3 & ~F4:: ;; auto ping enemy while holding Rmouse+Lmouse (cooldown for 2-3s after ping)
Hotkey, % hk := A_ThisHotkey, Off
Send {RAlt 2}
SetTimer, Resume, -2000
SoundBeep, 1000
Return
Resume:
Hotkey, %hk%, On
SoundBeep, 1500
Return

WekizZ
Posts: 73
Joined: 14 Jul 2017, 04:40

Re: HELP: Hold a button for 0,5s to trigger action

Post by WekizZ » 24 Jan 2022, 09:25

mikeyww wrote:
24 Jan 2022, 07:59
An example is below. You can change the keys to what you need.
This looks interesting (even dough, I don't understand more than half of it)! Let me try it soon and I will let you know.

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

Re: HELP: Hold a button for 0,5s to trigger action

Post by mikeyww » 24 Jan 2022, 09:49

It is fairly simple. When you activate the hotkey, the routine disables the hotkey for two seconds.

Post Reply

Return to “Gaming Help (v1)”