 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Tue Nov 20, 2007 3:56 pm Post subject: anyway round this autohotkey limitation? |
|
|
There is a limitation in autohotkey that a script will end its execution once it meets a return statement or a Hotkey definition.
In my script I want to be able to set hotkeys
| Code: | $h:: Home
$j:: Left
$k:: Down |
and have them initially disabled and when you press the capslock key, these hotkeys will be active. When you press the capslock key again these hotkeys will be disabled etc. (the idea is that pressing the capslock key would change the editing mode in a similar way to vim)
There is a workraround for this, that I would have the following code:
| Code: | | Hotkey, h, HomeLabel |
and then I would have the definition
| Code: | HomeLabel:
send, {blind}{home}
return |
and then set the hotkey disabled
The above approach is a little ineligent for my liking. I don't like using a label for something which should be defined inline.
Thanks. |
|
| Back to top |
|
 |
Razlin
Joined: 05 Nov 2007 Posts: 434 Location: canada
|
Posted: Tue Nov 20, 2007 4:04 pm Post subject: keep alive |
|
|
I think what you want is
|
|
| Back to top |
|
 |
BoBoĻ Guest
|
Posted: Tue Nov 20, 2007 4:14 pm Post subject: |
|
|
| Code: | ~Capslock::
KeyWait, Capslock ; Wait for user to physically release it.
Suspend, Toggle
Return | ? |
|
| Back to top |
|
 |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Tue Nov 20, 2007 4:22 pm Post subject: Re: keep alive |
|
|
| Razlin wrote: | I think what you want is
|
Well, I tried that:
| Code: | #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#Persistent
$j:: Left
msgbox, hello |
but it did not work (hello does not pop up) . I am thinking of having two scripts running side by side which are invoked by a bat file.
Is there any way to sharing information (variable values) between scripts?
Thanks |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
Posted: Tue Nov 20, 2007 4:40 pm Post subject: |
|
|
having something on the same line as a hotkey precludes running the lines below it.
#Persistent won't help you
a hotkey definition is also a label, and can be turned on and off via the hotkey command
your current script does not need the $ modifier, I should think.
variables can be shared via the IPC module by majkinetor. _________________
Unless otherwise noted, all code is untested.
Common Answers: 1.(Loops, Viruses, etc.) 2. Search 3.RTFM |
|
| Back to top |
|
 |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Tue Nov 20, 2007 6:48 pm Post subject: |
|
|
Could you please provide a link to this IPC module please.
I cannot find it through the forum search or google for that matter
gratz
PS. there is
which I will use to keep the hotkeys in a suspended state when the app starts up. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 6847 Location: Pacific Northwest, US
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4078 Location: Pittsburgh
|
Posted: Tue Nov 20, 2007 7:04 pm Post subject: |
|
|
You probably want to disable the original function of CapsLock, when you use it for something else, but to preserve its function, create another hotkey, like Ctrl-CapsLock.
Enabling/disabling hotkeys were discussed several times. One solution was that CapsLock creates a hidden window, and then destroys it. You can precede your hotkeys with #IfWinExist directive. | Code: | DetectHiddenWindows On
^CapsLock:: ; Control-Capslock = Toggle CapsLock
GetKeyState t, CapsLock, T
IfEqual t,D, SetCapslockState AlwaysOff
Else SetCapslockState AlwaysOn
Return
CapsLock:: ; toggle hotkeys by create/destroy a hidden window
IfWinExist CapsLock
Gui 9:Destroy
Else
Gui 9:Show, Hide, CapsLock
Return
#IfWinExist CapsLock ; below the HotKeys active when CapsLock is pressed an odd number of times
h::Home
j::Left
k::Down |
|
|
| Back to top |
|
 |
Razlin
Joined: 05 Nov 2007 Posts: 434 Location: canada
|
Posted: Tue Nov 20, 2007 8:23 pm Post subject: Re: keep alive |
|
|
| Razlin wrote: | I think what you want is
|
#Persistent was related to your very first line
| Quote: | | There is a limitation in autohotkey that a script will end its execution once it meets a return statement or a Hotkey definition. |
#Persistent will make it so it doesnt "quit"
but yes I did not continue reading your entire post and do appologize
at work right now.. trying to help out a bit but maybe I shouldnt if I cant read all
edit:
as per your suspend option if I understand correctly
| Code: |
Activator := TRUE
RAlt::
Activator := !Activator ;toggle state
if Activator
{
;do something
}
Return
|
I use that for a script I wrote..
maybe it will help.. |
|
| Back to top |
|
 |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Tue Nov 20, 2007 10:14 pm Post subject: |
