I am trying to figure out how to have an AutoHotKey script save different coordinates through MouseGetPos on different hotkeys at the same time, without one overwriting the other.
I want to make a loop performing some sequence of events. Say at one point I want it to execute the first coordinate that I saved before or during the loop (but before the action), and then I want the script to execute the other coordinate at a later point in the loop.
I want the coordinates to be able to be preset (instead of pressing Hotkey1 before the action of hotkey1 and consequently hotkey2 after hotkey1 to ensure one is not overwritten).
An example of this would be:
If one hotkey saves one coordinate:
hotkey1::
MouseGetPos, posx, posy,
// If inserted into a loop, it would be:
MouseMove, %posx%, %posy%,
One hotkey saves another coordinate:
hotkey2::
MouseGetPos, posx1, posy1,
// In loop:
MouseMove, %posx1%, %posy1%,
If inputted into a random loop
#SingleInstance Force
SendMode Input
#MaxThreadsPerHotkey, 2
#NoEnv
#Persistent
SetWorkingDir %A_ScriptDir%
SetTitleMatchMode, 3
SetMouseDelay, -1
Process, Priority, , H
SetKeyDelay, -1, -1
SetDefaultMouseSpeed, 0
SetKeyDelay, 0, 10, Play
SetMouseDelay, 10, Play
CoordMode, Mouse, Screen
hotkey1::
MouseGetPos, posx, posy,
return
hotkey2::
MouseGetPos, posx1, posy1,
return
Toggle=0
f::
Toggle := !Toggle
Loop
{
If Toggle
{
Send {a}
sleep 100
MouseMove, %posx%, %posy%, // Say I want it to move to bottom left corner
Send {LButton}
sleep 100
If !Toggle
Break
MouseMove, %posx1%, %posy1%, // Say this coordinate would be top-right corner
Send {LButton}
sleep 100
Send {c}
}
}
return
q::ExitApp
If I trigger hotkey1 and hotkey2 in that order, hotkey1 would get saved then overwritten by hotkey2, making hotkey1 the same coordinate as hotkey2. I would want 2 separately saved coordinates.
Do I do that by having one script trigger another script or can this be done in a single script?
Related
I would like to drag window using right click with title bar just like left click. Is it possible do that with AutoHotkey?
Background : I use Dell Display Manager which lets me arrange my windows in pre-defined grid. I can do this directly dragging or Shift+ Drag. Both options are sub optimal. Direct dragging just forces unwanted resize. Shift and Drag requires a key and mouse. I am wondering if I can drag using right click. I use application called RBTray to minimize to tray using right click. So, I know we can definitely add something like. I am looking something in AutoHotkey as that's much easier to code than C++.
This is probably what you're looking for: https://www.autohotkey.com/docs/scripts/index.htm#EasyWindowDrag
Below is code adapted to right click:
~RButton::
CoordMode, Mouse ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0 ; Only if the window isn't maximized
SetTimer, EWD_WatchMouse, 0 ; Track the mouse as the user drags it.
return
EWD_WatchMouse:
GetKeyState, EWD_LButtonState, RButton, P
if EWD_LButtonState = U ; Button has been released, so drag is complete.
{
SetTimer, EWD_WatchMouse, Off
return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D ; Escape has been pressed, so drag is cancelled.
{
SetTimer, EWD_WatchMouse, Off
WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1 ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return
I Have a loop I am trying to get to work properly, so space is held throughout the entire looping period, and it doesnt have to be pressed within the loop to be activated.
I tried moving the space function outside of the loop, and release it after, but then it doesnt recognize the action at all.
toggle = 0
#MaxThreadsPerHotkey 20
!Z::
Toggle := !Toggle
While Toggle{
Loop, X
{
Send {Space down}
MouseMove, %X1%, %Y1%, 3,
MouseClick, L
Sleep Y
MouseMove, %X2%, %Y2%, 3
Sleep Y
MouseClick, L
Send {Space up}}
If !Toggle
Break
}
return
The result I want to happen is that for as long as that loop is running, spacebar is pressed down, from before the loop starts, and is released after the loop ends.
I'm having trouble to control click i'm trying to simulate control click without moving mouse, but i always get error when i run is there something wrong in my code.
PixelSearch, X, Y, 0, 0, 1292, 747, 0x00FF00, 0, fast
if(ErrorLevel=0)
sleep, 100
ControlClick, %X% %Y%, form1, Left, 1
There are a few things you've missed.
First, If Statements only execute the line beneath them. If you have more than 1 line of code, you have to put it in curly braces. It's called A Block/Blocking (Multiple Lines of Code Inside Braces)
Second, your ControlClick syntax is wrong. If you're passing coordinates, you have to put x and y in front of them so controlclick knows they're coords.
Incorrect:
ControlClick, %X% %Y%
Correct:
ControlClick, x%X% x%Y%
Third, if you look at your code, you have Left where WinText is supposed to be.
Syntax for ControlClick:
ControlClick [, Control-or-Position, WinTitle, WinText , WhichButton, ClickCount, Options, ExcludeTitle, ExcludeText]
You can't assume AHK knows Left is for the button. You have to tell it to skip that field by putting a comma there to indicate the field is blank. That way it knows to skip wintext and treat Left as the WhichButton field.
Incorrect:
ControlClick, %X% %Y%, form1, Left
Correct:
ControlClick, %X% %Y%, form1,, Left
Put it all together, and you have this:
PixelSearch, X, Y, 0, 0, 1292, 747, 0x00FF00, 0, fast
if (ErrorLevel = 0){
Sleep, 100
ControlClick, x%X% y%Y%, form1, , Left, 1
}
Let me know if that solves your problem.
Edit: Apparently this didn't fix your issue. I'm going to bet your controlclick and the info you're passing it are the problem.
Make sure you define CoordMode at the beginning of the script. You need to set it to fullscreen or relative.
Next, don't use a window's title for WinTitle unless there's a good reason to. Instead of form1, use something like ahk_exe chrome.exe. It's much more reliable.
#SingleInstance, Force
CoordMode, Pixel, Screen
return
F1::
PixelSearch, X, Y, 0, 0, 1292, 747, 0x00FF00, 0, fast
if (ErrorLevel = 0){
Sleep, 100
ControlClick, x%X% y%Y%, ahk_exe yourExeName.exe, , Left, 1
}
return
What program/site/game are you using this for?
I cobbled the following together from various posts:
keys = 0,9,8,7
loop, parse, keys, `,
{
Key_%A_Index% := A_LoopField
KeyCount++
}
return
XButton1::
Rotation ++
Send % Key_%Rotation%
if Rotation = %KeyCount%
Rotation = 0
return
#Persistent
SetTimer, Check, 1000 ;check every second
return
Check:
If (A_TimeIdle >= 3000)
Rotation = 0
return
The idea being that I press my mouse4 button and it cycles through the keys and then goes back to start, however I also wanted a loop so that if I don't press the button for 3 seconds, it resets back to the start of the sequence. The key sequence works however the idle reset doesnt and I'm not sure where to go from here to debug it.
1- You must let the #Persistent SetTimer, Check, 1000 part before the first return.
2- A_TimeIdle is sensible to any input, even a simple mouse move (by user or by script) resets it to zero. If you want to get the Idle time of this single hotkey use A_TimeSinceThisHotkey instead:
Check:
if (A_TimeSinceThisHotkey >= 3000)
{
Rotation = 0
}
return
I am struggling to make an AutoHotkey script, if anyone would help I would appreciate it.
the script im trying to make is: if I pressed F my mouse would move to the left as long as im pushing the F button from the current location I had my mouse on. This is what I have right now and it's not working.
Loop {
Sleep 10
MouseGetPos,x
if (MouseMove, -1000, 0, 100, R)
send {C down}
else if {C up}
break
}
return
Esc::ExitApp
Try this:
f::MouseMove -5, 0, 50, R
50 is the speed, and the R letter means that the offset is relative to the current cursor position. You don't need the loop, as the pressed key will generate subsequent events on its own.