Sleep Function [HELP]

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Sleep Function [HELP]

20 Jul 2017, 12:38

Good morning my friends...
I made a script that works as follows, when I press the "A" key it triggers the "A" key and waits for 10 seconds to send again if the key is kept pressed.
If I press the "B" key it triggers the "B" key and waits for no second to trigger the "B" key again.

Code: Select all

Loop
{
while getKeyState("A", "P")
send {A}{sleep 10000}

while getKeyState("B", "P")
send {B}
}

The problem is that if I hit the "A" key and then hit the "B" key I have to wait for the 10 seconds for it to work.
Is there any way I can interrupt the Sleep of 10 seconds of the "A" key the moment I press the "B" key, to prevent it being sent with the delay due to sleep?
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

20 Jul 2017, 12:55

The easiest way would be to split it into two different hotkeys. If you really need it to all be in one hotkey, try using SetTimer instead of sleep.
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

20 Jul 2017, 14:30

MaxAstro wrote:The easiest way would be to split it into two different hotkeys. If you really need it to all be in one hotkey, try using SetTimer instead of sleep.
Could you give me an example of how you would look with Settimer and another example of separate seals?
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

20 Jul 2017, 14:31

I'm not entirely sure what your script is supposed to do, which makes it hard to give examples. Does it just loop the A key? Why do you need to do anything with the B key at all?
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

20 Jul 2017, 15:00

MaxAstro wrote:I'm not entirely sure what your script is supposed to do, which makes it hard to give examples. Does it just loop the A key? Why do you need to do anything with the B key at all?
What I am trying to do is the following ...

By pressing the "A" key it sends the "A" key every 10 seconds

