screen corner and edge functions Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kocelac205
Posts: 24
Joined: 04 Jul 2021, 01:15

screen corner and edge functions

Post by kocelac205 » 29 Nov 2021, 11:06

i am trying to modify the script from this post..

Code: Select all

;-------------------------------------------------------------------------------
#If isMousePos("TopRight") ; context for the following hotkeys
;-------------------------------------------------------------------------------
    WheelUp::   SendInput, {Volume_Up}
    WheelDown:: SendInput, {Volume_Down}

#If ; end of context



;-------------------------------------------------------------------------------
isMousePos(Position, maxDistance := 10) { ; return true if mouse is in position
;-------------------------------------------------------------------------------
    CoordMode, Mouse, Screen
    MouseGetPos, MouseX, MouseY

    ; check all the edges
    if InStr(Position, "Left")   and (maxDistance < MouseX)
        return False

    if InStr(Position, "Top")    and (maxDistance < MouseY) 
        return False

    if InStr(Position, "Right")  and (maxDistance < A_ScreenWidth - MouseX) 
        return False

    if InStr(Position, "Bottom") and (maxDistance < A_ScreenHeight - MouseY) 
        return False

    ; still here?
    return True
}
my goal is to activate the script when mouse is ositioned in the corners without any other triggers...eg send win+d when mouse position is in bottom right corner
the code i cud come up with this is given below..but it does not seem to activate when the mouse position is bottom right..however if i add another trigger (eg LButton:: ) then the script runs...clearly i am missing something here..if somebody can help me i wud be very greatful

Code: Select all


;-------------------------------------------------------------------------------
#If isMousePos("TopRight") ; context for the following hotkeys
;-------------------------------------------------------------------------------
     SendInput, #d
  

#If ; end of context



;-------------------------------------------------------------------------------
isMousePos(Position, maxDistance := 10) { ; return true if mouse is in position
;-------------------------------------------------------------------------------
    CoordMode, Mouse, Screen
    MouseGetPos, MouseX, MouseY

    ; check all the edges
    if InStr(Position, "Left")   and (maxDistance < MouseX)
        return False

    if InStr(Position, "Top")    and (maxDistance < MouseY) 
        return False

    if InStr(Position, "Right")  and (maxDistance < A_ScreenWidth - MouseX) 
        return False

    if InStr(Position, "Bottom") and (maxDistance < A_ScreenHeight - MouseY) 
        return False

    ; still here?
    return True
}

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: screen corner and edge functions

Post by mikeyww » 29 Nov 2021, 11:17

#If is used only with hotkeys and hotstrings-- explained in the documentation for it.

If I wanted to detect a mouse position without a hotkey trigger, I would SetTimer to check the position with MouseGetPos, and then act based on the position. CoordMode can be used to change the coordinates' point of reference.

Code: Select all

#Persistent
SetTimer, Check, 200
Check:
last := corner, corner := corner()
If (corner && corner != last)
 MsgBox, 64, Corner, %corner%
Return

corner() {
 ; 1 = Top left
 ; 2 = Bottom left
 ; 3 = Top right
 ; 4 = Bottom right
 CoordMode, Mouse
 MouseGetPos, xpos, ypos
 margin := 50
 x := border(xpos, margin, A_ScreenWidth - margin)
 y := border(ypos, margin, A_ScreenHeight - margin)
 Return x && y ? 2 * x + y - 2 : 0
}

border(pos, min, max) { ; 1 if pos < min; 2 if pos > max
 Return (pos < min) + 2 * (pos > max)
}
You could make yours work, too, if you set a timer.

kocelac205
Posts: 24
Joined: 04 Jul 2021, 01:15

Re: screen corner and edge functions

Post by kocelac205 » 29 Nov 2021, 12:13

i wud like to have the flexibility to have both button press and jus position based trigger...is using getkeystate the followin way the right way to use it?

Code: Select all

#Persistent
GetKeyState, LState, LButton


SetTimer, Check, 200
Check:
last := corner, corner := corner()


If (corner && corner != last && corner = 1 && LState = "D")
    Send, #d
Return

corner() {
 ; 1 = Top left
 ; 2 = Bottom left
 ; 3 = Top right
 ; 4 = Bottom right
 CoordMode, Mouse
 MouseGetPos, xpos, ypos
 margin := 50
 x := border(xpos, margin, A_ScreenWidth - margin)
 y := border(ypos, margin, A_ScreenHeight - margin)
 Return x && y ? 2 * x + y - 2 : 0
}

border(pos, min, max) { ; 1 if pos < min; 2 if pos > max
 Return (pos < min) + 2 * (pos > max)
}

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: screen corner and edge functions

Post by mikeyww » 29 Nov 2021, 12:31

What is the desired effect of the script?

kocelac205
Posts: 24
Joined: 04 Jul 2021, 01:15

Re: screen corner and edge functions

Post by kocelac205 » 29 Nov 2021, 12:39

sry for not being cear abt the purpose...let me list the desired effects
top left corner left button click - task view menu
top right corner - no function
bottom right corner mouse position - win+d
bottom left corner mouse position - windows button
left edge scroll - volume change
right edge scroll - brightness change

