Check my syntax please

Ask gaming related questions (AHK v1.1 and older)
Nowaydude
Posts: 2
Joined: 15 Apr 2018, 17:39

Check my syntax please

17 Apr 2018, 11:56

I'm trying to make a code that, while F1 is being held down, it sends a left Ctrl left mouse click every 100ms. Is this the proper syntax?


Code: Select all

~$F1::
    While GetKeyState("F1", "P"){
        Send, Lctrl Click
        Sleep, 100
    }
return
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Check my syntax please

17 Apr 2018, 12:43

Will it work as it stands in the script? Yes.
Is it the best way to do it? No. The technique will work for one key at a time ONLY.
If you tried to add an F2 hotkey that did the same thing, then held F1, then held F2, then releasing F1 would NOT stop the F1 loop.
The "right" way to do it is with timers.

Also, if your intention is to send a Ctrl+Click, then Send LCtrl Click will NOT do that. It will press and release CTRL then press and release left mouse. You probably want Send ^{LButton}

Code: Select all

 F1_State := 0
 return
 
~$F1::
	if (F1_State)
		return	; Keys in AHK repeat if held, so if the key is already held, do not start the timer again
	F1_State := 1
	SetTimer, DoClick, 100
	return

~$F1 up::
	F1_State := 0
	SetTimer, DoClick, Off
	return

DoClick:
	Send, Lctrl Click	; This is NOT sending a ctrl click. It is sending Ctrl press/release, then click press/release
	return
Nowaydude
Posts: 2
Joined: 15 Apr 2018, 17:39

Re: Check my syntax please

17 Apr 2018, 14:23

Awesome! Thank you so much for the great feedback and explanation. You are correct; I was wanting Lctrl to be held down while the left click happens. Timers looks like what i was trying to do. I will try this when i get home.
User avatar
evilC
Posts: 4823
Joined: 27 Feb 2014, 12:30

Re: Check my syntax please

17 Apr 2018, 14:55

With the repeat suppression, it is a little bit of a cumbersome way to do such a simple thing, but I don't know of any shorter solution using KeyWait / GetKeyState that does not have some side-effect when you try to do it multiple times.
You don't actually need the F1_State := 0 at the beginning, as unset variables are considered "false" - I just included it for clarity.
If all you want is one key in a script using that technique (or only one will ever be used at one time), your original technique is fine.

If you get tired of re-implementing that pattern over and over for lots of keys, it can be done as a function by using the hotkey command.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: doanmvu and 39 guests