Skip to content
Snippets Groups Projects

KDE Meta+drag for windows AHK v2

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Rijaja

    Modified from the v1 script (idk i missed there was an ahk v2 version i guess) (or maybe i found it elsewhere i don't remember). It looks like the original uses alt, this version uses the windows key.

    Win+Escape to make a window sticky Win+Left click drag to move a window Win+Right click drag to resize from the closest corner

    Edited
    kde.ahk 3.11 KiB
    #Requires AutoHotkey v2.0
    
    ; This is the setting that runs smoothest on my
    ; system. Depending on your video card and cpu
    ; power, you may want to raise or lower this value.
    SetWinDelay(16)
    
    CoordMode("Mouse")
    return
    
    ; win+esc
    #Esc:: {
        global
        WinSetAlwaysontop(-1, "A")
        return
    }
    
    #LButton:: {
        ; Get the initial mouse position and window id, and abort if the window is maximized.
        global
        MouseGetPos(&KDE_X1, &KDE_Y1, &KDE_id)
        KDE_Win := WinGetMinMax("ahk_id " KDE_id)
    
        if KDE_Win
            WinRestore("ahk_id " KDE_id)
    
        ; Get the initial window position.
        WinGetPos(&KDE_WinX1, &KDE_WinY1, , , "ahk_id " KDE_id)
        WinActivate("ahk_id " KDE_id) ; focus window
    
        loop {
            KDE_Button := GetKeyState("LButton", "P") ? "D" : "U" ; Break if button has been released.
    
            if (KDE_Button = "U")
                break
    
            ; Get the current mouse position.
            MouseGetPos(&KDE_X2, &KDE_Y2)
    
            ; Obtain an offset from the initial mouse position.
            KDE_X2 -= KDE_X1
            KDE_Y2 -= KDE_Y1
    
            ; Apply this offset to the window position.
            KDE_WinX2 := (KDE_WinX1 + KDE_X2)
            KDE_WinY2 := (KDE_WinY1 + KDE_Y2)
    
            ; Move the window to the new position.
            WinMove(KDE_WinX2, KDE_WinY2, , , "ahk_id " KDE_id)
        }
    
        return
    }
    
    #RButton:: {
        ; Get the initial mouse position and window id, and
        ; abort if the window is maximized.
        global
        MouseGetPos(&KDE_X1, &KDE_Y1, &KDE_id)
        KDE_Win := WinGetMinMax("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 {
            KDE_Button := GetKeyState("RButton", "P") ? "D" : "U" ; 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(
                KDE_WinX1 + (KDE_WinLeft + 1) / 2 * KDE_X2,
                KDE_WinY1 + (KDE_WinUp + 1) / 2 * KDE_Y2,
                KDE_WinW - KDE_WinLeft * KDE_X2,
                KDE_WinH - KDE_WinUp * KDE_Y2,
                "ahk_id " KDE_id
            )  ; X of resized window
    
            KDE_X1 := (KDE_X2 + KDE_X1) ; Reset the initial position for the next iteration.
            KDE_Y1 := (KDE_Y2 + KDE_Y1)
        }
    
        return
    }
    
    ; Prevent auto-repeat from breaking everything
    ~#:: {
        global
        KeyWait("LWin")
        return
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment