Move the mouse pointer in one of the four corners

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Move the mouse pointer in one of the four corners

14 May 2021, 17:20

Move position the mouse pointer in one of the four corners, of the active Window (not maximized, preferably), and keep the left button pressing to change the size of the window. Click with left button for released the drag.
With age it is complicated to place the cursor at the corners. :shifty: I thought about this script to help me.
(I have an extra numeric keypad. That way I have the keyboard activated as a numeric block, and the extra keyboard as a scroll keyboard (the double of keys to be able to configure)).
It is a bad code :headwall: . How can it be improved?

Code: Select all

;Move position the mouse pointer in one of the four corners,
;of the active Window (not maximized, preferably), 
;and keep the left button pressing to change the size of the window.
;Click with left button for released the drag.

NumpadHome:: 
;Take the position of the active window and the size.
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, up to the left.
DllCall("SetCursorPos", "int", X, "int", Y)
Click, Down
Return

NumpadPgUp::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, up to the right.
DllCall("SetCursorPos", "int", X+W-10, "int", Y)
Click, Down
Return

NumpadEnd::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, down to the left.
DllCall("SetCursorPos", "int", X, "int", Y+H-10)
Click, Down
Return

NumpadPgDn::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, down to the rigth.
DllCall("SetCursorPos", "int", W+X-10, "int", H+Y-10)
Click, Down
Return
Last edited by SKAN on 14 May 2021, 17:25, edited 1 time in total.
Reason: Moved from Scripts and Functions
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Move the mouse pointer in one of the four corners

14 May 2021, 19:47

This should work:

Code: Select all

NumpadHome:: SetPos("left-top")
NumpadPgUp:: SetPos("right-top")
NumpadEnd::  SetPos("left-bottom")
NumpadPgDn:: SetPos("right-bottom")

SetPos(pos) {
   static flag := DWMWA_EXTENDED_FRAME_BOUNDS := 9
   hWnd := WinExist("A")
   WinGetPos, X, Y, W, H
   top := Y
   VarSetCapacity(RECT, 16, 0)
   res := DllCall("Dwmapi\DwmGetWindowAttribute", "Ptr", hWnd, "UInt", flag, "Ptr", &RECT, "UInt", 16) = 0
   dX := NumGet(RECT,  0, "Int")
   dW := NumGet(RECT,  8, "Int")
   dH := NumGet(RECT, 12, "Int")
   left   := res && dX != X     ? dX : X     + 3
   right  := res && dW != X + W ? dW : X + W - 3
   bottom := res && dH != Y + H ? dH : Y + H - 1
   CoordMode, Mouse
   MouseMove, InStr(pos, "left") ? left : right, InStr(pos, "top") ? top : bottom, 0
   if (A_Cursor ~= "SizeN...")
      Click, Down
}
or

Code: Select all

NumpadHome:: SetPos("left-top")
NumpadPgUp:: SetPos("right-top")
NumpadEnd::  SetPos("left-bottom")
NumpadPgDn:: SetPos("right-bottom")

SetPos(pos) {
   WinGetPos, X, Y, W, H, A
   CoordMode, Mouse
   MouseMove, InStr(pos, "left") ? X : X + W, InStr(pos, "top") ? Y : Y + H, 0
   while !(A_Cursor ~= "SizeN...") && A_Index < 15
      MouseMove, InStr(pos, "left") ? 1 : -1, InStr(pos, "top") ? 1 : -1, 0, R
   if (A_Cursor ~= "SizeN...")
      Click, Down
}
Last edited by teadrinker on 15 May 2021, 07:32, edited 1 time in total.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

15 May 2021, 05:40

Thank you, teadrinker :superhappy:

Not only a solution, but two. The two go very well. I am unable to understand the code accurately. teadrinker, you are at another level. I do not understand that imprecision of the autohotkey of having to add or subtract some numbers, when it should be mathematical. It was also happening to me.

Again, thank you! :bravo:
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Move the mouse pointer in one of the four corners

15 May 2021, 07:36

