Jump to content

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

MButton: Close windows(taskbar+titlebar) & open IE links


  • Please log in to reply
38 replies to this topic
evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
Features:
Middle click sends a normal middle click unless:
- Over the taskbar: Closes the window whose button is under the mouse (aborts if window prompts to save,etc).
- Over the titlebar of a window: closes the window (can be made more sensitive to specific window areas - see Shimanov's post:
<!-- m -->http://www.autohotke...opic.php?t=8486<!-- m --> ).
- Over an internet explorer class window: Opens a link in a new window (keep holding the button down to switch back to the original window).


MButton::
  SetBatchLines, -1
  CoordMode, Mouse, Screen
  SetMouseDelay, -1 ; no pause after mouse clicks
  SetKeyDelay, -1 ; no pause after keys sent
  MouseGetPos, ClickX, ClickY, WindowUnderMouseID
  WinActivate, ahk_id %WindowUnderMouseID%

  ; WM_NCHITTEST 
  SendMessage, 0x84,, ( ClickY << 16 )|ClickX,, ahk_id %WindowUnderMouseID% 
  WM_NCHITTEST_Result =%ErrorLevel%
      /*
      #define HTERROR             (-2)
      #define HTTRANSPARENT       (-1)
      #define HTNOWHERE           0
      #define HTCLIENT            1
      #define HTCAPTION           2
      #define HTSYSMENU           3
      #define HTGROWBOX           4
      #define HTSIZE              HTGROWBOX
      #define HTMENU              5
      #define HTHSCROLL           6
      #define HTVSCROLL           7
      #define HTMINBUTTON         8
      #define HTMAXBUTTON         9
      #define HTLEFT              10
      #define HTRIGHT             11
      #define HTTOP               12
      #define HTTOPLEFT           13
      #define HTTOPRIGHT          14
      #define HTBOTTOM            15
      #define HTBOTTOMLEFT        16
      #define HTBOTTOMRIGHT       17
      #define HTBORDER            18
      #define HTREDUCE            HTMINBUTTON
      #define HTZOOM              HTMAXBUTTON
      #define HTSIZEFIRST         HTLEFT
      #define HTSIZELAST          HTBOTTOMRIGHT
      #if(WINVER >= 0x0400)
      #define HTOBJECT            19
      #define HTCLOSE             20
      #define HTHELP              21
      */



; Close window with titlebar click
  If WM_NCHITTEST_Result in 2,3,8,9,20,21 ; in titlebar enclosed area - top of window
    {
    PostMessage, 0x112, 0xF060,,, ahk_id %WindowUnderMouseID% ; 0x112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE
    Return
    }



; Close taskbar program click
  IfWinActive, ahk_class Shell_TrayWnd
    {
    MouseClick, Right, %ClickX%, %ClickY%
    Sleep, 50
    Send, c
    WinWaitNotActive,  ahk_class Shell_TrayWnd,, 0.5 ; wait for save dialog, etc
    If ErrorLevel =1
      Send, !{Tab}
    Return
    }


; Open IE link in new window
  IfWinActive, ahk_class IEFrame ; going to click on a link to open in new window
    {
    If A_Cursor =Unknown ; pointer is probably the Hand icon (for links)
      {
      StatusBarGetText, IEStatusBarText, 1
      StringLeft, IEStatusBarText, IEStatusBarText, 4
      If IEStatusBarText = java ; javascript - should open in its own new window anyhow
        {
        Click, %ClickX%, %ClickY%
        SetTimer, OpenIELinkInNewWindowSleep, 400 ; new thread
        }
      Else
        {
        Send +{Click %ClickX%, %ClickY%}  ; Shift+LeftClick
        SetTimer, OpenIELinkInNewWindowSleep, 400 ; new thread
        }
      Return
      }
    }


; else send normal middle click
  If GetKeyState("MButton", "P") ; The middle button is physically down
    MouseClick, Middle, %ClickX%, %ClickY%,, Down
  Else
    MouseClick, Middle, %ClickX%, %ClickY%
Return



OpenIELinkInNewWindowSleep: ; if still holding middle mouse down, returns to original window
  SetTimer, OpenIELinkInNewWindowSleep, Off
  WinWaitNotActive, ahk_id %WindowUnderMouseID%,, 1
  Sleep, 100
  WinMaximize, A
  If GetKeyState("MButton", "P") ; The middle button is physically down
    WinActivate ahk_id %WindowUnderMouseID%
Return


Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
Making the middle button context sensitive is very useful. I've put a lot of functions onto mine too.

Your script is ultra-context sensitive since it detects titlebar and taskbar too. Thanks for sharing it.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I've always wanted to add some extra functionality to the middle mouse button. Thanks for the code!

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
I've made some changes to the script which make use of shimanov's code for the titlebar sensitivity to clicks:
<!-- m -->http://www.autohotke...opic.php?t=8486<!-- m -->
and generally improved some of the functionality and code so posted an update above.

jballi
  • Members
  • 1029 posts
  • Last active:
  • Joined: 01 Oct 2005
I'm a fan of this code. I've been using a variation of it since you first posted it. It's a bunch of features all bundled into the middle mouse button. Thanks for sharing! :)

I have a few questions/comments:

1) I noticed that you mix the use of MouseClick and Click. It certainly doesn't make any difference but you probably don't need to use both (?) and when using/sending Click you don't need to specify the X,Y coords since you're not repositioning the mouse.

