AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

KDE-Style Alt-Grab Window Move Convenience Script

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
ck



Joined: 25 Jul 2004
Posts: 8

PostPosted: Sun Jul 25, 2004 8:54 pm    Post subject: KDE-Style Alt-Grab Window Move Convenience Script Reply with quote

I did promise Chris to post this too, so here it is.
But first some quick

Background:
========

Do you ever work with multiple monitors? If you do, you probably have to move windows from one screen to the other quite a lot. Did it ever happen to you that one window for some reason managed to appear off-screen, so you couldn't grab the title bar anymore?

It happened to me last Friday...

I had to laugh out loud when I learned what loops you have to jump through in WindowsXP to remedy this situation.
Get this:
- You have to hover your mouse cursor on the window you want to move.
- Then you have to press [Alt]+[SPACE] to trigger the window menu
- Next you press [M] for move. (Warning: This key is localized, [V] in a German Windows version for example (yeah, I'm forced to use this version at work Sad ))
- Now you can use your keyboard (!) cursor keys [UP],[DOWN],[LEFT],[RIGHT] to move the window around.

Yeah, right.... Rolling Eyes Well, I already knew AutoHotKey from my recent Umlaute Convenience script so it didn't take me long to glimpse at AutoHotKeys' features to capture mouse input and to move windows around. Well it can do all that, too. Smile

Idea:
===

I have worked with KDE on top of Linux a number of times and really liked their solution to move windows around. Here you press [ALT] and click ANYWHERE within ANY window and just start dragging it around with your mouse. Just release [ALT] when you are done.

Well, my idea was to re-produce this behavior in Windows XP.

In this script, I also stumbled upon one minor AutoHotKey (or rather Windows API probably) limitation.
You will notice this when moving maximized windows. I will not go into more details here. See my posting "minor request: ToggleWindowFlags" in the "Wish List" sub forum.

Oh, btw. this script has one unexpected positive side effect. Sometimes windows freeze on me (due to network latencies) for an arbitrary amount of time. Moving these windows around with the AutoHotKey script immediately unfreezes them. Very Happy

Enjoy:

Script:
====

Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                      ;;
;; KDE-Style Alt-Grab Window Move Convenience Script    ;;
;;                                                      ;;
;; version 0.1 - 07/24/04                               ;;
;; ck <use www.autohotkey.com forum to contact me>      ;;
;;                                                      ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Usage Instructions:
;; -------------------
;; Load Script into AutoHotKey (only verified to work properly with 1.0.16 and Windows XP)
;; - Hold down the Alt key and lef-click anywhere in a window to move the window

LAlt & LButton::
; If this command isn't used, all commands except those documented otherwise (e.g. WinMove and InputBox)
; use coordinates that are relative to the active window
CoordMode, Mouse, Screen

; speed things up
SetWinDelay, 0

; get current mouse position
MouseGetPos, OLDmouseX, OLDmouseY, WindowID

; get the postition and ID of the window under the mouse cursor
WinGetPos, winX, winY, winW, winH ,ahk_id %WindowID%

; Turn off Window Animation
;RegRead, BoolMinAni, HKEY_CURRENT_USER,Control Panel\Desktop\WindowMetrics, MinAnimate
;if BoolMinAni = 1
;{
;   ; this has no effect until it's reloaded / re-logon... (-> disabled code)
;   RegWrite, REG_SZ, HKEY_CURRENT_USER, Control Panel\Desktop\WindowMetrics, MinAnimate, 0
;}

; this restore (= undo max or min) call tells the OS that originally
; maximized windows are now restored/normal and can be moved even after this script takes off
; this is absolutely mandatory for windows which remember their last location like IE windows
; otherwise you cannot properly maximize them anymore

;WinHide, ahk_id %WindowID%
WinRestore, ahk_id %WindowID%
WinMove, ahk_id %WindowID%,,%winX%,%winY%,%winW%,%winH%
;WinShow, ahk_id %WindowID%

Loop
{
   ; is the User still keeping that Alt key down?
   GetKeyState, AltKeyState, Alt, P
   if AltKeyState = U ; key has been released
   {
      break
   }
      
   ; get new mouse position
   MouseGetPos, newMouseX, newMouseY

   ; get relative mouse X movement
   if newMouseX < %OLDmouseX%
   {
      ; mouse was moved to the left
      Xdistance = %OLDmouseX%
      EnvSub, Xdistance, %newMouseX%   
      EnvSub, winX, %Xdistance%
   }
   else if newMouseX > %OLDmouseX%
   {
      ; mouse was moved to the right
      Xdistance = %newMouseX%
      EnvSub, Xdistance, %OLDmouseX%   
      EnvAdd, winX, %Xdistance%
   }
   else
   {
      ; mouse X coordinate wasn't changed
   }
   
   ; set OLDmouseX
   OLDmouseX = %newMouseX%

   ; repeat the same stuff for the Y-axis
   if newMouseY < %OLDmouseY%
   {
      Ydistance = %OLDmouseY%
      EnvSub, Ydistance, %newMouseY%   
      EnvSub, winY, %Ydistance%
   }
   else if newMouseY > %OLDmouseY%
   {
      Ydistance = %newMouseY%
      EnvSub, Ydistance, %OLDmouseY%   
      EnvAdd, winY, %Ydistance%
   }
   else
   {
   }
   OLDmouseY = %newMouseY%

   ; move Window accordingly
   WinMove, ahk_id %WindowID%,,%winX%,%winY%
}
return


Last edited by ck on Mon Aug 09, 2004 9:35 pm; edited 2 times in total
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Tue Jul 27, 2004 1:37 am    Post subject: Reply with quote

I finally got a chance to try this. Very cool script.

Quote:
only verified to work with 1.0.25 and Windows XP

Minor typo; I think you meant 1.0.15

Quote:
else
{
}

You might be using the above as just part of your coding style, but in case not: You can omit the final else statement if it's empty.

With your permission, I'll post this to the script showcase at http://www.autohotkey.com/docs/scripts/ , perhaps with a few minor style changes since many users study these scripts to learn the commands and syntax.

Great work and thanks for sharing it. I'm sure many both now and in the future will quietly use and benefit from this script.

edit: fixed typo


Last edited by Chris on Tue Jul 27, 2004 10:46 am; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail
ck



Joined: 25 Jul 2004
Posts: 8

PostPosted: Tue Jul 27, 2004 8:10 am    Post subject: Reply with quote

Chris wrote:
I finally got a chance to try this. Very cool script.
[...] Great work and thanks for sharing it. I'm sure many both now and in the future will quietly use and benefit this script.


Glad you like it. Smile
But don't forget that without your efforts in creating and documenting AutoHotKey, I wouldn't have been able to write this. Thanks again!

Quote:

Quote:
only verified to work with 1.0.25 and Windows XP

Minor typo; I think you meant 1.0.15


Oops. Yes, of course. I have the same typo in my GermanUmlaute.ahk.

Quote:

Quote:
else
{
}

You might be using the above as just part of your coding style, but in case not: You can omit the final else statement if it's empty.


Yes, I know. In this case I decided consciously to use it for better readability.

Quote:

With your permission, I'll post this to the script showcase at http://www.autohotkey.com/docs/scripts/ , perhaps with a few minor style changes since many users study these scripts to learn the commands and syntax.


Sure, I'd be honoured. Go ahead. Smile These showcase scripts helped kick-start me, too.
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Mon Jan 31, 2005 1:23 pm    Post subject: Reply with quote

Partially inspired by this topic, Jonny has posted the first version of his ultimate KDE-style window dragging/resizing script. If you have an interest, please give it a try and post feedback. http://www.autohotkey.com/forum/viewtopic.php?t=2062
Back to top
View user's profile Send private message Send e-mail
andrei
Guest





PostPosted: Sat Sep 19, 2009 9:17 pm    Post subject: Reply with quote

works awesome in windows 7 too.

the mouse button needs a click then move with alt pressed, not drag to work. not sure if it's a bug or feature, but works nifty!
Back to top
guest92
Guest





PostPosted: Fri Jan 22, 2010 1:30 pm    Post subject: true Reply with quote

ture works very nice with win 7 Smile
Back to top
guest92
Guest





PostPosted: Fri Jan 22, 2010 1:48 pm    Post subject: question Reply with quote

how do i have to change the code, so LButton has to be pressed and alt can be released, so i can move the window around with only the mouse button pressed and not the alt button pressed, i hope you understand, what i mean ^^
Back to top
Talisman



Joined: 24 Feb 2010
Posts: 36

PostPosted: Mon Mar 14, 2011 1:43 pm    Post subject: Modified Reply with quote

Thanks ck for your original script.

I've modified it somewhat to release the grab on either releaseing alt or the mouse-button - so releasing either Alt or LButton will stop the window from moving - as well a toltip to display the top/left.

I've also added in the resize functionality that linux provides in the same style - this with Alt+RButton, and the tooltip with width/height displayed. Releasing either the Alt or RButton also stops resizing the window.

I've stored the modified script on my own site at : http://development.twdev.net/index.php/ahk/linux-win-grab/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group