|
|
I have finally implemented it
here it is:
| Code: |
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force
; First set up the so called undefined keys which will do nothing in the navigation mode.
; (I will have to find a use for these keys).
undefinedKeys:= "qtyuopasgzxcvbnm,"
loop {
if undefinedKeys =
break
StringLeft, k, undefinedKeys, 1
StringTrimLeft, undefinedKeys, undefinedKeys, 1
HotKey, $%k%, DO_NOTHING
}
; If the scroll lock is already on then we "switch on" the navigation mode by un-suspending
; the hotkeys
GetKeyState, state, ScrollLock, T
IfEqual state, D, Suspend, Off
else Suspend, On
; Set the indicator to reflect the mode
Menu, Tray, Icon, %A_IsSuspended%.ico, 1, 1
return
DO_NOTHING:
return
Capslock:: ; if caps lock is pressed we change the editing mode and we use the scroll lock indicator
Suspend
Send, {Blind}{ScrollLock}
Menu, Tray, Icon, %A_IsSuspended%.ico, 1, 1
return
*ScrollLock:: ; scroll lock acts as if it is caps lock
SUspend
Send, {Blind}{CapsLock}
Suspend
return
; moded editing
$i:: Up
$h:: Home
$j:: Left
$k:: Down
$l:: Right
$`;:: End
$w:: ^x
$e:: ^c
$r:: ^v
$d:: LShift
$f:: LControl
; modeless editing
CapsLock & d:: Send, {Delete}
CapsLock & e:: Send, {Enter}
|
I did not need interprocess communication or an invisible window -- I just used the scroll lock status.
The ico files:
http://t.a.hassan.googlepages.com/home
I will have to wait and see whether this is of any use but moded editing seems to be all the rage now and it is probably better to come up with your own way of editing rather than use somebody elses.
Thanks for all your help  |
|
| Back to top |
|
 |
Lexikos
Joined: 17 Oct 2006 Posts: 2739 Location: Australia, Qld
|
Posted: Wed Nov 21, 2007 1:51 am Post subject: |
|
|
Tip: You can simplify | Code: | undefinedKeys:= "qtyuopasgzxcvbnm,"
loop {
if undefinedKeys =
break
StringLeft, k, undefinedKeys, 1
StringTrimLeft, undefinedKeys, undefinedKeys, 1
HotKey, $%k%, DO_NOTHING
} | by replacing it with a parsing loop: | Code: | undefinedKeys:= "qtyuopasgzxcvbnm,"
Loop, Parse, undefinedKeys
HotKey, $%A_LoopField%, DO_NOTHING |
 |
|
| Back to top |
|
 |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Wed Nov 21, 2007 12:37 pm Post subject: |
|
|
wow, that cuts 7 lines down to 2.
In fact its good that you can read my code and suggest improvements. Before my code was about 200 lines which is ridiculous for what I wanted it to do. |
|
| Back to top |
|
 |
tazmanian
Joined: 17 Nov 2007 Posts: 16
|
Posted: Sat Nov 24, 2007 7:42 pm Post subject: |
|
|
Thanks Laszlo,
I ended up adopting your technique or creating / destroying a hidden window. The problem with my approach with suspending the script is that other keys should still work even when the script is suspended.
I could of course but "Suspend" as the first and last line but this gets incredibly tedious and produces unreadable code.
Here is better, more extensible code:
| Code: |
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force
INITIALISATION:
DetectHiddenWindows On
SetCapsLockState AlwaysOff ; Set capslock to alwaysoff to stop it blinking each time it is pressed
Menu, Tray, Icon, 1.ico ; set the icon to show that we are in typing mode
; when in navigation mode prevent undefined keys to be used for typing
undefinedKeysNavMode := "qtyuopasgzxcvbnm,"
Loop, Parse, undefinedKeysNavMode
HotKey, %A_LoopField%, DoNothing, Off
; prevent capslock + <undefinedkey> from doing something
undefinedKeysModifierPress := "qwrtyuopasfgzxcvbnm,"
Loop, Parse, undefinedKeysNavMode
HotKey, CapsLock & %A_LoopField%, DoNothing, Off
return ; INITIALISATION
; label to make a key do nothing
DoNothing:
return
; Navigation keys which require the holding of capsLock
CapsLock & j:: Send, {Blind}{Left}
CapsLock & k:: Send, {Blind}{Down}
CapsLock & l:: Send, {Blind}{Right}
CapsLock & `;:: Send, {Blind}{End}
CapsLock & i:: Send, {Blind}{Up}
CapsLock & h:: Send, {Blind}{Home}
; Enter and delete which require the holding of CapsLock
CapsLock & d:: Send, {Blind}{Delete}
CapsLock & e:: Send, {Blind}{Enter}
^CapsLock:: ; control + capslock to toggle capslock. alwaysoff/on so that the key does not blink
GetKeyState t, CapsLock, T
IfEqual t,D, SetCapslockState AlwaysOff
Else SetCapslockState AlwaysOn
Return
CapsLock:: ; toggle hotkeys by create/destroy a hidden window
IfWinExist CapsLock
{
Gui 9:Destroy
Loop, Parse, undefinedKeysNavMode
HotKey, %A_LoopField%, Off
Menu, Tray, Icon, 1.ico
}
else
{
Gui 9:Show, Hide, CapsLock
Loop, Parse, undefinedKeysNavMode
HotKey, %A_LoopField%, On
Menu, Tray, Icon, 0.ico
}
Return
#IfWinExist CapsLock ; below the HotKeys active when CapsLock is pressed an odd number of times
; The arrow keys
h:: Home
j:: Left
k:: Down
l:: Right
`;:: End
i:: Up
; cut copy and paste
w:: ^x
e:: ^c
r:: ^v
; shift and control keys
$d:: LShift
$f:: LControl
#IfWinExist
|
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|