Jump to content


Photo

Alt Drag Windows


  • Please log in to reply
5 replies to this topic

#1 guidez

guidez
  • Members
  • 5 posts

Posted 02 August 2012 - 02:08 AM

Hi. I have two questions about this script, which lets you move windows when you alt+left click them.

1) How can I add a line that says "if the mouse is over the desktop, don't run the script"; because alt-clicking the desktop moves all the icons and that's rather annoying. :(

2) This script doesn't take into account the active window, just the mouse position; so, how can I edit it so that when I [alt] CLICK on a window, that window becomes active?

Thank you :)


Alt & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
    SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
    SetTimer, EWD_WatchMouse, off
    return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
    SetTimer, EWD_WatchMouse, off
    WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
    return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return


#2 sumon

sumon
  • Moderators
  • 1310 posts

Posted 02 August 2012 - 08:22 AM

Requires AHK v1.1 (_L).

#if !MouseIsOver("ahk_class ahk_class Progman")
Alt & LButton::
; your code...
#if

Use in combination with the function below. Oh, and I am not sure if progman is the class of the desktop, I think it was.

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle " ahk_id " Win)
}


#3 guidez

guidez
  • Members
  • 5 posts

Posted 02 August 2012 - 10:11 PM

Requires AHK v1.1 (_L).

#if !MouseIsOver("ahk_class ahk_class Progman")
Alt & LButton::
; your code...
#if

Use in combination with the function below. Oh, and I am not sure if progman is the class of the desktop, I think it was.

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle " ahk_id " Win)
}


Did you try it out yourself? Didn't work for me..gave me a bunch of errors. Maybe I didn't add those lines properly? :?

#4 PAEz

PAEz
  • Members
  • 1 posts

Posted 30 January 2013 - 03:01 PM

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle " ahk_id " Win)
}




#if !MouseIsOver("ahk_class ahk_class WorkerW")
Alt & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
    SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return
#if


EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
    SetTimer, EWD_WatchMouse, off
    return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
    SetTimer, EWD_WatchMouse, off
    WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
    return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return
 


#5 Bret Leduc

Bret Leduc
  • Members
  • 1 posts

Posted Today, 05:57 AM

Is there a way to change the script so you can move the windows by just holding the Alt key?  (Without having to hold down the left mouse button)

I tried this (which I got from here) but it doesn't do anything  (Modifications in red):

 

; This script modified from the original: http://www.autohotke...yWindowDrag.htm
; by The How-To Geek
; http://www.howtogeek.com

Alt::  ; Changed from:  Alt & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin%
if EWD_WinState = 0  ; Only if the window isn't maximized
    SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_AltState, Alt, P  ; Changed from:  GetKeyState, EWD_LButtonState, LButton, P
if EWD_AltState = U  ; Alt has been released, so drag is complete.

; Changed from:  if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
    SetTimer, EWD_WatchMouse, off
    return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
    SetTimer, EWD_WatchMouse, off
    WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
    return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

 

 



#6 lain

lain
  • Members
  • 208 posts

Posted Today, 11:10 AM

Using Alt as a hotkey on its own will have unpractical side effects!The closest i can get to what you asked for is pressing alt and clicking on the window you want to move.This window will "stick" to the mousepointer (you can release the left mousebutton) till you release the alt key.

!Lbutton::
coordmode,mouse,screen
MouseGetPos,Xm,Ym,IDwin
WinGetPos,WinXm,WinYm,WinW,WinH,ahk_id %IDwin%

Loop
{
    GetKeyState,Alt_,Alt,P 
     
    If (Alt_ = "U")
        break
    MouseGetPos,XMnew,YMnew
    
    XMnew -= Xm 
    YMnew -= Ym
    WinGotoX:=(WinXm + XMnew)
    WinGotoY:= (WinYm + YMnew)
   
    WinMove,ahk_id %IDwin%,,%WinGotoX%,%WinGotoY% 
}
return

esc::exitapp