Jump to content

Using the Same Hotkey in 2 running scripts.


cal
 Share

Recommended Posts

Is this possible?

I have a script that uses the ` key as a universal hotkey. Basicly a Case statement that does different things depending on what is on the screen at the time.

Can I use the same key in a seperate running script? None of the functions overlap and each script works fine if running by itself. Currenly I had to change the key in one of them so they both will run at once.

It used to be all one script but I seperated it out as some of my coworkers are useing part of it. I want to keep them running seperate so If the work one needs adjusting I don't have to go though my much bigger personal one. I can just update the smaller one and send it to them.

Do I need to call the hotkey diferently somehow.

Both Case statements in both scripts unset the hotkey and pass the ` key to windows if none of the case statements match.

I was thinking that script1 should intercept the key, find nothing, pass it to windows where the second script would do the same. But it does not seem to work that way.

Any Ideas?

Ah... Its been just over a year since I discovered Autoit. Long live Autoit.

Link to comment
Share on other sites

  • Moderators

You would be able to use the same 'HotKey' in both scripts, but, it would operate both functions unless you programmed it specifically.

Example:

Script One: -

HotKeySet('{F1}', 'FunctionOne')
Func FunctionOne()
    If $SomethingIsTrueForThisScript Then
        ;'Do Something I want To Do'
    Else
        Send('{F2}')
    EndIf
EndFunc

Second Script:

HotKeySet('{F2}', 'FunctionTwo')
Func FunctionTwo()
    If $SomethingIsTrueForThisScript Then
        ;'Do Something I want To Do'
    Else
        Send('{F1}')
    EndIf
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You would be able to use the same 'HotKey' in both scripts, but, it would operate both functions unless you programmed it specifically.

Very interesting. Basicly your saying that I want to leave them as seperate hotkeys in the 2 scripts but one will invoke the hotkey of the other if it does not match any of its defined stuff. Net result is that the one keypress will get looked at as needed by both scripts. Thats exactly the effect I'm looking for.

But why would I need to do the same in the second script? Would that not have the effect of making both hotkeys able to call either function. Unless I'm missing some of the logic. (ie, F1 and F2 would both have the same net effect in your example.)

I'm thinking the first script in its master case else statement can have the call of the seconds hotkey, and the second at the end if nothing is found can finnally pass the unbound key back to windows if neither has a match.

No. that won't work as I can't unbind the hotkey of script 1 from within script2. Oh well, its not like I need to be able to pass the ` key back to windows if nothing is found in either script. Thats why I picked that key in the first place. Nice location on the keyboard and not used much for anything else. It makes a good choice for a univeral hotkey. I'll use CTRL ` or something in the second script to avoid using more usefull keys.

Hmm, perhapes I can send an ascii code or something to get around the unbinding part.

I'll give it a try on monday when I'm back at work. Thanks, that looks like an idea that will work.

Edited by cal
Link to comment
Share on other sites

  • Moderators

But why would I need to do the same in the second script? Would that not have the effect of making both hotkeys able to call either function. Unless I'm missing some of the logic. (ie, F1 and F2 would both have the same net effect in your example.)

It was just an example, you don't need the 'Else' statement in the 2nd script if you don't want it. It was just so that you didn't think your were limited.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I think it is possible.

Compile these 2 scripts and give it a try. I hope this is what you are looking for.

Script 1:

#include <GUIConstants.au3>

Global $f = 0; remeber active state of script

GUICreate("Script 1") ; will create a dialog box that when displayed is centered
GUISetState (@SW_SHOW)    ; will display an empty dialog box

While(1)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If Not $f And WinActive("Script 1") Then
        HotKeySet('`', 'MyFunction')
        $f = 1
    EndIf
    If $f And Not WinActive("Script 1") Then
        HotKeySet('`')
        $f = 0
    EndIf
Wend

Exit

Func MyFunction()
    MsgBox(0, "S1", "` in Script 1")
EndFunc

Script 2:

#include <GUIConstants.au3>

Global $f = 0; remeber active state of script

GUICreate("Script 2") ; will create a dialog box that when displayed is centered
GUISetState (@SW_SHOW)    ; will display an empty dialog box

While(1)
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    If Not $f And WinActive("Script 2") Then
        HotKeySet('`', 'MyFunction')
        $f = 1
    EndIf
    If $f And Not WinActive("Script 2") Then
        HotKeySet('`')
        $f = 0
    EndIf
Wend

Exit

Func MyFunction()
    MsgBox(0, "S2", "` in Script 2")
EndFunc
Link to comment
Share on other sites

Thanks to both of you. This should work.

Dickb - I need both scripts to check, I'm not 100% on your example since I've never played aournd with GUI stuff yet. But I like this code. I'm going to keep it for later once I start playing around with GUI stuff. I like how it passes the keypress back to windows if neither of the GUI boxes is active.

SmOke_N - I think this is what I need. I'm going to see if I can incorperate it when I'm back at work. I'll report back next week.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...