Trying to hit random keys in order. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

11 Apr 2021, 22:45

Hmmmmmm.

The problem is that they are random, it is not always the same the first one, how would you write that in the code? Because for example if I use this code

Code: Select all

^h::
CoordMode, Pixel, Window
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\Users\Anon\Downloads\Arrows\down.png
if (ErrorLevel = 2)
    MsgBox Image not found.
else if (ErrorLevel = 1)
    MsgBox Image not found.
else
    Send {down}
Return

ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\Users\Anon\Downloads\Arrows\down.png
if (ErrorLevel = 2)
    MsgBox Image not found.
else if (ErrorLevel = 1)
    MsgBox Image not found.
else
    Send {up}
Return
The code will first look for the "down" arrow and even if it is not the first in the sequence it will find it and when it finds it the key will be pressed, which will cancel the sequence having to start over.

There is what I am looking for, I want them to be pressed in order, because without the order the sequence is canceled.

Edit. I have tried this code as well, but it seems that it does the search in this order: up, down, left, right (and it is not what I am looking for, I need it to recognize the images in order from left to right)

Code: Select all

^h::
CoordMode, Pixel, Window  ; Interprets the coordinates below as relative to the screen rather than the active window.
For index, direction in ["Up", "Down", "Left", "Right"] {
 ImageSearch,,, 0, 0, 1440, 900, C:\Users\Anon\Downloads\Arrows\%direction%.png
 If !ErrorLevel {
  Send {%direction%}
  Break
 }
}
The simplest solution I see is to ask yourself, how would you go about pressing a series of keys in order (from left to right) when you don't know what their order is going to be? If you could provide me with a code for this, I would appreciate it, because at this rate my head will explode :cry:
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

12 Apr 2021, 05:13

Sure. Here it is.

https://www.autohotkey.com/boards/viewtopic.php?p=393309#p393309

What the script does:
1. Search for Up.png. If found, Send {Up}, and finish. Otherwise:
2. Search for Down.png. If found, Send {Down}, and finish. Otherwise:
3. Search for Left.png. If found, Send {Left}, and finish. Otherwise:
4. Search for Right.png. If found, Send {Right}, and finish.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 12:45

The problem is, if the Sequence is: down, up, left, left, up if the script looks for Up first and Up is the second arrow, the Sequence will be cancelled.
User avatar
boiler
Posts: 16923
Joined: 21 Dec 2014, 02:44

Re: Trying to hit random keys in order.

12 Apr 2021, 13:20

You're right that you need to search by each smaller area in sequence, not the whole area. The version should work where you had separate loops for each area moving left to right, each one finding which arrow is in that slot. I guess you were asking if you could do that in a loop instead of four separate loops. That's what this post of mikeyww's was doing with its nested loops (the while loop and the for loop). if it didn't work, there must just be some debugging to be done, but that's the approach.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 13:55

Thank you!

Is there a way to automatically find out the coordinates of the lower right corner? I know that% FoundX% x% FoundY% finds the coordinates of the upper left corner.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

12 Apr 2021, 14:30

The coordinates would be the sum of the upper left corner and the width or height of your reference image, minus one.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 14:42

Code: Select all

width := 30, height := 40, x := 100, y1 := 100, y2 := y1 + height - 1
While !done {
 Sleep, 500
 x1 := x + (A_Index - 1) * width, x2 := x + A_Index * width - 1
 For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, x1, y1, x2, y2, C:\My Images\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Break
  }
 }
}
ExitApp
F3::done := True
Before you provided me with this code, now I just need to fill it in and try to see how it works. Let's see, the size of the arrows is 24x24. Suppose the first arrow is located at 331, 321, 368, 380 of the active window

How do I put that in the code?

By the way, that's how the arrows work. I am attaching a video so that you can better figure out what I am trying to do.
Attachments
arrows.gif
arrows.gif (125.96 KiB) Viewed 855 times
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

12 Apr 2021, 15:15

