4
votes

So I'm trying to write a simple script in AutoHotKey that will use NumLock (which I have mapped to the capslock in my registry) as a toggle to turn my directional keys into the numpad nab keys. My script is as follows:

GetKeyState, state, NumLock, T
if state = D
{
    Up::Numpad8
    Down::Numpad2
    Left::Numpad4
    Right::Numpad6
    Enter::Numpad5
}
if state = U
{
    $Up::Up
    $Down::Down
    $Left::Left
    $Right::Right
    $Enter::Enter
}
Return

However, I get an error saying Up is repeated in line 15. How do I tell AutoHotKey to return my keys to their original key designation? I tried leaving an "else" section blank as opposed to the "if state = U" section, but then the keys remain in their altered state when toggling again. I'm sure there is something simple I am missing.

2
Note, this is about programming. Related: meta.stackexchange.com/questions/187705/… – Benjamin Gruenbaum

2 Answers

3
votes

Ah, here you go. You can't do it the way you are trying to do it. Since you can only map the key once in the script, put the if/or function inside the hotkey, like so:

GetKeyState, state, NumLock, T

up::
    if(state = D){
        send {Numpad8}
    }else{
        send {up}
    }
return
3
votes

This is a simplified solution that uses the #If context.

The advantage of this is that you will not have to have If statements to remap a key back to itself in the Else statement. The key will retain its normal functionality if the condition is not true.

#If GetKeyState("NumLock", "P")
    Up::Numpad8
    Down::Numpad2
    Left::Numpad4
    Right::Numpad6
    Enter::Numpad5
#If