1
votes

I have a script that is supposed to emulate the Colemak keyboard layout. I want to be able to toggle the layout with Win+Space

Here's the script:

colemak = true

#If %colemak% = true
hotkeys...
#If

#Space::ChangeLayout()

ChangeLayout()
{
    colemak := !colemak
    if (colemak = false) {
        MsgBox Colemak deactivated.
    } else {
        MsgBox Colemak activated.
    }
}

When I press Win+Space, the colemak variable doesn't change, it stays at true. What am I doing wrong here?

Also, I want to have some kind of feedback that tells the user that the layout changed. MsgBox isn't really the thing I am looking for because it requires the user to press ok. I want a popup or something similar that just tells the user that it changed without any interaction required or possible. What are some possibilities?

1

1 Answers

2
votes

Two things:

  1. In order to declare a variable, you need to use the := operator. So instead of initially declaring colemak = true, you need colemak := true.

  2. The function ChangeLayout() has its own scope in which the initial declaration of the global variable colemak is not visible. In order to fix this, you need to make colemak a "super global" variable by adding global in front of it.

Final code:

global colemak := true

#If %colemak% = true
hotkeys...
#If

#Space::ChangeLayout()

ChangeLayout()
{
    colemak := !colemak
    if (colemak = false) {
        MsgBox Colemak deactivated.
    } else {
        MsgBox Colemak activated.
    }
}

Source