Move mouse between two monitors - autohotkey

How can I move the mouse pointer between monitors using a keyboard shortcut? I'm using autohotkey.
I didn't find a straightforward answer to it, so here's what I suggest.

Here's my approach, using Ctrl+Space for this function:
^Space::
CoordMode, Mouse, Screen ; This is needed to assure that you get your mouse coordinates related to the screen, not to the window
MouseGetPos, MouseX, MouseY
if( MouseX > 1920) ; 1920 is the Width of my monitor number 1, replace it with yours
{
MouseMove, -A_ScreenWidth, 0, 0, R
}
else
{
MouseMove, A_ScreenWidth, 0, 0, R
}
return

Related

How do I activate a script only if the mouse is at a certain coordinate of the screen without interfering with my keyboard?

I cooked up a script that lets me map my keyboard's media shortcuts to my mouse LRM buttons when they are pressed while the mouse coordinate is leftmost or rightmost of the screen. While it does work, I'm having having strange side effects:
When I have caps lock on, ever few strokes the letter comes out lowercase.
When I use shift to type capital letters for an extended period of time, this will turn on caps lock
Using the keyboard history, I see that my script is constantly sending the "Alt Up" key, I did this so that it release the "Alt Down" state, but something is off.
My goal is to send an modifier key when a mouse is over a certain coordinate, so that when I click with that mouse button, it launches another ahk-programmed shortcut. But can't figure out where the logic error is in my code or thinking process.
Here's the script:
; ------------------------
; Global Initializers
#InstallKeybdHook
#MaxThreadsPerHotkey 1
; ---------------------
; Control Spotify; position your mouse top-most edge and use L/M/R-mouse keys.
SetTimer, WatchCursorx, 1000
return
WatchCursorx:
CoordMode, Mouse, Screen
MouseGetPos, xpos, ypos
;Based on location of the mouse simulate shortcut activation
If (xpos == 2559 || xpos == 0)
{
Send {Alt Down}
}
Else
{
Send {Alt Up}
}
return
;Define shortcuts mentioned above
!RButton::
Send {Media_Next}
return
!LButton::
Send {Media_Prev}
return
!MButton::
send {Media_Play_Pause}
return
#If(docs) is meant for this.
You could use it for example like this:
CoordMode, Mouse, Screen
#If, MouseOnTheRight()
LButton::SendInput, {Media_Prev}
RButton::SendInput, {Media_Next}
MButton::SendInput, {Media_Play_Pause}
#If
MouseOnTheRight()
{
MouseGetPos, x
return x == A_ScreenWidth - 1
}
Per my comments, try it like this:
CoordMode, Mouse, Screen
~RButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
{
Send {Media_Next}
sleep, 500
Send {esc} ' this gets rid of right context menu
}
return
~LButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
Send {Media_Prev}
return
~MButton::
MouseGetPos, xpos, ypos
If (xpos == 2559 || xpos == 0)
Send {Media_Play_Pause}
return
Note, the preceding ~ lets the original mouse click go through so ordinarily the context menu will come up on right click. I add a Sleep and Send Escape key to dismiss . . . Ymmv

AutoHotkey Run script while holding down key

I need help with a script, i want it to only run while im holding down a key. Heres the script:
;If you use this, you have to use absolute screen coordinates.
CoordMode, Mouse, Screen
;Suppose a 100x100 px bounding box for your game inventory.
;Eg., from (500, 500) to (600, 600)
#if GetKeyState("joy5")
joy5:: MouseMove, 1771, 531
joy5 Up::MouseMove %oldx%,%oldy%
Numpad8::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent
yMoved := yCurrent - 35
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
MouseGetPos, CoordXRec, CoordYRec
MouseMove, xMoved, yMoved
if(yMoved < 503 && yMoved > 350 && yMoved > 360){
MouseMove 1846, 166
}
if(yMoved < 145){
MouseMove, %CoordXRec%, %CoordYRec%, 0
}
if(yMoved < 718 && yMoved < 720 && yMoved > 680){
MouseMove 1771, 671
}
return
}
Numpad5::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent
yMoved := yCurrent +35
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
MouseMove, xMoved, yMoved
if(yMoved > 285 && yMoved < 360){
MouseMove 1773, 526
}
if(yMoved > 697 && yMoved < 715){
MouseMove 1772, 736
}
return
}
Numpad4::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent -40
yMoved := yCurrent
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
if (xMoved > 1740) {
MouseMove, xMoved, yMoved
}
return
}
Numpad6::
{
;Get current Mouse coords
MouseGetPos, xCurrent ,yCurrent
;Calculate future Mouse coords
xMoved := xCurrent +40
yMoved := yCurrent
;Check if the future mouse postion will be
;below the top border of your bounding box,
;aka still inside it, after it has moved.
;If so, proceed and move the mouse,
;otherwise do nothing.
if (xMoved < 1917) {
MouseMove, xMoved, yMoved
}
return
}
Basicly you control the mouse with WASD and theres some other functionality to it aswell but i want to make it so that you have to hold down a key in order to move. Thanks!
only move when holding down a key.
Move mouse to 500,500 when Number-Pad-8 is pressed and NumLock is on. Return mouse to original location when key is released.
Numpad8::move()
Numpad8 UP::unmove()
move()
{
global oldx,oldy
MouseGetPos oldx,oldy
MouseMove 500,500
}
unmove()
{
global oldx,oldy
MouseMove %oldx%,%oldy%
}
To answer your original question (if the question changed so drastically, please open another one and finish this one):
You can dis/enable hotkeys dynamically, using the Hotkey-command. Assuming your masterkey is Space:
space:: ; this is a static hotkey definition
hotkey, numpad8, moveMouse1 ; this is a dynamic hotkey definition
hotkey, numpad6, moveMouse2
; etc
return
space up::
hotkey, numpad8, OFF ; this is a dynamic hotkey removal
hotkey, numpad6, OFF
; etc
return
moveMouse1: ; this is a label
moveMouse 50, 100
; your actions
return
moveMouse2:
; ....
return

