Jump to content

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

Dynamic hotkey for open windows


  • Please log in to reply
14 replies to this topic
Vardhan
  • Members
  • 2 posts
  • Last active: Aug 22 2005 11:21 AM
  • Joined: 22 Aug 2005
I disovered ahk a couple of hours back, and wrote this to do what
I always wanted to do ... Dynamically give hotkeys to open windows,
to switch quickly from one to other.

With this you can:
1. Use Win+Ctrl+0..9 to attach hotkey to current active window.
2. Use Win+0..9 to switch to corrospoding window.


--enjoy and improvise.

;;;;; Dynamic hot key to windows ....
;;;;; To see current assignment, see values of variable var0-9
;;;Win-Control-n --> Assign window to this hot key
;;;Win-n         --> Switch to window with this hot key
;;;Win-Alt-n     --> Iconize others and do this


DynHotkey( ByRef var , what)
{
  if ( what == 0 ) ;; Save
  {
      WinGetActiveStats, var, w,h,x,y
      return
  }
  if ( what == 1 ) ;; Switch
  {
       WinActivate, %var%
       return
  }
  if ( what == 2 ) ;; IconizeAll, Switch
  {
       WinMinimizeAll
       WinActivate, %var%
       WinRestore, %var%
       return
  }

}

#^0::DynHotKey(var0,0)
#0::DynHotKey(var0,1)
#!0::DynHotKey(var0,2)


#^1::DynHotKey(var1,0)
#1::DynHotKey(var1,1)
#!1::DynHotKey(var1,2)

#^2::DynHotKey(var2,0)
#2::DynHotKey(var2,1)
#!2::DynHotKey(var2,2)

#^3::DynHotKey(var3,0)
#3::DynHotKey(var3,1)
#!3::DynHotKey(var3,2)

#^4::DynHotKey(var4,0)
#4::DynHotKey(var4,1)
#!4::DynHotKey(var4,2)

#^5::DynHotKey(var5,0)
#5::DynHotKey(var5,1)
#!5::DynHotKey(var5,2)

#^6::DynHotKey(var6,0)
#6::DynHotKey(var6,1)
#!6::DynHotKey(var6,2)

#^7::DynHotKey(var7,0)
#7::DynHotKey(var7,1)
#!7::DynHotKey(var7,2)

#^8::DynHotKey(var8,0)
#8::DynHotKey(var8,1)
#!8::DynHotKey(var8,2)

#^9::DynHotKey(var9,0)
#9::DynHotKey(var9,1)
#!9::DynHotKey(var9,2)


Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Nice idea! And it works.
If you like experimenting with a working script, you can shorten it a little by using the hotkey command and a loop. Also, it is safer to use the unique ID of a window, instead of its title (in case there are conflicting titles), and you don't have to worry about the title matching modes.
Loop 10
{
   i := A_Index - 1
   HotKey #^%i%,DynHotkey
   HotKey #%i%, DynHotkey
   HotKey #!%i%,DynHotkey
}
Exit

DynHotkey:
   StringRight i, A_ThisHotKey, 1
   StringMid what,A_ThisHotKey, 2, 1
   var := var%i%
   IfEqual what, ^, WinGet var%i%, ID, A  ; Save ID
   Else IfEqual what,!, WinMinimizeAll    ; MinimizeAll
   WinRestore  ahk_id %var%
   WinActivate ahk_id %var%               ; Switch
Return
Edit 2005.08.23: trimmed off another couple of lines

Vardhan
  • Members
  • 2 posts
  • Last active: Aug 22 2005 11:21 AM
  • Joined: 22 Aug 2005
Hello,
Thanx for shortening it.

Can you pls explain why the 'A' in 'WinGet var, ID, A' should get the
active window ? I noticed that documenation refers to WinExist("A")
to get active window, but what is so special about "A" ?

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Please have a look at the manual for "last found window". There you'll find beside other usefull information this:

Almost all of the windowing commands can be told to operate upon the active window by specifying the letter A as the WinTitle parameter and omitting WinText, ExcludeTitle, and ExcludeText.


Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

freakkk
  • Members
  • 182 posts
  • Last active: Dec 16 2014 06:23 PM
  • Joined: 29 Jul 2005
You might want to check out this script i posted a while ago:

http://www.autohotke...opic.php?t=4721

It makes Win 1-9 & 0 (on top of keyboard & on side numeric pad) to be hotkeys that perform a variety of functions. I also have built in shortcuts for capturing info when the gui window is minimized to system tray (like your active window script..)

Left Ctrl+Left Alt+<1-10> = Sets that number hotkey to either activate, minimize, or maximize the currently active window (you can specify default action on the settings tab...)

Right Alt+<1-10> = Sets hotkey to either left click, right click, or simply move to where you currently have mouse.

win+Left shift+<1-10> = If you have text highlighted-- it will copy it.. & set specified hotkey to send those keystrokes (think of it as an extension to clipboard..)

win+numeric pad minus key = Takes a screenshot of either the whole screen- or the currently active window.. & saves as a JPG in folder you are running script from.