For the first line:

Code: Select all

width := 24, height := 24, x := 331, y1 := 321, y2 := y1 + height - 1
With your situation, an alternative approach could be that after you find the first arrow, instead of moving right by the width, you move right by a smaller increment, in case the images shift to the left between the searches. You might also need to use a smaller y-value if the images are shifting upward.

Whenever you can describe the actual search position in words, then translating it into AHK is more straightforward!
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 15:51

Finally It's working! Yay!!!!

This is the code

Code: Select all

^h::
For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 320, 323, 381, 390, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }
For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 361, 323, 418, 390, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }

For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 392, 323, 451, 380, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }

For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 422, 323, 488, 380, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }

For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, 455, 323, 515, 380, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }
The problem is that it is very slow, I have to keep pressing CTRL + H. Any way to make it work faster and without having to be pressing all the time?

Also I need add some delay between te keys, wit a single Sleep will be enoug?
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

12 Apr 2021, 16:11

I think you will have to test it and use some trial and error to see what adjustments need to be made, regarding the sequences of searches as well as any additional sleep commands.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 16:23

It's running pretty smooth now. All I need is a loop so that it repeats continuously without having to press any keys.

Would the while be enough? Basically I need it to do:
- Press the 5 arrows.
- Wait a few seconds after pressing all of them.
- It repeats automatically.

If you can give me the code to repeat it, I would also appreciate it! ^^
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.  Topic is solved

12 Apr 2021, 16:36

Code: Select all

#MaxThreadsPerHotkey 2

^h::
on := !on
While on {
 search(320, 323, 381, 390)
 If on
  search(361, 323, 418, 390)
 If on
  search(392, 323, 451, 380)
 If on
  search(422, 323, 488, 380)
 If on
  search(455, 323, 515, 380)
 Sleep, 3000 * on
}
SoundBeep, 1000
Return

search(x1, y1, x2, y2) {
 SoundBeep, 1500
 For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, x1, y1, x2, y2, C:\Users\Anon\Downloads\Arrows\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }
}
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 16:46

Finally everything works correctly. Thank you very much Mickey.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

12 Apr 2021, 22:08

Sometimes due to server lags, the sequence cuts out. I need something that in case of not detecting an arrow on the screen, it allows me to press a key to reactivate the sequence in the game and then to activate the script again.

Basically when the arrow sequence is canceled, an image that says INCORRECT appears. I need when that image appears, I need to press CTRL + J and then ENTER and then the keypress script to resume again.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

13 Apr 2021, 05:29

Ctrl+h is a toggle, so you can just press it twice to start.
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

13 Apr 2021, 09:30

I know but I want something automated and I dont know if its possible. When the INCORRECTA appeals has to press CTRL+J and then ENTER
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

13 Apr 2021, 09:51

Possibly like this:

Code: Select all

search(x1, y1, x2, y2) {
 Global on
 Static dir := StrReplace(A_Desktop, "Desktop", "Downloads\Arrows")
 SoundBeep, 1500
 For index, direction in ["Up", "Down", "Left", "Right"] {
  ImageSearch,,, x1, y1, x2, y2, %dir%\%direction%.png
  If !ErrorLevel {
   Send {%direction%}
   Return
  }
 }
 ImageSearch,,, x1, y1, x2, y2, %dir%\incorrect.png
 If ErrorLevel
  Return
 Send ^j{Enter}
 on := False
}
Anonplayer
Posts: 26
Joined: 28 Jan 2021, 18:49

Re: Trying to hit random keys in order.

13 Apr 2021, 11:24

That works but only repeats the process 1 time. Has to be indefinetly or til I decide to turn off the script.
User avatar
mikeyww
Posts: 26882
Joined: 09 Sep 2014, 18:38

Re: Trying to hit random keys in order.

13 Apr 2021, 11:47

It runs every time the search function is called. You could move the new routine inside the For loop, or even set a separate timer for it if you like.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 89 guests