Click and drag one pixel in given direction

I am looking for a script that will click and hold the right mouse button down and drag in a given direction 1 pixel every "x" or 2 seconds. Something I can either tell to move in given direction by hitting the corresponding direction key or by adjusting the script manually.
Thank you!
Not entirely sure what you want, but try this:
CoordMode, Pixel, Screen
direction = left
secondsBetweenMoves = 0.1
F1:: ;F1 to start it
SendInput, {LButton Down}
SetTimer, Move, %secondsBetweenMoves%
Return
F2:: ;F2 to end it
SendInput, {LButton Up}
SetTimer, Move, Off
Return
Move:
MouseGetPos, mouseX, mouseY
If (direction = "left") {
MouseMove, mouseX-1, mouseY
}
Else If (direction = "right") {
MouseMove, mouseX+1, mouseY
}
Else If (direction = "up") {
MouseMove, mouseX, mouseY-1
}
Else If (direction = "down") {
MouseMove, mouseX, mouseY+1
}
Return
The send event command might make your life a whole lot easier. For example, what you're trying to do in a simplistic infinite loop:
coordmode, mouse, screen
setmousedelay, 0 # This makes the mouse move extremely fast
loop {
mousegetpos, mx, my
mx := mx+1
sendevent {click, r, down}
sendevent {click, %mx%, %my%, r, up}
sleep 2000
}
This loop will hold down the right button and move one pixel to the right every two seconds. If you want it to move to the left, change
mx := mx+1
to
mx := mx-1
Making it move up or down is the same mechanism, but adding or subtracting to the variable "my" instead.
This loop can be made into a timer, with hotkeys to enable or disable it.

Get real mouse displacement while dragging

I'm trying to get the final dragging point (right button) with AutoHotKey:
~RButton::
CoordMode, Mouse, Screen
MouseGetPos, x0, y0
while GetKeyState("RButton")
{
MouseGetPos, x1, y1
Sleep, 10
}
MsgBox X: %x1% Y: %y1%
return
What it does is to wait the RightButton of the mouse to be pressed, gather x0 and y0 (inicial coordinates) and while the button is still pressed it gets the position of the mouse again (each 10 miliseconds).
After that it just displays the final coordinates.
It works nicely in normal environment but in this particular case this script needs to be executed in an application that takes control of the mouse when the right button is pressed. What that particular program does is to bring the mouse pointer to the center of the screen and when the button is realised it leaves it in the initial position. (x1, y1 are always the center of my screen, in pixels).
I believe that internally this program gathers the displacement of the mouse (while dragging) and use it for user interaction.
My question is: is there a way to obtain the real mouse input rather than looking at the screen and searching for the mouse pointer ( ~MouseGetPos) ? Is that achievable with AutoHotKey?
You can use a mouse hook to get notified for every mouse movement. MSDN Hooks
To use hooks with AHK you have to use DllCall.
#Persistent
MouseHook := DllCall("SetWindowsHookEx", "int", 14 ; WH_MOUSE_LL = 14
, "uint", RegisterCallback("MouseProc"), "uint", 0, "uint", 0)
return
MouseProc(nCode, wParam, lParam)
{
global MouseHook
Critical
if wParam = 0x200 ; WM_MOUSEMOVE
{
ToolTip % NumGet(lParam+0,0,"int") ", " NumGet(lParam+4,0,"int")
}
return DllCall("CallNextHookEx", "uint", MouseHook
, "int", nCode, "uint", wParam, "uint", lParam)
}
Example Source

change the coordmode from the active window to any window

this is the situation:
1) I have 3 Windows;
2) My mouse is positioned over any of them (active table under the mouse cursor);
3) I have the ahk_id of both Windows (stored in global variables);
4) Every 5 seconds, I would check (regardless of the movement of the mouse cursor), if a pixel of a specific window (window1, window2...) has a certain color;
5) Click with a controlclik of that pixel and regain control of the starting window under the mouse cursor.
checktime(){
var := Mod(A_Sec, 5)
if (var = 0){
checkpixel(window1) ; window1 is an ahk_id, stored in a global variable
checkpixel(window2)
checkpixel(window3)
}
checkpixel(window){
CoordMode, ToolTip, window ; this line of code is definitely wrong, what do you recommend?
pixelgetcolor, color, 440, 295
if(color=0x4E3500){
controlclick, x440 y295, ahk_id %window%
}
thanks in advance for the answers!