Help with a very simple code problem I have

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Help with a very simple code problem I have

10 Jan 2023, 22:55

I want my program to restart only a certain code, for example this is my code:

Code: Select all

1::
{
;this is my first code
return

2::
{
;This is my second code
}
return


f::
{
sleep, 4000
sendinput, {Lshift down}
sleep, 10
sendinput, {Lshift up}
sleep, 200
sendinput, {d down}
sleep, 100
sendinput, {d up}
return
}

Suppose I press "f", but 2 seconds later I press "f" again. The program must start again if "f" is pressed within 4000 ms. How would I code something like this? Currently, the code executes to completion when "f" is press for the first time, so any second or third "f" presses will not restart and start the f:: code.
DustinLuck
Posts: 7
Joined: 28 Jul 2021, 12:26

Re: Help with a very simple code problem I have

10 Jan 2023, 23:13

Try the following code. After the F key is released, it will wait for four seconds to see if the F key is pressed again. If it is, the first iteration will be canceled.

Code: Select all

    f Up::
    {
    KeyWait, f, D T4
    if ErrorLevel = 0
    {
        return
    }
    sendinput, {Lshift down}
    sleep, 10
    sendinput, {Lshift up}
    sleep, 200
    sendinput, {d down}
    sleep, 100
    sendinput, {d up}
    return
    }
User avatar
mikeyww
Posts: 27274
Joined: 09 Sep 2014, 18:38

Re: Help with a very simple code problem I have

10 Jan 2023, 23:15

Code: Select all

#Requires AutoHotkey v1.1.33

f::
SetTimer Go, -4000
SoundBeep 1500
Return

Go:
Send abc
Return

Code: Select all

#Requires AutoHotkey v2.0

f:: {
 SetTimer go, -4000
 SoundBeep 1500
}

go() {
 Send 'abc'
}
mexican scientist
Posts: 33
Joined: 02 Jul 2020, 21:55

Re: Help with a very simple code problem I have

11 Jan 2023, 16:30

DustinLuck wrote:
10 Jan 2023, 23:13
Try the following code. After the F key is released, it will wait for four seconds to see if the F key is pressed again. If it is, the first iteration will be canceled.

Code: Select all

    f Up::
    {
    KeyWait, f, D T4
    if ErrorLevel = 0
    {
        return
    }
    sendinput, {Lshift down}
    sleep, 10
    sendinput, {Lshift up}
    sleep, 200
    sendinput, {d down}
    sleep, 100
    sendinput, {d up}
    return
    }
Thanks, this works exactly like I want, but looking at the documentation for the "KeyWait" function, it only words for a single key press. Suppose I want to modify the script so that while it is waiting for 4000 ms, it checks if "Lshift" is press and if "Lshift" is press, then it exits "f UP::"

So basically we have the following, if "f" is pressed, the code waits 4000 ms for another "f" press and it resets, but now I want to modify the code so that if "Lshift" is press within those 4000 ms, then the code "f UP" ends and its ready for another "f" press whenever I want.

I made this code, to include an "LShift" check while it waits 4000 seconds and it works!

Code: Select all

~f Up::
	{
	start_time := A_TickCount
	time_to_run := 4000
	end_time := start_time + time_to_run
	while (A_tickcount <= end_time)
		{
		if (GetKeyState("f"))
			return
		else
			{
			if (GetKeyState("LShift"))
				Exit
			}
		}


    	sendinput, {Lshift down}
    	sleep, 10
    	sendinput, {Lshift up}
    	sleep, 200
    	sendinput, {d down}
    	sleep, 100
    	sendinput, {d up}
    	return
    	}
Return


Is there an easier way to do what I want or is this is? The code works like I want, but looking at your original script it uses the "KeyWait" function and I wanted to know it it is possible to do your way because it looks cleaner!
DustinLuck
Posts: 7
Joined: 28 Jul 2021, 12:26

Re: Help with a very simple code problem I have

11 Jan 2023, 19:03

This is how I'd modify my code to allow LShift to cancel the action for the F key press.

Code: Select all

CancelF := false
~f Up::
{
    CancelF := false
    KeyWait, f, D T4
    if (ErrorLevel = 0 OR CancelF)
    {
        return
    }
    sendinput, {Lshift down}
    sleep, 10
    sendinput, {Lshift up}
    sleep, 200
    sendinput, {d down}
    sleep, 100
    sendinput, {d up}
    return
}

~Lshift::
{
    CancelF := true
}
I have added a variable (CancelF) that is initially set to false when the script starts and also at each press of the F key. When the LShift key is pressed, CancelF is set to true. Within the code for the F hotkey, the value of CancelF is checked along with the KeyWait ErrorLevel and will return if either condition is met.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 231 guests