right now my only solution to achieve all is to run both your script and the first script together...even though autohotkey resource is very low it felt a bit wasteful to run two scripts together..i apologize for asking this much as it looks a bit out of scope for my understanding of autohotkey

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: screen corner and edge functions  Topic is solved

Post by mikeyww » 29 Nov 2021, 14:17

Code: Select all

edgeMargin := 20, volumeIncrement := 5, brightnessIncrement := 10
Process, Priority,, B
SetTimer, Check, 200
Check: ; Check for bottom corner
last := corner, corner := corner()
If !corner || corner = last
 Return
Switch corner {
 Case BOTTOMLEFT  := 2: Send {LWin}
 Case BOTTOMRIGHT := 4: WinMinimizeAll
}
Return

WheelUp::
WheelDown::
CoordMode, Mouse
MouseGetPos, x
factor := 1 - 2 * (Instr(A_ThisHotkey, "U") > 0)           ; 1 for WheelDown, -1 for WheelUp
Switch border(x, edgeMargin, A_ScreenWidth - edgeMargin) { ; 1 if x < edgeMargin; 2 if x > edgeMargin
 Case MIDDLE     := 0: Send {%A_ThisHotkey%}               ; Middle of screen
 Case LEFTEDGE   := 1: SoundSet, (factor = 1 ? "+" : "-") volumeIncrement
                       SoundBeep, 1500
                       If !WinExist("ahk_exe sndvol.exe")
                        Run, % "sndvol -f " Round((A_ScreenHeight/2) * 65536 + (A_ScreenWidth/2))
 Case RIGHTEDGE  := 2: ChangeBrightness(GetCurrentBrightness() + factor * brightnessIncrement)
}
Return

#If corner(TOPLEFT := 1)
LButton::Send #{Tab} ; Task view
#If

corner(number := "") {
 ; 1 = Top left
 ; 2 = Bottom left
 ; 3 = Top right
 ; 4 = Bottom right
 CoordMode, Mouse
 MouseGetPos, xpos, ypos
 margin := 50
 x := border(xpos, margin, A_ScreenWidth - margin)
 y := border(ypos, margin, A_ScreenHeight - margin)
 corner := x && y ? 2 * x + y - 2 : 0
 Return number > "" ? corner = number : corner
}

border(pos, min, max) { ; 1 if pos < min; 2 if pos > max
 Return (pos < min) + 2 * (pos > max)
}

ChangeBrightness(ByRef brightness, timeout := 1) {
 If brightness between 1 and 99
  For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery("SELECT * FROM WmiMonitorBrightnessMethods")
   property.WmiSetBrightness( timeout, brightness )
 Else brightness := brightness < 1 ? 0 : 100
}

GetCurrentBrightness() {
 For property in ComObjGet( "winmgmts:\\.\root\WMI" ).ExecQuery( "SELECT * FROM WmiMonitorBrightness" )
  Return property.CurrentBrightness
}

kocelac205
Posts: 24
Joined: 04 Jul 2021, 01:15

Re: screen corner and edge functions

Post by kocelac205 » 29 Nov 2021, 21:34

thank you...this code works flawlessly

reyn
Posts: 57
Joined: 21 Jul 2021, 18:08

Re: screen corner and edge functions

Post by reyn » 10 Jul 2023, 18:03

mikeyww wrote:
29 Nov 2021, 11:17
#Persistent
SetTimer, Check, 200
Check:
last := corner, corner := corner()
If (corner && corner != last)
MsgBox, 64, Corner, %corner%
Return

corner() {
; 1 = Top left
; 2 = Bottom left
; 3 = Top right
; 4 = Bottom right
CoordMode, Mouse
MouseGetPos, xpos, ypos
margin := 50
x := border(xpos, margin, A_ScreenWidth - margin)
y := border(ypos, margin, A_ScreenHeight - margin)
Return x && y ? 2 * x + y - 2 : 0
}

border(pos, min, max) { ; 1 if pos < min; 2 if pos > max
Return (pos < min) + 2 * (pos > max)
}
Hello. I was searching for some examples or proper cursor in corner detection code and got interested in your variant. But for pity i can't understand what is happening in the return lines... If you'll have spare time, would appreciate some elaboration. Thank you.

User avatar
mikeyww
Posts: 26438
Joined: 09 Sep 2014, 18:38

Re: screen corner and edge functions

Post by mikeyww » 10 Jul 2023, 20:05

The secret is already provided in the comments that you have pasted with each function. If you test the function with a few values, you can also demonstrate what the comments indicate.

Code: Select all

x && y ? 2 * x + y - 2 : 0
translates to: If x is neither zero nor null, and y is neither zero nor null, then return the value of two times x plus y minus 2; otherwise, return zero.

The goal of these functions was to determine whether the mouse was in any of the four corners and, if so, which corner it was.

Rohwedder recently posted an interesting script that uses small GUIs to accomplish the same thing via a mouseover technique.

reyn
Posts: 57
Joined: 21 Jul 2021, 18:08

Re: screen corner and edge functions

Post by reyn » 11 Jul 2023, 14:44

mikeyww wrote:
10 Jul 2023, 20:05
Rohwedder recently posted an interesting script that uses small GUIs to accomplish the same thing via a mouseover technique.
Can you please share a link or mention the name of the topic?


Post Reply

Return to “Ask for Help (v1)”