0

[help] #inputlevel but not overwrite key combination ?

i have 2 ahk script ,both remap "k" ,but script 1 has "scrolllock & k"

script1.ahk

k::msgbox script1
scrolllock & k::click

script2.ahk

k::msgbox script2

i want this : when running both script at the same time , if i press k , it will execute the script2.ahk (which show "msgbox script2" ) ,

in other way, script2 overwrite script1

so i put #inputlevel2 in script2.ahk

it now looks like this

script1.ahk

k::msgbox script1
scrolllock & k::click

script2.ahk

#inputlevel2
k::msgbox script2

but now if i press "scrolllock & k" , it will not do the click (function of script1) but it show msgbox (script2) i guess because "k" has been overwrited by script2 and "scrolllock & k" no longer be recognized

i found a way to make scrolllock & k work, when i reload script1 ,and press scrolllock & k ,it will work

but if i reload script2 , i will have to reload script1 again to make that key combination work

In other way, my question is Is it possible to overwrite "k" of script2 to "k" of script1 but still do function like "scrolllock & k" of script1 ?

Thanks

1

Not sure how that works with #InputLevel.

One solution might be to use a common function call from both scripts that is loaded via an #Include statement. It would have to detect if script2 is running and not execute if script2 is running.

Script2 would also have to let the k key pass through to Script1 so scrolllock & k would work in Script1.

Code below is an example, not tested--I'm sure there are more elegant ways to solve this but it might work or get you closer. It's not exactly how I would code it either but is meant to be more of an example idea.

; -----------------------
; Script1.ahk
; -----------------------
#Persistent
k::functionk(A_ScriptName)
scrollock & k::Click
#Include functionCalls.ahk

; -----------------------
; Script2.ahk
; -----------------------
#Persistent
Script2Running("Set", True)      ; auto-execute when script2 starts
OnExit(Script2Exit)
return

~k::functionk(A_ScriptName)      ; tilde should let k pass through to script1

Script2Exit()
{
    Script2Running("Set",False)
}
#Include functionCalls.ahk

; -----------------------
; functionCalls.ahk
; -----------------------
functionk(callersScriptName)
{
    if (callersScriptName="script1.ahk") and not script2Running()
    {
        msgbox script1
        ; add code as needed
    }
    else if (callersScriptName="script2.ahk")
    {
        msgbox script2    
        ; add code as needed
    }
    else
        msgbox Unrecognized script name: %callersScriptName%
}

Script2Running(SetOrRead := "", newState := false)  
{
    static running := false
    if (SetOrRead="Set")
        running := newState

    return running       ; same as 'return %running%', see help topic on return
}
  • Thank you so much for the help, i really appreciate it but ~k in "~k::functionk(A_ScriptName) ; tilde should let k pass through to script1" will send keystroke k when i run on script 2, this is what i don't want ,for example if i want to do ~k::click , it will send k if the current active window is notepad ,word .... i want it to do only "click" (in the example i said) not send "k" Thanks again – nav Oct 21 '15 at 3:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.