I edited my post a bit. Added a check before clicking whether the cursor is changed.
Good luck with learning AHK! :)
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

16 May 2021, 06:54

Hello, Teadriker

I went down the new updates, but in the end I am using my own version, because those that you have created do not work well outside the main monitor. It must be because the monitors do not have the same size and because in your script the size of the screen is not updated, it seems to remember (I think) the size of the main screen.

Thanks again.
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Move the mouse pointer in one of the four corners

16 May 2021, 07:53

You can try replacing MouseMove with SetCursorPos like this:

Code: Select all

SetPos(pos) {
   WinGetPos, X, Y, W, H, A
   DllCall("SetCursorPos", "Int", InStr(pos, "left") ? X : X + W, "Int", InStr(pos, "top") ? Y : Y + H)
   CoordMode, Mouse
   while !(A_Cursor ~= "SizeN...") && A_Index < 15
      MouseMove, InStr(pos, "left") ? 1 : -1, InStr(pos, "top") ? 1 : -1, 0, R
   if (A_Cursor ~= "SizeN...")
      Click, Down
}
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

16 May 2021, 09:32

Hello

The new change causes the script to not even work on the main monitor. On the other hand AutohotKey works differently with a normal window, which with a video player like Potplayer. I modified my script like this:

Code: Select all

;Move position the mouse pointer in one of the four corners,
;of the active Window (not maximized, preferably), 
;and keep the left button pressing to change the size of the window.
;Click with left button for released the drag.

CoordMode, Mouse

NumpadHome:: 
;Take the position of the active window and the size.
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, up to the left.
IfWinActive ahk_exe PotPlayerMini64.exe,
	{
	DllCall("SetCursorPos", "int", X, "int", Y)
	Click, Down
	}
else
DllCall("SetCursorPos", "int", X+8, "int", Y+1)
Click, Down
Return

NumpadPgUp::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, up to the right.
IfWinActive ahk_exe PotPlayerMini64.exe,
	{
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}
else
DllCall("SetCursorPos", "int", X+W-13, "int", Y)
Click, Down
Return

NumpadPgDn::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, down to the rigth.
IfWinActive ahk_exe PotPlayerMini64.exe,
	{
	DllCall("SetCursorPos", "int", W+X-10, "int", Y+H-10)
	Click, Down
	}
else
DllCall("SetCursorPos", "int", W+X-4, "int", H+Y-6)
Click, Down
Return

NumpadEnd::
WinGetPos, X, Y, W, H, A
;Move the mouse pointer, down to the left.
IfWinActive ahk_exe PotPlayerMini64.exe,
	{
	DllCall("SetCursorPos", "int", X, "int", Y+H-8)
	Click, Down
	}
else
DllCall("SetCursorPos", "int", X, "int", Y+H-6)
Click, Down
Return
But it does not work perfectly in two monitors. I know that with the Sysget function you can know the monitor. How to create a conditional rate from which I tell me that it is not on the primary monitor?, I give me a "false". It is already a question of challenge, because at the end this other code makes the window can be extended without going to the corners.

Code: Select all

*!RButton::
If DoubleAlt
{
    MouseGetPos,,,KDE_id
    ; Toggle between maximized and restored state.
    WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
    If KDE_Win
        WinRestore,ahk_id %KDE_id%
    Else
        WinMaximize,ahk_id %KDE_id%
    DoubleAlt := false
    return
}
; Get the initial mouse position and window id, and
; abort if the window is maximized.
MouseGetPos,KDE_X1,KDE_Y1,KDE_id
WinGet,KDE_Win,MinMax,ahk_id %KDE_id%
If KDE_Win
    return
; Get the initial window position and size.
WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
; Define the window region the mouse is currently in.
; The four regions are Up and Left, Up and Right, Down and Left, Down and Right.
If (KDE_X1 < KDE_WinX1 + KDE_WinW / 2)
    KDE_WinLeft := 1
Else
    KDE_WinLeft := -1
If (KDE_Y1 < KDE_WinY1 + KDE_WinH / 2)
    KDE_WinUp := 1
