Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

KDE-Style Alt-Grab Window Move Convenience Script


  • Please log in to reply
7 replies to this topic
ck
  • Members
  • 8 posts
  • Last active: Aug 29 2004 04:03 PM
  • Joined: 25 Jul 2004
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 :( ))
- Now you can use your keyboard (!) cursor keys [UP],[DOWN],[LEFT],[RIGHT] to move the window around.

Yeah, right.... :roll: 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. :)

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. :D

Enjoy:

Script:
====

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                                                      ;;
;; 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


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
I finally got a chance to try this. Very cool script.

only verified to work with 1.0.25 and Windows XP

Minor typo; I think you meant 1.0.15

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.autohotke...m/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

ck
  • Members
  • 8 posts
  • Last active: Aug 29 2004 04:03 PM
  • Joined: 25 Jul 2004

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. :-)
But don't forget that without your efforts in creating and documenting AutoHotKey, I wouldn't have been able to write this. Thanks again!

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.

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.

With your permission, I'll post this to the script showcase at http://www.autohotke...m/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. :) These showcase scripts helped kick-start me, too.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
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.autohotke...opic.php?t=2062

andrei
  • Guests
  • Last active:
  • Joined: --
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!

guest92
  • Guests
  • Last active:
  • Joined: --
ture works very nice with win 7 :)

guest92
  • Guests
  • Last active:
  • Joined: --
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 ^^

Talisman
  • Moderators
  • 51 posts
  • Last active: Feb 26 2014 02:22 PM
  • Joined: 24 Feb 2010
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.t...linux-win-grab/