jDally987

AltDrag - AutoHotKey Window Dragging/Resizing (with Alt or Win keys) script - ONLY FOR AHK V2

Aug 20th, 2020
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #SingleInstance ;force
  2. TraySetIcon "AHK-altdrag.ico"
  3. ; #NoTrayIcon
  4. #UseHook
  5. SetWinDelay 5                   ; ms - default 100ms
  6. ; CoordMode "Mouse", "Screen"   ; causes craziness, leave it default ("Client") for now
  7.  
  8. ; KeyHistory
  9.  
  10.  
  11.  
  12.  
  13.  
  14. ; *********************************************             ; Customize your
  15. ; *************** BUTTONS SETUP ***************             ; trigger hotkey &
  16. ; *********************************************             ; Mouse buttons here
  17. global Trigger_Hotkey:= ">^"    ; must be symbol prefix
  18.                                 ; (like:  ^  or  >!  )
  19.                                 ; (NOT  Ctrl or RAlt )
  20.  
  21.  
  22. global Mousebtn_Move := "LButton"
  23. global Mousebtn_Resize := "MButton"
  24. global Mousebtn_Minimize := "RButton"
  25. global Mousebtn_Close := "XButton1"
  26. ; global Mousebtn_AlwaysOnTop := "XButton2"
  27. global Mousebtn_extra := "XButton2"
  28. ; *********************************************
  29. ; *********************************************
  30. ; *********************************************
  31.  
  32.  
  33.  
  34. ;_________________________________________________________________________________________________
  35. ;------------------------------------------------ HOTKEY INSTANTIATION
  36.  
  37. ; Hotkey Trigger_Hotkey Mousebtn_Move, "MoveWindow"         ; Move
  38. >^Lbutton:: MoveWindow()
  39. ; Hotkey Trigger_Hotkey Mousebtn_Resize, "ResizeWindow"     ; Resize
  40. >^Mbutton:: ResizeWindow()
  41. Hotkey Trigger_Hotkey Mousebtn_Minimize, "MinimizeWindow"   ; Minimize
  42. Hotkey Trigger_Hotkey Mousebtn_Close, "CloseWindow"         ; Close
  43. ; Hotkey Trigger_Hotkey Mousebtn_AlwaysOnTop, "AlwaysOnTop" ; Set always on top
  44. Hotkey Trigger_Hotkey Mousebtn_extra, "SendToBottom"        ; Send to bottom
  45.  
  46.  
  47. ;--------------------------------------------------------------------------------
  48.  
  49. >^WheelUp::Volume_Up                                        ; Volume wheel
  50. >^WheelDown::Volume_Down
  51.  
  52. ;--------------------------------------------------------------------------------
  53.  
  54. !MButton::                                                  ; Send window to bottom
  55. !#^+F22::       ;;;;;;;;;;; <--see *NOTE-TOUCHPAD*
  56. SendToBottom()
  57. return
  58.  
  59.  
  60.  
  61. ; *******************************************************
  62. ; *                 *NOTE-TOUCHPAD*                     *
  63. ; *                                                     *
  64. ; * When using a touchpad on a lot of laptops, Windows  *
  65. ; * simulates a middle-click using Win+Ctrl+Shift+F22   *
  66. ; * instead of sending the "true" MButton signal.       *
  67. ; *                                                     *
  68. ; * That's why it's necessary to set up another shortcut*
  69. ; * for that key combination, so a 3-finger touch on a  *
  70. ; * laptop touchpad will work the same as on a mouse.   *
  71. ; *******************************************************
  72. ;_________________________________________________________________________________________________
  73. ;------------------------------------------------ GLOBAL VARS/FUNCTIONS
  74.  
  75. global X:=0,Y:=0,winW:=0,winH:=0,winID:=0
  76. MouseSetWinID() {
  77.     MouseGetPos ,, winID
  78. }
  79. MouseSetWinIDAndCoordsActivate() {
  80.     ; bring to front, make transparent
  81.     MouseGetPos X,Y,winID       ; (X,Y RELATIVE TO ACTIVE CLIENT WINDOW)
  82.     WinActivate "ahk_id " winID
  83.     WinWaitActive "ahk_id " winID
  84. }
  85.  
  86. ;_________________________________________________________________________________________________
  87. ;------------------------------------------------ Actions: SIMPLE ACTIONS
  88.  
  89. MinimizeWindow() {
  90.     MouseSetWinID(), WinMinimize("ahk_id " winID), exit()
  91. }
  92.  
  93. CloseWindow() {
  94.     MouseSetWinID(), WinClose(winID), exit()
  95. }
  96.  
  97. SetAlwaysOnTop() {
  98.     MsgBox("Donelol")
  99. }
  100.  
  101. SendToBottom() {
  102.     MouseSetWinID(), WinMoveBottom("ahk_id " winID), exit()
  103. }
  104. ;_________________________________________________________________________________________________
  105. ;------------------------------------------------ Actions: RESIZE
  106.  
  107. ResizeWindow() {
  108.     MouseSetWinIDAndCoordsActivate()
  109.     ; WinGetPos ,,winW,winH,"ahk_id " winID
  110.     ;------------------------------------------             ----------
  111.     ; MenuSelect("ahk_id winID",, "0&", "Size")             ; unstable
  112.     ;------------------------------------------             ----------
  113.    
  114.     ; sends a message directly to the OS
  115.     WM_SYSCOMMAND := 0x112                      ; Window's "system menu" (alt+space)
  116.     SC_SIZE := 0xF000 ; - previously 0x05       ; Resize function
  117.     PostMessage WM_SYSCOMMAND, SC_SIZE,0,, "ahk_id " winID
  118.    
  119.     Sleep 50
  120.     WinGetPos ,,winW,winH,"ahk_id " winID
  121.    
  122.    
  123.    
  124.    
  125.     ; Freeze the cursor for a sec
  126.     ;DllCall( "ClipCursor", UInt,&R ) : DllCall( "ClipCursor" )
  127.     ; https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-clipcursor
  128.    
  129.    
  130.    
  131.    
  132.    
  133.     ;       autohotkey.com/board/topic/44588-restrict-mouse-move-to-area/?p=277613
  134.     ;       ----------------------------------------------------------------------
  135.    
  136.    
  137.     ; the '*' is Wildcard: Fire the hotkey even if extra modifiers are being held down.
  138. ;   *ScrollLock:: ; Hotkey will toggle status, scroll-lock key
  139. ;   Confine := !Confine
  140. ;
  141. ;   ClipCursor( Confine, 0, 50, A_screenwidth, A_screenheight)
  142. ;   Return
  143. ;
  144. ;
  145. ;   ClipCursor( Confine=True, x1=0 , y1=0, x2=1, y2=1 ) {
  146. ;    VarSetCapacity(R,16,0),  NumPut(x1,&R+0),NumPut(y1,&R+4),NumPut(x2,&R+8),NumPut(y2,&R+12)
  147. ;   Return Confine ? DllCall( "ClipCursor", UInt,&R ) : DllCall( "ClipCursor" )
  148. ;   }
  149.  
  150.     ; for reference, I got the script from here
  151.  
  152.     ;Is there a way to use AHK to effectively ignore the y-axis while still retaining full x-axis ;functionality?
  153.     ;
  154.     ;http://www.autohotkey.com/forum/topic42365.html&highlight=draw+mouse
  155.    
  156.    
  157.    
  158.    
  159.    
  160.    
  161.    
  162.    
  163.    
  164.     ; Which quadrant are we in?     (this part needs keyboard hook REAL BAD)
  165.     Send X<(winW/2) ? "{Left}" : "{Right}"      ; Left half?
  166.     Sleep 50
  167.     Send Y<(winH/2) ? "{Up}" : "{Down}"         ; Upper half?
  168.                                    
  169.                                    
  170.     ; Switch {
  171.         ; Case X<(winW/2) AND Y<(winH/2): Send "{Left}{Up}" ;upper-left corner
  172.         ; Case X>(winW/2) AND Y<(winH/2): Send "{Right}{Up}"    ;upper-right corner
  173.         ; Case X<(winW/2) AND Y>(H/2): Send "{Left}{Down}"  ;lower-left corner
  174.         ; Default: Send "{Right}{Down}"                 ;lower-right corner
  175.     ; }
  176.     ; OnMessage(0x232, ()=>WinSetTransparent("off", "ahk_id " winID))   ; WM_EXITSIZEMOVE
  177.     ; OnMessage(0x232, "SetAlwaysOnTop")    ; WM_EXITSIZEMOVE
  178.     WinSetTransparent "off", "ahk_id " winID
  179. }
  180. ;_________________________________________________________________________________________________
  181. ;------------------------------------------------ Actions: MOVE
  182. MoveWindow() {
  183.     MouseSetWinIDAndCoordsActivate() ; sets: X,Y coords/winID of mouse cursor
  184.     WinSetTransparent 215, "ahk_id " winID
  185.     if WinGetMinMax()
  186.         WinRestore
  187.    
  188.    
  189.     ;-------------TODO?---------------------------
  190.     ; additional MButton click -> toggle Maximized
  191.     ; Hotkey Mousebtn_Resize, ()=>WinMaximize("ahk_id " winID), "On"
  192.     ;---------------------------------------------
  193.    
  194.     ; Get initial mouse position (RELATIVE TO ACTIVE CLIENT WINDOW)
  195.     ; Xstart:=0,Ystart:=0
  196.     ; MouseGetPos Xstart, Ystart
  197.     ; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ deprecated if mousegetpos() is in global method
  198.    
  199.     ; Set X/Y vars ahead of time(for performance; see below inside loop)    [DEPRECATED!!!]
  200.     Xstart := X, Ystart := Y
  201.     ; X := Xstart, Y := Ystart
  202.    
  203.     ; Get initial Window position
  204.     WinGetPos winX, winY,,, "ahk_id " winID
  205.    
  206.     ; distanceBetween = ????????????????????
  207.    
  208.    
  209.     ; Record current MoveButton state   <------------- SEE *NOTE-MOVEBUTTONSTATE*
  210.     StartState := GetKeyState(Mousebtn_Move, "P")
  211.     while StartState = GetKeyState(Mousebtn_Move, "P")
  212.     {
  213.         ; update current mouse position
  214.         MouseGetPos X, Y
  215.        
  216.         ; Calculate new window position
  217.         winX += X-Xstart, winY += Y-Ystart
  218.        
  219.         ; you really want to ADD to the existing winX/Y vars, rather than
  220.         ; reassign them, because AHK rips open a new memory address for every
  221.         ; variable reassignment (just like Python), causing performance issues
  222.         ; when doing it again & again in every loop iteration
  223.         ; NOTE: at least, I think that's how it works... :/
  224.        
  225.         ;           TODO: borders (/snapping) stuff
  226.        
  227.         WinMove winX, winY,,, "ahk_id " winID
  228.     }
  229.     WinSetTransparent "off", "ahk_id " winID
  230.     ; Hotkey Mousebtn_Resize, , "Off"
  231.    
  232.    
  233.     ; *******************************************************
  234.     ; *             *NOTE-MOVEBUTTONSTATE*                  *
  235.     ; *                                                     *
  236.     ; * This flag helps when using programs that simulate   *
  237.     ; * the first click, like GestureSign - because in those*
  238.     ; * cases the btn's "pressed" state will only show "0"  *
  239.     ; * (whereas a real mouse always shows "1") - until you *
  240.     ; * click again. Therefore we'd rather check when the   *
  241.     ; * first button's state has been TOGGLED, rather than  *
  242.     ; * absolute down/up.                                   *
  243.     ; *                                                     *
  244.     ; * This way, you just need another tap/click to break  *
  245.     ; * the loop. Regular mice will still break the loop    *
  246.     ; * normally upon btn release.                          *
  247.     ; *******************************************************
  248. }
  249.  
Advertisement
Add Comment
Please, Sign In to add comment