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 

MButton: Close windows(taskbar+titlebar) & open IE links
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
evl



Joined: 24 Aug 2005
Posts: 1233

PostPosted: Sat Mar 04, 2006 12:51 am    Post subject: MButton: Close windows(taskbar+titlebar) & open IE links Reply with quote

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:
http://www.autohotkey.com/forum/viewtopic.php?t=8486 ).
- 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).

Code:


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


Last edited by evl on Tue May 09, 2006 10:32 am; edited 3 times in total
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10667

PostPosted: Sat Mar 04, 2006 2:05 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
jballi



Joined: 01 Oct 2005
Posts: 530
Location: Texas, USA

PostPosted: Mon Mar 06, 2006 7:55 pm    Post subject: Reply with quote

I've always wanted to add some extra functionality to the middle mouse button. Thanks for the code!
Back to top
View user's profile Send private message Send e-mail
evl



Joined: 24 Aug 2005
Posts: 1233

PostPosted: Mon May 08, 2006 6:59 pm    Post subject: Reply with quote

I've made some changes to the script which make use of shimanov's code for the titlebar sensitivity to clicks:
http://www.autohotkey.com/forum/viewtopic.php?t=8486
and generally improved some of the functionality and code so posted an update above.
Back to top
View user's profile Send private message
jballi



Joined: 01 Oct 2005
Posts: 530
Location: Texas, USA

PostPosted: Tue May 09, 2006 3:22 am    Post subject: Reply with quote

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! Smile

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:
Code:
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:

Code:
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...
Back to top
View user's profile Send private message Send e-mail
evl



Joined: 24 Aug 2005
Posts: 1233

PostPosted: Tue May 09, 2006 10:24 am    Post subject: Reply with quote

@ 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 Rolling Eyes Laughing

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
Back to top
View user's profile Send private message
kingzulu
Guest





PostPosted: Sun Sep 10, 2006 7:18 pm    Post subject: Reply with quote

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?
Back to top
vzx
Guest





PostPosted: Wed Feb 21, 2007 2:38 am    Post subject: Reply with quote

This is great, but it interferes with Firefox. When I middle-click on a bookmark in the menu, it does not open.
Back to top
Brad
Guest





PostPosted: Sun Feb 25, 2007 7:23 am    Post subject: Reply with quote

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?
Back to top
Sukarn



Joined: 16 Jun 2007
Posts: 35

PostPosted: Sun Jun 17, 2007 10:59 am    Post subject: Reply with quote

kingzulu wrote:
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 http://www.autohotkey.com/download/AutoHotkeyInstall.exe
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
Back to top
View user's profile Send private message
rojer_31



Joined: 28 Sep 2005
Posts: 3
Location: India

PostPosted: Fri Jul 20, 2007 7:29 am    Post subject: Reply with quote

Thank you. Was looking for something like this.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Someguy!
Guest





PostPosted: Sat May 24, 2008 6:57 pm    Post subject: Reply with quote

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?
Back to top
Someguy!
Guest





PostPosted: Sat May 24, 2008 6:57 pm    Post subject: Reply with quote

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?
Back to top
evl



Joined: 24 Aug 2005
Posts: 1233

PostPosted: Sun May 25, 2008 1:21 am    Post subject: Reply with quote

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"
Back to top
View user's profile Send private message
ttrickyy



Joined: 18 Jun 2008
Posts: 20

PostPosted: Mon Aug 18, 2008 3:27 am    Post subject: Reply with quote

Someguy! wrote:
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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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