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 

Toggle (activate/deactivate) groups of HotKeys

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sun Mar 05, 2006 11:47 pm    Post subject: Toggle (activate/deactivate) groups of HotKeys Reply with quote

It was asked several times. Here is a solution based on #IfWinExist. Some hotkey routines will activate groups of other hotkeys via creating/destroying hidden windows.

Below is an example with toggles ScrollLock and Alt-T. The ScrollLock hotkey routine checks the toggle state of ScrollLock. If it is On, create a hidden GUI window, named "ScrLk". If it is Off, destroy this invisible window. (Similarly, you can use CapsLock and NumLock.) Alt-T toggles a variable and creates/destroys another hidden window, named Alt-T. (Other non-toggle keyboard keys can be used similarly.)

These hidden windows only exist when the corresponding toggle is On, so the hotkeys after the corresponding #IfWinExist lines only work if they are turned On. (The first listed hotkey is activated if more than one toggle is On.) After the last #IfWinExist line list the hotkeys, which shall be active when no toggle condition is active.
Code:
DetectHiddenWindows On

GoSub ~ScrollLock ; react to initial state
; ... further code here

; --- end of autoexecute section ---

~ScrollLock::     ; toggle by create/destroy a hidden window
   If GetKeyState("ScrollLock","T")
      Gui 1:Show, Hide, ScrLk
   Else
      Gui 1:Destroy
Return

!T::              ; toggle by create/destroy a hidden window
   AltT := !AltT
   If AltT
      Gui 2:Show, Hide, Alt-T
   Else
      Gui 2:Destroy
Return

#IfWinExist ScrLk ; below the HotKeys are active when CapsLock is ON
!z::MsgBox ScrollLock ON
;...

#IfWinExist Alt-T ; below the HotKeys are active when Alt-T toggle is ON
!z::MsgBox Alt-T ON
;...

#IfWinExist       ; below the HotKeys are active when all toggles are Off
!z::MsgBox ScrollLock/Alt-T Off
;...
Edit 2006.03.07: Added comments, changed GoTo to GoSub

Last edited by Laszlo on Fri Sep 22, 2006 2:54 am; edited 2 times in total
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Tue Mar 07, 2006 4:11 am    Post subject: Reply with quote

It's a novel idea to have a different set of hotkeys active depending on the state of Numlock or ScrollLock. Even better, you've given us an easy framework to do it. Thanks.
Back to top
View user's profile Send private message Send e-mail
Igance
Guest





PostPosted: Tue Mar 07, 2006 4:37 pm    Post subject: Reply with quote

Hi Laszlo. Just a newbie question. In
Laszlo wrote:
Code:
DetectHiddenWindows On
GoTo ~ScrollLock  ; react to initial state

~ScrollLock::     ; toggle by create/destroy a hidden window
;....
;...
, why Goto to a label witch is just below ? What is the utility ? Thanks
Back to top
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Tue Mar 07, 2006 4:52 pm    Post subject: Reply with quote

This is not a label, but a hotkey (callable like a label).
The autoexecute section stops just before this hotkey.
So, to execute this part of the script, you have to go to it explicitely.
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
Ignace
Guest





PostPosted: Tue Mar 07, 2006 5:47 pm    Post subject: Reply with quote

OK PhiLho, thanks a lot for the explanation.
Back to top
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Tue Mar 07, 2006 7:46 pm    Post subject: Reply with quote

I added comments to the original post, to clarify this. If the ScrollLock toggle activation is not the last autoexecute instruction, you have to change GoTo to GoSub, calling directly the hotkey subroutine. It is also less confusing.
Back to top
View user's profile Send private message
genmce



Joined: 10 Jan 2009
Posts: 135
Location: Virginia

PostPosted: Thu Jan 22, 2009 3:49 pm    Post subject: Reply with quote

I have something working... almost....

Code:

;******************* CapsLock and NumLock experiement number 2 ********************
CLstate := GetKeyState("Capslock", "T")
NLstate := GetKeyState("NumLock", "T")

~CapsLock:: CLstate := GetKeyState("Capslock", "T")  ; True if CapsLock is ON, false otherwise.
~NumLock:: NLstate := GetKeyState("NumLock", "T") ; add the NumLock status; CapsLock navigation

; Implement some sort of hotkey trap based on the state of the caps and nums both 0
; in that situation the hotkey is not a hotkey.

$z::
loop
   {
   
      if ((CLstate = 0) & (NLstate = 0)) ; Send normal key function if both off
      {
         Send {~z} ; How do I get this to pass without triggering the hotkey again.
         MsgBox, 4, ,CAPS %CLstate% + NL %NLstate%, 1
         break
      }
      else if ((CLstate = 1) & (NLstate =0)) ; 2 caps only
      {   
         Send {2}
         MsgBox, 4, ,CAPS %CLstate% + NL %NLstate%, 1
         break
      }
   
      else if ((CLstate = 0) & (NLstate = 1)) ; 3 numlock is only 
      {
         Send {3}
         MsgBox, 4, ,CAPS %CLstate% + NL %NLstate%, 1
         break
      }
      else if ((CLstate = 1) & (NLstate = 1)) ; 4 both off
      {   
         Send {4}
         MsgBox, 4, ,CAPS %CLstate% + NL %NLstate%, 1
         return
      }
      
   }
return


I suppose I could make some sort of if then before the hotkey, hmm.....

Please consider and advise.

Thanks!
_________________
KeyMce/GenMce - mackie emulator for pc keyboard/Convert your controller to mackie.
Midi I/O - Want to play with midi/ahk?
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
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