When you press the "B" key it sends the "B" key every 0 seconds (if the 10 seconds of the "A" key is running, it stops the sleep to send the "B"
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

20 Jul 2017, 15:12

I guess my question on the B key is, how is that different from just holding the "B" button on your keyboard down?
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

20 Jul 2017, 15:18

MaxAstro wrote:I guess my question on the B key is, how is that different from just holding the "B" button on your keyboard down?
Because the idea of the two keys is spam.

When holding "A" he must spam the "A" key every 10 seconds
When holding the "B" key it must spam the "B" key every 0 seconds

The problem is that at some point when I hold the "B" key it takes time to execute because it is waiting for the 10 second delay of the "A" key.

I'm trying to undo the 10 second sllep of the "A" key the moment I run the "B" key so it has no delay.
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

21 Jul 2017, 12:52

Try this:

Code: Select all

DoPressA := true
Loop
{
	if (getKeyState("A", "P") && DoPressA)
	{
		send {A}
		DoPressA := false
		SetTimer, ResetA, 10000
	}
	if getKeyState("B", "P")
		send {B}
}

ResetA:
	DoPressA := true
	return
lexikos
Posts: 9554
Joined: 30 Sep 2013, 04:07
Contact:

Re: Sleep Function [HELP]

21 Jul 2017, 18:26

FYI, send {sleep 10000} does not sleep for 10 seconds. It sends the "Sleep" key 10,000 times.

Also, send {A} does not send "the A key". It sends the capital letter "A", by sending Shift (if necessary) and the A key.

By contrast, send {a} or send a sends just the A key, but may automatically release Shift temporarily if the user is holding it down.

Note that both send A and send a (with or without braces) will automatically release Ctrl or Alt temporarily if they are being held down.

To just send the A key and leave all modifier keys as they are, send {blind}a.
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

24 Jul 2017, 23:43

MaxAstro wrote:Try this:

Code: Select all

DoPressA := true
Loop
{
	if (getKeyState("A", "P") && DoPressA)
	{
		send {A}
		DoPressA := false
		SetTimer, ResetA, 10000
	}
	if getKeyState("B", "P")
		send {B}
}

ResetA:
	DoPressA := true
	return
It worked perfectly my friend, thank you ...
I would now like to implement the following function:
Pressing the "A" key sends the "A" key every 10 seconds and the "B" key repeatedly (spamming).
That is, it would join the two codes to be executed by a single key ...
it's possible?


I tried the following command but it did not work ...

Code: Select all

Loop
{
	if getKeyState("A", "P")
	{
		send {B}
		If DoPressA
		send {A}
		DoPressA := false
		SetTimer, ResetA, 10000
	}
}

ResetA:
	DoPressA := true
	return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

25 Jul 2017, 09:36

You need to put brackets around send {A}...SetTimer so that they are part of the "if" statement - you only want to set the timer if DoPressA is true, after all. Like this:

Code: Select all

If DoPressA
{
	send {A}
	DoPressA := false
	SetTimer, ResetA, 10000
}
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

25 Jul 2017, 18:25

MaxAstro wrote:You need to put brackets around send {A}...SetTimer so that they are part of the "if" statement - you only want to set the timer if DoPressA is true, after all. Like this:

Code: Select all

If DoPressA
{
	send {A}
	DoPressA := false
	SetTimer, ResetA, 10000
}
I made the following change and the code was as follows.

It is not working as I expect, it sends the "A" key once and then the "B" key continuously, and the "A" key is no longer sent, it seems the timer is not working.

Should work the following were:
Pressing the "B" key continuously sends the "B" key and the "A" key every 10 seconds.

Code: Select all

DoPressA := true
Loop
{
	if getKeyState("B", "P")
	{
		send {B}
		DoPressA
		{
			send {A}
			DoPressA := false
			SetTimer, ResetA, 10000
		}
	}
}

ResetA:
	DoPressA := true
	return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

26 Jul 2017, 11:33

It looks like you are missing the word "if" before the brackets - i.e., it should read if DoPressA and then the brackets.
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

26 Jul 2017, 12:57

MaxAstro wrote:It looks like you are missing the word "if" before the brackets - i.e., it should read if DoPressA and then the brackets.
I made the change and now it is working perfectly.
Thanks again my friend...


I made changes to the code, but when I use it that way, it only spwes the "a", "b", and "c" keys.
The "d" key is not sent at any time, not even from 10s to 10s.
(I changed the activation key to "z" for testing)

Code: Select all

DoPressA := true

Loop
{
	if getKeyState("z", "P")
	{
		send {a}{b}{c}
		if DoPressA
		{
			send {d}
			DoPressA := false
			SetTimer, ResetA, 10000
		}
	}
}

ResetA:
	DoPressA := true
	return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

26 Jul 2017, 14:28

Hm... Is there a reason you are using brackets in the send commands? Try changing it to just Send abc and Send d and see if that makes a difference.
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

26 Jul 2017, 20:52

MaxAstro wrote:Hm... Is there a reason you are using brackets in the send commands? Try changing it to just Send abc and Send d and see if that makes a difference.
I understand...

I made the following change, but it still does not send the "d"

Code: Select all

DoPressA := true

Loop
{
	if getKeyState("z", "P")
	{
		send abc
		if DoPressA
		{
			send d
			DoPressA := false
			SetTimer, ResetA, 10000
		}
	}
}

ResetA:
	DoPressA := true
	return
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

27 Jul 2017, 09:12

That's odd, when I run the same code it definitely does send the d. Try this: Add the line MsgBox Test 1 right after send d, and MsgBox Test 2 near the bottom right after DoPressA := true. Then run the code and tell me: does a messagebox that says "Test 1" pop up when you hit z? Does another one that says "Test 2" pop up ten seconds later?
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

27 Jul 2017, 10:36

MaxAstro wrote:That's odd, when I run the same code it definitely does send the d. Try this: Add the line MsgBox Test 1 right after send d, and MsgBox Test 2 near the bottom right after DoPressA := true. Then run the code and tell me: does a messagebox that says "Test 1" pop up when you hit z? Does another one that says "Test 2" pop up ten seconds later?
I made the following code change ...

Code: Select all

DoPressA := true

Loop
{
	if getKeyState("z", "P")
	{
		send abc
		if DoPressA
		{
			send {space}
			DoPressA := false
			SetTimer, ResetA, 1000
		}
	}
}

ResetA:
	DoPressA := true
	return
I opened the notepad and pressed the "z" key for about 5 seconds.
The result was this ...

Code: Select all

zabc abcabcabcabcabcabcabcabcabcazbczabzcabzcazbczabzcazbczabczabzcazbczabzcazbczabzcazbczabzcabzcazbczabzcazbczabzcabzcazbczabzcazbczabc
In the first routine he sent the "space" key, but then he did not send it any more.
What I would like is for the "z" key not to be sent and the "space" key to be sent every 1 second.
It should look like this ...

Code: Select all

abcabcabc abcabcabc abcabcabc abcabcabc abcabcabc abcabcabc
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Sleep Function [HELP]

28 Jul 2017, 10:06

Getting it to not send the "z" key is going to be very hard with the way you have this set up, as a loop instead of a hotkey. Can it be a hotkey instead? What about this:

Code: Select all

DoPressA := true

z::
	send abc
	if DoPressA
	{
		send {space}
		DoPressA := false
		SetTimer, ResetA, 1000
	}
	return

ResetA:
	DoPressA := true
	return
raphaelmca
Posts: 25
Joined: 19 Feb 2016, 08:32

Re: Sleep Function [HELP]

28 Jul 2017, 14:50

MaxAstro wrote:Getting it to not send the "z" key is going to be very hard with the way you have this set up, as a loop instead of a hotkey. Can it be a hotkey instead? What about this:

Code: Select all

DoPressA := true

z::
	send abc
	if DoPressA
	{
		send {space}
		DoPressA := false
		SetTimer, ResetA, 1000
	}
	return

ResetA:
	DoPressA := true
	return
I tried to use this code that posted but the result was this ...

Code: Select all

abc abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc
As in the first code, it did the first routine correctly, but in the sequence did not send the {space}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jaka1 and 140 guests