Trying to hit random keys in order. Topic is solved
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Trying to hit random keys in order.
Greetings again,
Some time ago I tried to make a macro for a game that consisted of hitting arrows that appeared on the screen randomly inside a box following the order, but I didn't succeed.
Any idea how to do it? For example the first time can be: up, up, down, right, left, down and the second time can be down, up, right, left, up, down
Is there any way to create a script that checks a certain section of the screen (for example, check an image) and press a key depending on the image?
Some time ago I tried to make a macro for a game that consisted of hitting arrows that appeared on the screen randomly inside a box following the order, but I didn't succeed.
Any idea how to do it? For example the first time can be: up, up, down, right, left, down and the second time can be down, up, right, left, up, down
Is there any way to create a script that checks a certain section of the screen (for example, check an image) and press a key depending on the image?
Last edited by Anonplayer on 12 Apr 2021, 18:09, edited 1 time in total.
Re: Trying to hit random keys in order.
Yes, you can use ImageSearch to search for a match to an image file. Random can generate random numbers for you.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
The problem is that the order of the arrows changes continuously, I need the keys to be pressed in the order they appear (from left to right)
How could I do that?
How could I do that?
Re: Trying to hit random keys in order.
You have four images, one for each direction. Search for each image. When you find a match, you press the corresponding key, and move to the next set of coordinates for the next search. To get the process working, start with a single arrow.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
So it would be something like this:
And after that I should move to the next coordinate and repeat the same code right?
I'm sorry if i'm so noob in this.
Code: Select all
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\up.png
if (ErrorLevel = 0)
{
Send, {up}
}
if (ErrorLevel = 1)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\down.png
Send, {down}
}
if (ErrorLevel = 2)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\left.png
Send, {left}
}
if (ErrorLevel = 3)
{
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\My Images\right.png
Send, {right}
}
I'm sorry if i'm so noob in this.
Re: Trying to hit random keys in order.
That is the idea. Possibly like this:
Code: Select all
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 0, 0, 1440, 900, C:\My Images\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
Thank you!
So I should write that code once for each arrow to press according to its order, right? In total it would be a sequence of 5
So I should write that code once for each arrow to press according to its order, right? In total it would be a sequence of 5
Re: Trying to hit random keys in order.
You could do that, or make a Loop, with adjustments of coordinates. As usual, there is more than one way.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
It sounds complicated about the loop, I have been testing and there is no way. Could you give me an example if it's not too much trouble? And again, thank you.
The idea is also that it repeats continuously until I press a certain key.
The idea is also that it repeats continuously until I press a certain key.
Re: Trying to hit random keys in order.
This is just a demo. Adjust as needed.
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
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
I'm trying but it doesn't seem to work, I must be doing something wrong.
My screen has a resolution of 1440x900.
In X1 and Y1 I am putting the coordinates of the upper left corner of where the arrow should be and in X2 and Y2 I am putting the coordinates of the lower right corner of where the arrow should be.
I press ctrl h but for some reason it doesn't detect the image on the screen.
The code has been like this:
In the game it looks like this.
My screen has a resolution of 1440x900.
In X1 and Y1 I am putting the coordinates of the upper left corner of where the arrow should be and in X2 and Y2 I am putting the coordinates of the lower right corner of where the arrow should be.
I press ctrl h but for some reason it doesn't detect the image on the screen.
The code has been like this:
Code: Select all
^h::
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,,, 354, 354, 384, 375, C:\Users\Anon\Pictures\focus\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
}
Re: Trying to hit random keys in order.
Don't break the lines.
Since your script does not work, break it down into smaller parts. Start with a single ImageSearch-- just one line. Get it working. You can then add the multi-image search. After that works, add the While loop.
Since your script does not work, break it down into smaller parts. Start with a single ImageSearch-- just one line. Get it working. You can then add the multi-image search. After that works, add the While loop.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
Okay I did it, seems it's working now with this code. Another problem is that the box where the arrows are located changes position after completing each sequence, así que no termina de funcionar bien.
Now I need the script to be able to press the keys in the order they appear on the screen. However, once a sequence of arrows is completed, a new one appears whose order is different from the previous one.
Code: Select all
^h::
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 337, 328, 377, 361, C:\Users\Anon\Downloads\Arrows\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 373, 325, 410, 367, C:\Users\Anon\Downloads\Arrows\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 405, 327, 441, 368, C:\Users\Anon\Downloads\Arrows\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 439, 328, 471, 368, C:\Users\Anon\Downloads\Arrows\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
For index, direction in ["Up", "Down", "Left", "Right"] {
ImageSearch,,, 370, 332, 507, 367, C:\Users\Anon\Downloads\Arrows\%direction%.png
If !ErrorLevel {
Send {%direction%}
Break
}
}
Re: Trying to hit random keys in order.
It looks like you have the basic routine working. You can now adjust according to wherever you need to look for the arrows. If you describe the complete process in words, translating it into AHK will be easier for you. If you cannot describe it, then you cannot translate it! Best of luck.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
Is there a way that the script can search the images from left to right? That would save me a lot of time and everything would be easier and I would not have to go selecting coordinate by coordinate.
Re: Trying to hit random keys in order.
You can search the entire window if you like. When a match is found, you can adjust the coordinates as needed.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
The search only give me the x1 and y1, how can I do to get the x2 and y2?
The code that i'm using is
The code that i'm using is
Code: Select all
^h::
CoordMode Pixel ; Interprets the coordinates below as relative to the screen rather than the active window.
ImageSearch, FoundX, FoundY, 0, 0, 1440, 900, C:\Users\Anon\Downloads\Arrows\up.png
if (ErrorLevel = 2)
MsgBox Could not conduct the search.
else if (ErrorLevel = 1)
MsgBox could not be found on the screen.
else
MsgBox found at %FoundX%x%FoundY% %FoundX2%x%FoundY2%.
Re: Trying to hit random keys in order.
You are searching for an image that you already have, so your image has a known width and height. You can compute the other coordinates from it.
-
- Posts: 26
- Joined: 28 Jan 2021, 18:49
Re: Trying to hit random keys in order.
I really appreciate your help, but I think I'm going to continue with problems.
Suppose I manage to get the x1, y1, x2, y2 coordinates of all the arrows in one of the sequences. Well, I turn on the script and everything perfect, it manages to complete the sequence.
After that first sequence, the arrows change their position in the window, but suppose I also have their coordinates, as well as the other multiple random locations that it moves to after each sequence of arrows.
How do I make the script automatically search and detect those sequences in the window? I'm gonna show you an example:
First seq. Second seq. As you can see, in the second sequence the arrows no longer have the same order and the box that contains them has moved slightly in position. Basically I need an script that hit the arrows from left to right following the order and knowing that the coordinates vary after each sequence.
Suppose I manage to get the x1, y1, x2, y2 coordinates of all the arrows in one of the sequences. Well, I turn on the script and everything perfect, it manages to complete the sequence.
After that first sequence, the arrows change their position in the window, but suppose I also have their coordinates, as well as the other multiple random locations that it moves to after each sequence of arrows.
How do I make the script automatically search and detect those sequences in the window? I'm gonna show you an example:
First seq. Second seq. As you can see, in the second sequence the arrows no longer have the same order and the box that contains them has moved slightly in position. Basically I need an script that hit the arrows from left to right following the order and knowing that the coordinates vary after each sequence.
Re: Trying to hit random keys in order.
1. Search the entire window for the first image. The leftmost image will then be found.
2. At that point, you search to the right for each of the next four images-- each time, searching to the right of the previous image.
3. Go back to step #1 for each set.
With each search, you are searching for any of the four possible images.
2. At that point, you search to the right for each of the next four images-- each time, searching to the right of the previous image.
3. Go back to step #1 for each set.
With each search, you are searching for any of the four possible images.