Random key presses held down

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gallyhad
Posts: 4
Joined: 19 Oct 2021, 15:09

Random key presses held down

Post by Gallyhad » 19 Oct 2021, 15:28

what I'm trying to do is hold down a key a random amount of time, with random intervals before.

it's kinda cobbled together from looking at other bits, and the main part works for random presses, but while it parses fine it doesn't do hold down far as I can tell. certainly doesn't seem to work when tested. the outside was added to make it more useable, but then it seemed to fall apart. The button needs to be held down to let the camera swing as opposed to rapid fire presses. I am still learning. just can't figure out what I'm doing wrong here know it's prolly something stupid

Code: Select all

\::


Loop, 50

Random, Delay, 1000, 8000
Random, TRd5, 20, 600

cam: 
Send, {
Random, var, 1,5
if (var = 1){
   Send, {Left down}
   Sleep, %TRd5%
   Send, {Left up}
   
}
if (var = ){
   Send, {Right down}
   Sleep, %TRd5%
   Send, {Right up}
  
}
if (var = 3){
   Send, {Up down}
   Sleep, %TRd5%
   Send, {Up up}
   
}
if (var = 4){
   Send, {Down down}
   Sleep, %TRd5%
   Send, {Down up}
   
}
if (var = 5){
   Send, {Left down}
   Sleep, %TRd5%
   Send, {Left up}
   
}
  }

SetTimer, Cam, %Delay%
Return


esc::exitapp ; <- Press escape to exit.

return

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

Re: Random key presses held down

Post by mikeyww » 19 Oct 2021, 16:35

Why is there a loop and also a timer? If loop applies to the whole thing, then use a block enclosed in braces.

Test your script in Notepad to see if it works there.

If there is a problem holding a key, then create a short test script (3 lines) that just holds a key, so that you can demonstrate that you can make that action work first.

Gallyhad
Posts: 4
Joined: 19 Oct 2021, 15:09

Re: Random key presses held down

Post by Gallyhad » 20 Oct 2021, 06:44

the loop was added sow hen testng in notepad it just ran a set length and stopped, planned to take that out before actually using

should be 2 timers, one small one for time keys pressed down and one for the time between key presses. the numbers I made smaller just for testing purposes.

individually, bits work. the keypress doesn't seem to hold down, not sure why

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

Re: Random key presses held down

Post by mikeyww » 20 Oct 2021, 07:26

Thank you for the clarifications. I maintain my recommendations.

Gallyhad
Posts: 4
Joined: 19 Oct 2021, 15:09

Re: Random key presses held down

Post by Gallyhad » 20 Oct 2021, 09:25

after fiddling last night, this is what I've finally come up with. though it's outputting as an example in notepad "Q1Q1Q1Z2Z2Z2Q1Q1Q1Z2Q1Z2Q1Z2Z2Q" when it should only be sending out Q and Z's.

Letters used as stand in so I can watch it working in notepad

Code: Select all

\::


; Loop, 30

Random, Delay, 100, 3000
Random, var1, 500, 3000

SetKeyDelay, %var1%, 500, 


   SetTimer, PressTheKey, %Delay%
   Return

   PressTheKey:
   Send, %key%
 
{
Random, key, 1, 2

{
if (key = 1){
   Send, {Q}
 }
 
if (key = 2){
   Send, {Z}
 }

}
   
}
  
   Return

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

Re: Random key presses held down

Post by mikeyww » 20 Oct 2021, 09:48

key itself is a number, so when you Send %key% (line 16), you are sending the number.

Gallyhad
Posts: 4
Joined: 19 Oct 2021, 15:09

Re: Random key presses held down

Post by Gallyhad » 20 Oct 2021, 10:33

mikeyww wrote:
20 Oct 2021, 09:48
key itself is a number, so when you Send %key% (line 16), you are sending the number.
so what should I use there instead?

mybrains kinda stuck in a look in what should be there

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

Re: Random key presses held down

Post by mikeyww » 20 Oct 2021, 11:28

If you do not need line 16, then delete it.

Code: Select all

\::
Random, frequency, 100, 3000
Random, afterKeyUp, 500, 3000
SetTimer, PressTheKey, %frequency%
PressTheKey:
SetKeyDelay, afterKeyUp, 500
Random, key,, 1        ; 0 or 1
Send % key ? "q" : "z" ; 0 -> z, 1 -> q
Return

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 04 Jun 2023, 15:14

its nice script but which key turn it of?

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

Re: Random key presses held down

Post by mikeyww » 04 Jun 2023, 16:04

None. If needed, you can add a hotkey to disable the timer.

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 04 Jun 2023, 16:37

how?

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

Re: Random key presses held down

Post by mikeyww » 04 Jun 2023, 17:49

Timer syntax:

SetTimer [, Label, PeriodOnOffDelete, Priority]

You can omit the priority.

The Label here is PressTheKey.

The PeriodOnOffDelete parameter would be Off.

Explained: SetTimerIntroduction and simple examples

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 05 Jun 2023, 06:35

can you add that to this script? i dont know anything about that, even instruction dont tell me anything

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Random key presses held down

Post by boiler » 05 Jun 2023, 06:56

mikeyww showed you the syntax of the line and exactly what to put in for each parameter. And you apparently know how to add a hotkey. So what else is there?

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 05 Jun 2023, 07:05

now its not working

,::
Random, frequency, 100, 50
Random, afterKeyDown, 100, 50
SetTimer, PressTheKey, Off
PressTheKey:
SetKeyDelay, afterKeyDown, 100
Random, key,, 1 ; 0 or 1
Send % key ? "i" : "o" ; 0 -> i, 1 -> o
Return

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Random key presses held down

Post by boiler » 05 Jun 2023, 07:12

You were told you could add a hotkey to turn off the timer and were shown the syntax to do so, and you implemented it by replacing the line where it turns it on in your existing hotkey? How would it ever get tuned on now?

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 05 Jun 2023, 07:14

i dont know thats why im asking for help

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Random key presses held down

Post by boiler » 05 Jun 2023, 07:20

I think you should read the above posts and figure it out. Its like if you asked "How do you turn on the TV?" and the answer is "Aim the remote at the TV and press the button labeled "Power" in the upper-left corner, and your response is "Can you show me?"

kyubi85
Posts: 13
Joined: 23 Nov 2022, 06:42

Re: Random key presses held down

Post by kyubi85 » 05 Jun 2023, 07:23

not helpful :(

User avatar
boiler
Posts: 16767
Joined: 21 Dec 2014, 02:44

Re: Random key presses held down

Post by boiler » 05 Jun 2023, 07:29

Actually, it’s more helpful to you if it makes you take some time to read the posts and links provided, think about what they’re telling you, and figuring out what to do. The fact that the direction given is actually so simple and straightforward if you actually read it and followed it is what makes it a necessary exercise for you to go through if you are going to be able to learn to do anything else in the future.

Post Reply

Return to “Ask for Help (v1)”