Else
    KDE_WinUp := -1
Loop
{
    GetKeyState,KDE_Button,RButton,P ; Break if button has been released.
    If KDE_Button = U
        break
    MouseGetPos,KDE_X2,KDE_Y2 ; Get the current mouse position.
    ; Get the current window position and size.
    WinGetPos,KDE_WinX1,KDE_WinY1,KDE_WinW,KDE_WinH,ahk_id %KDE_id%
    KDE_X2 -= KDE_X1 ; Obtain an offset from the initial mouse position.
    KDE_Y2 -= KDE_Y1
    ; Then, act according to the defined region.
    WinMove,ahk_id %KDE_id%,, KDE_WinX1 + (KDE_WinLeft+1)/2*KDE_X2  ; X of resized window
                            , KDE_WinY1 +   (KDE_WinUp+1)/2*KDE_Y2  ; Y of resized window
                            , KDE_WinW  -     KDE_WinLeft  *KDE_X2  ; W of resized window
                            , KDE_WinH  -       KDE_WinUp  *KDE_Y2  ; H of resized window
    KDE_X1 := (KDE_X2 + KDE_X1) ; Reset the initial position for the next iteration.
    KDE_Y1 := (KDE_Y2 + KDE_Y1)
}
return
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

16 May 2021, 14:21

Hello
I have modified my own code. It is not the most elegant, but it works. I used the MOUSGETPOS, XPOS, YPOS command (the value 3840 is the width of the first monitor, 4K) to know if the mouse is on the monitor two, and if that case is given the cursor will be positioned in one way or another (correct The initial value, adding or subtracting pixel) and if the active window is also the same PotPlayer player, both on one screen and another.

Code: Select all

NumpadHome::

CoordMode, Mouse
MouseGetPos, xpos, ypos

IfWinNotActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+8, "int", Y+1)
	Click, Down
	}
Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+8, "int", Y+2)
	Click, Down
	}

IfWinActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X, "int", Y+1)
	Click, Down
	}
	Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X-8, "int", Y)
	Click, Down
	}

xpos = 0
Return

;----------------

NumpadPgUp::

CoordMode, Mouse
MouseGetPos, xpos, ypos

IfWinNotActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}
Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}

IfWinActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}
Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-13, "int", Y)
	Click, Down
	}

xpos = 0
Return

;----------------

NumpadPgDn::

CoordMode, Mouse
MouseGetPos, xpos, ypos

IfWinNotActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-12, "int", Y+H-12)
	Click, Down
	}
Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-4, "int", H+Y-6)
	Click, Down
	}
IfWinActive ahk_exe PotPlayerMini64.exe
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-4, "int", Y+H-4)
	Click, Down
	}
Else If xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-4, "int", H+Y-6)
	Click, Down
	}

xpos = 0
Return

;----------------

NumpadEnd::

CoordMode, Mouse
MouseGetPos, xpos, ypos

IfWinNotActive ahk_exe PotPlayerMini64.exe
{
WinGetPos, X, Y, W, H, A
DllCall("SetCursorPos", "int", X+5, "int", Y+H-10)
Click, Down
}
Else If xpos > 3840
{
WinGetPos, X, Y, W, H, A
DllCall("SetCursorPos", "int", X+5, "int", Y+H-10)
Click, Down
}
IfWinActive ahk_exe PotPlayerMini64.exe
{
WinGetPos, X, Y, W, H, A
DllCall("SetCursorPos", "int", X+6, "int", Y+H-6)
Click, Down
}
Else If xpos > 3840
{
WinGetPos, X, Y, W, H, A
DllCall("SetCursorPos", "int", X+3, "int", Y+H-6)
Click, Down
}

xpos = 0
Return
teadrinker
Posts: 4309
Joined: 29 Mar 2015, 09:41
Contact:

Re: Move the mouse pointer in one of the four corners

16 May 2021, 15:02

wetware05 wrote: How to create a conditional rate from which I tell me that it is not on the primary monitor?

Code: Select all

MsgBox, % IsPrimaryMonitorFromWindow(WinExist(A))

