2

I wanna disable a key globally, say it, space,
but enable in a specific window, eg, Notepad.

Then I put below lines in key.ahk and load it

space::return

#IfWinActive ahk_class Notepad
    space::send {space}
#IfWinActive

But below error occurs when I type space in Notepad:

71 hotkeys have been received in last 1109 ms.
Do you want to continue?
(see #MaxHotkeysPerInterval in the help file)

seems like result in infinite recursion.

If I change

    space::send {space}

to

    space::space

Then nothing happens when space is typed in Notepad.

The question is, how can I disable space globally, but enable it in Notepad?

== EDIT ==

I do want to disable the key globally, or more usefully, remap to another key-sequence.

2

You don't need to worry about what to do when Notepad is not active. If you only have a portion that deals with what happens when Notepad is active, then the non-active times will just do nothing by default.

This code will disable Space in everything except Notepad, as described in your question:

space::
    if WinActive("ahk_class Notepad") 
    {
        send {Asc 32}
        return
    }

The send {Asc 32} line sends the ASCII code for a space, which prevents the script from catching it's own sent space and starting over.

  • Thx for your answer. But I do want to disable the key globally, or more usefully, remap to another key-sequence. – qeatzy Oct 26 '17 at 2:48
  • @qeatzy My code does disable the space key globally, unless Notepad is the active window. If you want another key sequence globally, just follow this with an else statement containing those commands. – techturtle Oct 26 '17 at 2:51
  • Thx, just tried it, but strangely a <kbd>Return</kbd> is sent instead of <kbd>Space</kbd>. – qeatzy Oct 26 '17 at 2:59
  • correction: it send space, but followed by return. – qeatzy Oct 26 '17 at 3:02
  • Thx, fixed by adding return command at the last. – qeatzy Oct 26 '17 at 3:06
1

The code for the accepted answer (as it is listed at the time of this writing) has a return inside of the if statement...

space::
    if WinActive("ahk_class Notepad") 
    {
        send {Asc 32}
        return
    }

Best practice would be to put the return outside of the if statement (place a return at the end of any hotkey code), so it will always return, so if this code block is used in another file and/or with other code, execution won't continue past the routine that handles the space:: hotkey.

Currently, any subsequent code will still execute if space is pressed on a-non notepad window.

The slight modification to correct this would be as follows:

space::
    if WinActive("ahk_class Notepad") 
    {
        send {Asc 32}
    }
return

If the return statement is outside of the if statement, you can also get rid of the brackets for the single send line following the if statement:

space::
    if WinActive("ahk_class Notepad") 
        send {Asc 32}
return

One more slight variation on this would be to simply return if Notepad is not active. This can be useful in cases where there is a bunch of following code (instead of just a single send statement), basically because it can be easier to read and understand...

space::
    if !WinActive("ahk_class Notepad") 
        return   ; execution will end here unless Notepad is active

    send {Asc 32}

    ...

    ...

    ; lots of other code can be added here 
    ; without encompassing it in an if {} block

    ...

    ...

return   ; make sure nothing following this will execute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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