I'm new to the AHK and trying to make a script to click on a coordinate in a specific window, once per minute.
I already used the WindowSpy to get the coordinates and the WindowTitle, but i can't get to work
Right now the script is:
ControlClick, x469 y363, ahk_pid 11532
Am i missing something?? Thanks!
edit1: I changed the script to this and tested in another window like Excel and it worked, but in the game that it is supposed to work, it doesnt, no idea why
ControlClick, x466 y364, ahk_pid 11532,,,, Pos
sleep 60000
}
Not sure if you just pasted in that code badly, or it really looks like that, but a simple loop can be used to achieve that. (Possibly not the most ideal thing to use, depends on if you want the script to do something else as well)
Loop
{
ControlClick, x466 y364, ahk_pid 11532,,,, Pos NA
Sleep, 60000
}
Also I recommend at least trying out the NA option, it's documented as possibly improving reliability.
And about it not working in some game you're trying, it's very much expected. More often than not games will receive input in some way which makes control clicking not work. For example receiving direct input.
There's not much you can do about that, and it can get really complicated/advanced.
Again, I don't know what what sort of game you're talking about, but going into windowed mode (assuming fullscreen even is a thing for the game) and actually moving the cursor and then clicking might work. But that of course defeats the purpose of even using ControlClick in the first place..
The answer from #0x464e is good if you're only running one process within the script. If want to run multiple processes use the SetTimer function.
SetTimer, ClickOneMin, 60000
return
ClickOneMin:
ControlClick, x466 y364, ahk_pid 11532,,,, Pos NA
return
This will allow you to add additional loops, timers and processes.
Related
I wanted to have a window closed by Autohotkey as soon as I click somewhere else and was confident that there would be a fitting trigger / listener like e.g. an OnMouseClick - function. But seems not; MouseClick just actively clicks the mouse. So the best way I could figure was this:
SetTitleMatchMode, 2
Loop{
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose , AutoTasks 2.ahk
}
Sleep, 1000
}
return
While the above code does work, it leaves me unsatisfied that that's really the best AHK can do (?), because those few lines of code, while not a real problem, do cost CPU and memory (according to my Task Manager e.g. more than Skype or my Cloud Background service). The 1-second-sleep also introduces a discernible lag, but without it, the loop would even cost 2% CPU!
Can you come up with a more efficient / AHK-native solution (except for further increasing the Sleep-time)?
I've found the answer myself in the meantime:
SetTitleMatchMode, 3 ; exact match
WinWaitNotActive, My_Window_Name_to_close.ahk
WinClose, My_Window_Name_to_close.ahk
Even though you found a solution to your problem, I wanted to provide an answer to the actual question of detecting a click. That can be done using a hotkey for "LButton" in combination with the tilde "~" modifier like so:
~LButton:: ; The "~" sets it so the key's native function will not be blocked
GoSub CheckActiveWindow
Return
CheckActiveWindow:
sleep, 1 ; Wait a small amount of time in case the desired Window is changing from inactive to Active
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose , AutoTasks 2.ahk
}
return
when I‘m making a script, I can‘t make two functions happen.
All details first: I‘m using Auto Hot Key‘s current version.
code:
<+<!`::Sound beep 627,92
Sleep, 100
Sound beep 837,92
return
only Sound beep at pitch 627 is happening, not the other beep.
I try other‘s suggestions by copy pasting them, they work, but my handwritten script doesn‘t work.
did I do any mistake? thanks.
To have more than one command executed by a hotkey, put the first line beneath the hotkey definition and make the last line a return:
<+<!`::
SoundBeep 627,92
Sleep, 100
SoundBeep 837,92
return
For more details, read the Hotkey page.
In my AutoHotKey script I'm using #IfWinActive to detect if the Roblox window is in focus, and then press the number 1 button whenever left clicking the mouse, like this:
#IfWinActive, Roblox
LButton::
MouseClick, Left
SendInput, {1}
return
#IfWinActive
It works great, except for when I'm clicking out of the Roblox window back to another window. It still fires this code on the first click, resulting in it typing the number 1 into Notepad (or whatever window I switch focus to).
I figured that when I'm clicking on Notepad the focus is still on the Roblox window, which is why the code still fires. So I tried changing the code to this:
#IfWinActive, Roblox
LButton::
Sleep, 100
if WinActive("Roblox")
{
MouseClick, Left
SendInput, {1}
}
return
#IfWinActive
Assuming that by the time the Sleep finished the focus would have shifted to the Notepad window and If WinActive("Roblox") would return false, but it still returns true and types 1 into Notepad.
I also tried using StartTimer and a label, thinking that maybe the Sleep wasn't asynchronous, but that has the same problem as well.
Anybody know how to get around this issue? Thanks in advance!
The main problem in this case is that the hotkey is fired immediately after LButton is pressed down and the Roblox window is still active.
The only solution I see is to fire the hotkey upon release of the LButton using the tilde prefix (~) to prevent AHK from blocking the key-down/up events:
#IfWinActive, Roblox
~LButton Up:: SendInput, 1
#IfWinActive
There are a couple of ways we can achieve this. TL;DR for solution, check the yellow part of this post.
Firstly I'll address the problems in your code:
Usage of MouseClick over Click. Technically nothing wrong, but Click is said to be more reliable in some situations and easier to use. Looks cleaner as well.
Wrapping 1 in {} is not needed and does nothing for you here. In some cases you may even produce unwanted behavior by doing this. In a send command, {} is used to escape keys that have special meaning, or to define keys that you can't just type in. More about this from the documentation.
Having a somewhat of a bad WinTitle that you're matching against. Again, nothing technically wrong, but right now you match any window that starts with the word Roblox. Shouldn't be too hard accidentally match the wrong window.
A quick and a very effective solution would be matching against the process name of your Roblox window.
So #IfWinActive, ahk_exe Roblox.exe or in an if-statement if (WinActive("ahk_exe Roblox.exe")) (assuming that's the process' name, I have no idea)
For an absolutely fool proof way could match against the hwnd of the Roblox window. However, that's maybe a bit overkill and you couldn't really use it with #IfWinActive either. An example I'll write below will use this that though.
However, problems 1 and 2 can be entirely avoided by doing this neat way of remapping a key (remapping is pretty much what you're doing here).
~LButton::1
Ok, so why does that work?
key::key is just the syntax to easily do a basic remap, and with ~ we specify that the hotkey isn't consumed when it fires.
Cool, but now onto the actual problem you're having.
So what went wrong with the sleeping thing? Well since you're consuming the hotkey, all you're actually doing is firing the hotkey, waiting 100ms, then checking if Roblox is active. Well yes, it will still be active since nothing was ever done to switch focus away from it.
If you were to not consume the left clicking action, it would work, but it's definitely not a good idea. You do not want to sleep inside a hotkey statement. AHK does not have true multithreading and unless you would've specified a higher #MaxThreadsPerHotkey for your hotkey, all subsequent presses of the hotkey would be totally ignored for that 100ms.
So yes, with specifying a higher amount of threads that can run for that hotkey, it would kind of make this solution work, but it's still bad practice. We can come up with something better.
With timers you can avoid sleeping in the hotkey statement. Sounds like you tried the timers already, but I can't be sure it went right since code wasn't provided so I'll go over it:
#IfWinActive, ahk_exe Roblox.exe
~LButton::SetTimer, OurTimersCallbackLabel, -100 ;-100 specifies that it runs ONCE after 100ms has passed
#IfWinActive
OurTimersCallbackLabel:
if (WinActive("ahk_exe Roblox.exe"))
SendInput, 1
return
And now onto the real solution, to which #user3419297 seems to have beat me to, just as I'm writing this line of text.
Using the up event of your LButton press as the hotkey.
#IfWinActive, ahk_exe Roblox.exe
~LButton Up::SendInput, 1
#IfWinActive
This way the down event has already switched focus of the window and our hotkey wont even fire.
Note that here we unfortunately can't use the key::key way of remapping I described above.
Bonus:
Here's something that could be used if the up event of our keypress wouldn't be desirable, or somehow the window switching of the active window was delayed.
RobloxHwnd := WinExist("ahk_exe Roblox.exe")
#If, RobloxUnderMouse()
~LButton::1
#If
RobloxUnderMouse()
{
global RobloxHwnd ;specify that we're using the variable defined outside of this function scope
;could've also ran the code to get Roblox's hwnd here every time, but that would be wasteful
MouseGetPos, , , HwndUnderMouse ;we don't need the first two parameters
return RobloxHwnd == HwndUnderMouse ;are they the same hwnd? (return true or false)
}
Here we're first storing the hwnd of our Roblox to the variable RobloxHwnd.
Note that Roblox would need to be running before we run this script, and if you restart robox, script would need to be restarted as well.
So adding some way of updating the value of this variable on the fly would be good, maybe under some hotkey.
Then by using #If we're evaluating an expression (in our case, running a function and evaluating its return value) every time we're about to attempt to fire the hotkey. If the expression evaluates to true, we fire the hotkey.
Usage of #If is actually not recommended, and it is good practice to avoid using if at all possible. However, you wont encounter any problems in a script this small, so using #If is going to be very convenient here.
If you were to have a bigger script in which there's a lot of code running often, you'd be likely to run into problems.
Even though I have read the docs and watched tutorials I am a complete beginner so I apologize If its a silly question. I have tried for quite a while to do this with no results.
I want to have NUMPAD0 key to press the global recording button in Ableton Live but I do not want to do it through mouse coordinates (1. Because the windows might be minimized when I need to record and 2. Because I want to implement this in the future in order to be able to press the recording button also on individual tracks as I create them, which might range from 1 to 80 new tracks for example)
The thing is that the Windows class dynamically changes and the Control ID is not shown in winspy.
This is what I have been doing:
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:00000000007A04C3
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:0000000000200651
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:0000000000290651
GroupAdd, Live, ahk_class
Afx:0000000140000000:0:0000000000000000:0000000000900015:00000000001E05F1
curMode := 1
return
Numpad0::
SetControlDelay -1
controlclick, ahk_group Live0,,,,,NA
return
So, my issues are these: 1. In the GroupAdd command I have put the window classes that I have seen yet I believe there might be more classes, that appear everytime I open a new Ableton window. 2. In the Control Click command, I havent put the control ID because I couldnt find it.
Any advice on how to finish this code or change it to a different it in order to press that Record button in Ableton, without using coordinates?
EDIT:
This is the code Im trying now, following suggestions but I havent got any results yet:
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive, .*als
Numpad0::F9
#IfWinActive
return
And this is the 2 version of that, which is not working either
SetTitleMatchMode, Regex
SetTitleMatchMode, Slow
#IfWinActive, Afx:0000000140000000
Numpad0::F9
#IfWinActive
return
While I've never used GroupAdd, if there's a finite number of Class permutations, it seems to me you could RegExMatch against those and then form your group inside that. The GroupAdd spec seems to make all parameters optional beyond the GroupName, so perhaps you could skip the Class declaration after confirming you've got the right window.
Example:
WinGetTitle, Title, A
WinGetClass, Class, A
if (RegExMatch(Title, "Ableton Live.*") AND (RegExMatch(Class, "(AbletonClass1|AbletonClass2|AbletonClass3)")))
{
; Do stuff
}
I have a loop that works like this:
Loop {
; Do something here.
If (WinExist("ThisWindow")) {
; Do stuff here.
}
Sleep, 10
}
However, it seems that as soon as I added WinExist() in the loop, it starts doing some sizable I/O as can be seen in Process Explorer. I've already tried separating the WinExist() check to a SetTimer instead of inside the Loop but it still does it. Does anyone have any idea of alternatives I can do use? I wanted to use WinWait instead but surprisingly. that also uses up I/O. And I can't have the loop get interrupted or paused.
Is this something I cannot work around? This is a script constantly running in the background so I want to optimize it as much as I can.
Update
It seems to be related to me setting SetTitleMatchMode, RegEx. So it happens when RegEx is enabled