Alt+Left click/drag = Lets you draw a box around portion of screen to take screen shot of. Once released- JPG will be saved in folder you are running script from.

I also have other functions on there like a built in editor to help you arrange more complex multiple step macros, a simple hotstring editor, an autocomplete feature (you specify which windows to work for..), and an option to export your hotkey macros that you've created to an AHK file (or to an EXE file if you put a copy of the Ahk2Exe.exe, AutoHotkeySC.bin, & upx.exe in the same folder your running script from..). I'm still working on it-- so I'm currently working on a new & improved version. Let me know if there are any new features you want to see added. :D

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
Freakkk, your SW is the other extreme: very powerful but large and complex. Here you can see how much you can achieve with only 17 lines of AHK script. If you need something like this, you can put it together in 10 minutes, and it serves the purpose. If you keep adding features you will loose the overview, most of them you don't even remember and never use. Finding the balance is hard.

freakkk
  • Members
  • 182 posts
  • Last active: Dec 16 2014 06:23 PM
  • Joined: 29 Jul 2005
Thanks (..I think). Actually when I started the script- it was simply to harness some of the basic time saving features of AHK (like sending repetitive keystrokes, mouse movement, running progs/browsing to different web pages..) into a standalone EXE that i could take to work with me and save keystrokes (RSI's a b**ch! :D ). We don't currently have AHK installed at work- & I wouldn't really have time to sit there and code a little 'quickie' script to make it worth it if we did.

Then I found that I was using it quite a bit at home on projects--- setting a hotkey to repetively indent my loops, copying txt from one screen to another, or basically anything that came up where I found myself doing anything repetitive.

Soon after AHK introduced gui features- I realized how much easier it would be to have a nice little window for setting up scripts.. and on the path to creating that- I just kind of added other scripts that I already created (.. its sort of like my utility belt :lol: )

Those shortcut hotkeys I listed above are not necessary to utilize this script.. they are just some of the minor features I have added. Since there is a gui to interface with- its possible to mouse your way through creating simple macros (in other words-- there is no need to remember all those hotkeys I mentioned..). I just thought I'de share.
Let me know if there is something specific that you think I should improve or simplify


--- Thanks for the feedback Laszlo! :D

Sixty-Six
  • Members
  • 19 posts
  • Last active: Mar 16 2006 08:33 PM
  • Joined: 06 Aug 2005
How about making a gui based shortcut key manager in AHK?

I'd like to be able to easily see in one place all of my custom shortcut keys and what they do. Easily be able to set a shortcut key to run a windows shortcut, or program, or web page, or something else.

In fact, it'd be absolutely lovely if we could replace the keys that are never used like the Scroll-Lock key and use it for "Shortcut Lock"ing ...in other words, when its locked (or on) it would enable all of the shortcut keys you've programmed, and can replace any key on the keyboard (other than the shortcut lock key(scroll lock-or whatever)), and when its off it won't let the shortcut keys work so that you can use regular key assignments.

Maybe this is a job for --- Myself.

?

Yeah, so how about it?

polyethene
  • Members
  • 5519 posts
  • Last active: May 17 2015 06:39 AM
  • Joined: 26 Oct 2012

How about making a gui based shortcut key manager in AHK?

Take a look at this thread.

autohotkey.com/net Site Manager

 

Contact me by email (polyethene at autohotkey.net) or message tidbit


Sixty-Six
  • Members
  • 19 posts
  • Last active: Mar 16 2006 08:33 PM
  • Joined: 06 Aug 2005
Well, that's quite interesting. I'm using it now and I like it, but it disabled my Autohotkey hot keys just like the post said. That's annoying. Is there any way to make an AHK force its hot keys to work?

Or, better yet, if somebody designed a version of this in AHK...

It'd be nice if there was a LIST view for the assigned hot keys too.

I'll look into doing it myself in my spare time because I think this could be really useful for a lot of people.

Jason

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
You can use the AHK command ListHotkeys. If you want to process the info, you can select it, copy to the clipboard and put the list into your own GUI.

dv
  • Guests
  • Last active:
  • Joined: --
This does not seem to to anything for me on Win XP.
What is this hotkey supposed to do? (I tried both original and the shortened version)
;;;Win-Alt-n --> Iconize others and do this

Also a suggestion
--------------------
Please add a key combo to automatically set Win+(1-9) to the same order of windows visible in the taskbar from left to right. That would be amazing!

thanks!

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

Please add a key combo to automatically set Win+(1-9) to the same order of windows visible in the taskbar from left to right.

Try ActivateByNum - Quick task switching with Win+[1,2,...,0]
http://www.autohotke...pic.php?t=41542

dv
  • Members
  • 23 posts
  • Last active: Oct 25 2010 12:59 PM
  • Joined: 05 Jun 2009
thanks that works great!

Guest040712
  • Guests
  • Last active:
  • Joined: --
The script in the title works really well for me. The shortened one by Laszlo doesn't work as well - it changes window sizes when restoring windows and it doesn't seem to allow reassigning shortcuts. Is there any reason to try to get Laszlo's to work, other than the concern about conflicting window titles?