close active window with Hotkey

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

close active window with Hotkey

13 Dec 2014, 09:04

Someone asked me if I had a hotkey for closing the active window. For about 20 years I’ve been hitting Alt+F4 and I realized that this is not very convenient so I wrote the following to close the active window.

Code: Select all

#c:: ; Windows and C closes active window
WinGetTitle, Title, A
PostMessage, 0x112, 0xF060,,, %Title%
return
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
User avatar
Bon
Posts: 17
Joined: 11 Jan 2014, 07:31

Re: close active window with Hotkey

16 Dec 2014, 12:57

Using Win-C is also a bit awkward... There is a more convenient way:
http://www.autohotkey.com/board/topic/8 ... modifiers/
Quidquid Latine dictum sit altum videtur
"Anything said in Latin sounds profound"
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: close active window with Hotkey

19 Dec 2014, 03:05

Retrieving and then using the title introduces a small risk that it will close the wrong window - i.e. if there's a window with the same title or one that includes that string in its title, though it might also have to be higher in the z-order than the active window (which is rare but possible).

In any case, it seems quite unnecessary. Why not just use A?

Code: Select all

#c::PostMessage, 0x112, 0xF060,,, A
Also, I'm not aware of any reason to use PostMessage over WinClose.

Code: Select all

#c::WinClose A
Since you're replacing an existing hotkey, there's another way which is more obvious than PostMessage:

Code: Select all

#c::Send !{F4}
However, Alt+F4 doesn't work on console windows.
User avatar
h0ll0w
Posts: 4
Joined: 29 Jan 2014, 11:55
Location: Colorado
Contact:

Re: close active window with Hotkey

19 Dec 2014, 08:46

Joetazz thanks for this! This stuff has bugged me for years (long keypress combinations).

Bon - the idea for using KeyWait is awesome!

You guys made my day! Cheers! :D
AHK 1.1.26.01 OS Win 7 and 10
User avatar
Joe Glines
Posts: 770
Joined: 30 Sep 2013, 20:49
Location: Dallas
Contact:

Re: close active window with Hotkey

19 Dec 2014, 19:54

I agree I like the code from Bon to hold down a key and have it close the window. Funny how I hadn't thought abuot this even though I'd been using AutoHotKey for about 7 years now.
Sign-up for the 🅰️HK Newsletter

ImageImageImageImage:clap:
AHK Tutorials:Web Scraping | | Webservice APIs | AHK and Excel | Chrome | RegEx | Functions
Training: AHK Webinars Courses on AutoHotkey :ugeek:
YouTube

:thumbup: Quick Access Popup, the powerful Windows folders, apps and documents launcher!
zyfihx

Re: close active window with Hotkey

21 Sep 2018, 06:00

Awesome, thanks ! I use windows on a mac and when I switch over I love the shortcuts to be the same : alt + q = quit and now I have a way to somewhat equal that in windows :) Thanks !!!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 239 guests