2) There is no reason to send an Alt-Tab to return to the previous window since you already have the ID for the previous window. I would just reactiavte the window:
WinActivate ahk_id %WindowUnderMouseID%
I know. It does the exact same thing.

3) I'm not sure that you need the Sleep statements in the OpenIELinkInNewWindowSleep subroutine/timer since you are already sleeping via the WinWaitNotActive command. However, I did notice that the WinTile parameter for WinWaitNotActive is not defined correctly. Try the following:

WinWaitNotActive, ahk_id %WindowUnderMouseID%,, 1

These are all very tiny changes and I might be wrong about all of them. Once again, thanks for the code.

Them be my thoughts...

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
@ jballi:
Thanks for the comments:

1) I couldn't get Click to work with sending the middle mouse button down, so I reverted to just using MouseClick for that bit as it worked without any hassle. I was trying " Click Down Middle %ClickX%, %ClickY%" - but that seems to keep the middle mouse button pressed down even after you release it and it screws up the other mouse buttons until you close the script (ctrl-alt-del) and click the middle mouse again. I also tried adding "$" infront of the MButton hotkey. Maybe a bug or a mistake in my syntax?
I decided to put in the co-ords just to make sure the correct thing would be clicked if the user happened to have the mouse on the edge of a link, etc and was moving the mouse quickly. It shouldn't be necessary in 99% of clicks, but better safe than sorry.

2) You're right about Alt-Tab of course :roll: :lol:

3) The Sleep is for 2 reasons. a) The total should come to around 500ms so that the middle button state can be checked to see if the user wants to switch back to the original window or not (although I think I see your point of using SetTimer as the delay instead of the initial Sleep) otherwise the button state is checked too quickly to detect if the user really wants to hold the middle button down or was just pressign it still. B) IE can be a bit stupid sometimes and the window can steal focus if you try to switch away from it too soon, so the extra 100 ms sleep should work around that and making sure that the correct window has focus (for a split second no window has focus until the IE window is properly created I think). Also well done for catching my syntax error :wink:

kingzulu
  • Guests
  • Last active:
  • Joined: --
So for a person with limited progamming knowledge (but I know my way around windows fairly well) what to I do with above code? Is it ment to be compiled into a free running program or stuck in some windows system file?

vzx
  • Guests
  • Last active:
  • Joined: --
This is great, but it interferes with Firefox. When I middle-click on a bookmark in the menu, it does not open.

Brad
  • Guests
  • Last active:
  • Joined: --
I'm seeing the same behavior as vzx on a different script that uses the middle-click as a hotkey. Any ideas how to use the middle-click as a prefix key, but still retain the original functionality when clicked?

Sukarn
  • Members
  • 35 posts
  • Last active: Aug 16 2007 06:08 PM
  • Joined: 16 Jun 2007

So for a person with limited progamming knowledge (but I know my way around windows fairly well) what to I do with above code? Is it ment to be compiled into a free running program or stuck in some windows system file?


Download autohotkey from <!-- m -->http://www.autohotke... ... nstall.exe<!-- m -->
Install it.
Copy the code above, paste it in notepad, save it with any name ending with .ahk
Double click the .ahk file you just made and it will launch the program.
You can make it into a .exe by right clicking the .ahk file and compiling it.

EDIT: Oops... Just saw the post date. Seems like I got late in replying. :wink:

rojer_31
  • Members
  • 3 posts
  • Last active: Sep 15 2008 10:08 AM
  • Joined: 28 Sep 2005
Thank you. Was looking for something like this.

Someguy!
  • Guests
  • Last active:
  • Joined: --
The taskbar middle click function does not work on vista. I tried fooling around with the code myself with no luck. Any ideas on how to make it work?

Someguy!
  • Guests
  • Last active:
  • Joined: --
The taskbar middle click function does not work on vista. I tried fooling around with the code myself with no luck. Any ideas on how to make it work?

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
The section you need to look at is "; Close taskbar program click" - it doesn't do anything fancy though, just send a right click and press c to close, so perhaps the shortcut is different in Vista, or, the menu is taking too long to display so increase the sleep time: e.g. "Sleep, 100"

ttrickyy
  • Members
  • 20 posts
  • Last active: Aug 21 2008 03:09 PM
  • Joined: 18 Jun 2008

The taskbar middle click function does not work on vista. I tried fooling around with the code myself with no luck. Any ideas on how to make it work?


I messed around with it for about five minutes and found that if I move the section commented "Close window with titlebar click" under the section titled "Close taskbar program click" it works. I am not sure why, but I really don't mind.
ttrickyy