0
votes

I'm trying to send a non-break space ( ) through Space key only when CapsLock is On, ootherwise send the regular space key.

I've tried a bunch of different variations of the following but none of them exactly work right.

$~Space::
    GetKeyState, state, CapsLock, T
    if state = D
        send, { } ; non-break space
    else
        send, { } ; regular space
    Return

It seems to either always send the regular space ($~, or only ~) or not send it at all (only $), or not work at all (neither $ or ~).

Here's a list different variations of $/~ literals I've use with respective results:

For convenience, I'll pretend I want to use the character n instead of non-break space.

Keeping the above code same except the first line, on pressing the Space key:

  • $~Space::
    • caps lock off: Sends <space> (good)
    • caps lock on: Sends <space>n (why the extra space?)
  • $Space::
    • caps lock off: Sends nothing!
    • caps lock on: Sends n (good)
  • ~Space::
    • caps lock off: Sends <space> (good)
    • caps lock on: Sends <space>n (why the extra space?)
  • Space::
    • caps lock off: Sends nothing!
    • caps lock on: Sends n (good)

What's the correct way to make this work?

2

2 Answers

1
votes

if using ahk 1.1+ from http://ahkscript.org

try this:

#if GetKeyState("CapsLock", "T")
$space::send n
return

Hope it helps

0
votes

Try this code:


Version 1:

$~Space::

    if (GetKeyState("CapsLock", "T") = 1)
    {
       send, { } ; non-break space 
    }
    else
    {
       send, { } ; regular space
    }

return




Version 2:

$~Space::

    if (GetKeyState("CapsLock", "T") = 1)
    {
       Send, {Backspace}
       send, { } ; non-break space 
    }
    else
    {
       send, { } ; regular space
    }

return




Capslock state checking works fine in my code. I dont know what is different between regular and nonregular spaces (I coped Send commands from your code "as is"), so please check if correct space is send.


Advices:

  1. Use GetKeyState built in function instead of command.

  2. Always put if statement condition in () .

  3. Always use include if statement body in {} .

  4. Always use AutoHotkey and its documenatation from http://ahkscript.org/ (current uptodate version, new official website)! AutoHotkey and its documentation from autohotkey.com is outdated and you may have some problems using them!