IsPrimaryMonitorFromWindow(hWnd) {
   static MONITOR_DEFAULTTONEAREST := 2, MONITORINFOF_PRIMARY := 1
   hMon := DllCall("MonitorFromWindow", "Ptr", hWnd, "UInt", MONITOR_DEFAULTTONEAREST, "Ptr")
   VarSetCapacity(MONITORINFO, 40, 0)
   NumPut(40, MONITORINFO)
   DllCall("GetMonitorInfo", "Ptr", hMon, "Ptr", &MONITORINFO)
   Return NumGet(MONITORINFO, 36, "UInt") = MONITORINFOF_PRIMARY
}
I have only one, so I can't test.
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

16 May 2021, 15:57

Thanks teadrinker

It does not seem to work; always gives as a response 1. In any case I solved the problem through knowing the position of the mouse, if it gives a horizontal message greater than the horizontal of the main monitor, is that the mouse is on the monitor 2. May not be the best way , but it may be the simplest (from my limited knowledge, of course).
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Move the mouse pointer in one of the four corners

17 May 2021, 06:30

Hello.
What a shame! :oops:

The last script I shared was wrong, because I did not know very well how the conditional worked in Autohotkey. Two conditional were executed, one behind another. As I have studied philosophy (logic), I went to bed knowing about the problem and thinking how to solve it. Four conditions are given for each key pulsed:
1= Monitor 1, without PotPlayer
2= Monitor 2, without PotPlayer
3= Monitor 1, with PotPlayer
4= Monitor 2, with PotPlayer

As I do not want to deepen more about the conditional, this script works (use this script with the FastKey program, in four separate scripts and this is fine).

Code: Select all

NumpadHome::

CoordMode, Mouse
MouseGetPos, xpos, ypos
WindowName := "ahk_exe PotPlayerMini64.exe"

if !WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+8, "int", Y+1)
;	MsgBox, without PotPlayer, Monitor 1
	Click, Down
	}
if !WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+8, "int", Y+2)
	Click, Down
;	MsgBox, without PotPlayer, Monitor 2
	}
if WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X, "int", Y+1)
	Click, Down
;	MsgBox, with PotPlayer, Monitor 1
	}
if WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X, "int", Y)
	Click, Down
;	MsgBox,  with PotPlayer, Monitor 2
	}

;----------------

NumpadPgUp::

CoordMode, Mouse
MouseGetPos, xpos, ypos
WindowName := "ahk_exe PotPlayerMini64.exe"

if !WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}
if !WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-5, "int", Y+2)
	Click, Down
	}
if WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-4, "int", Y)
	Click, Down
	}
if WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+W-6, "int", Y)
	Click, Down
	}

;----------------

NumpadPgDn::

CoordMode, Mouse
MouseGetPos, xpos, ypos
WindowName := "ahk_exe PotPlayerMini64.exe"

if !WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-12, "int", Y+H-12)
	Click, Down
	}
if !WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-12, "int", H+Y-10)
	Click, Down
	}
if WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-4, "int", Y+H-4)
	Click, Down
	}
if WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", W+X-4, "int", H+Y-6)
	Click, Down
	}

;----------------

NumpadEnd::

CoordMode, Mouse
MouseGetPos, xpos, ypos
WindowName := "ahk_exe PotPlayerMini64.exe"

if !WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+5, "int", Y+H-10)
	Click, Down
	}
if !WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+5, "int", Y+H-10)
	Click, Down
	}
if WinActive(WindowName) and xpos < 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+6, "int", Y+H-6)
	Click, Down
	}
if WinActive(WindowName) and xpos > 3840
	{
	WinGetPos, X, Y, W, H, A
	DllCall("SetCursorPos", "int", X+3, "int", Y+H-6)
	Click, Down
	}
Return
(On the "NumPadhome" key, i placed AutohotKey put messages, to see that the conditional functioned.)

Teadrinker, your first scripts worked down, on the right and left, but none of the two positions above.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], marypoppins_1, Rohwedder, Spawnova and 153 guests