Trigger « if last char is whitespace and » if not

Viewed 71

I would like to print either « or » whenever I press Shift+Numpad7.

I know how to set up two different scripts: e.g. +Numpad7::« and +Numpad8::», but is there a way to have the output depend on whether the character before the caret is whitespace or not?

I.e., so I could type something like «Hello», where both times I pressed Shift+Numpad7

1 Answers

Shifting numpad numbers can have unintended overrides (like on my system) so I used Ctrl Numpad7, but YMMV:

^Numpad7::
    oldClip := Clipboard ; save current clipboard
    Send, +{Left}^c{right} ; select char to the left and copy to clipboard
    ClipWait
    lchar := Clipboard = " " ? "«" : "»" ; check if space or not
    Send, %lchar% ; send lchar
    clipboard := oldClip ; restore original clipboard
    ClipWait
return

Btw, since unicode and ansi vers of AHK handle that character differently, on some systems you may get an extra preceding character in the output. Just change the last Send to:

    Send, % SubStr(lchar, 0) ; send last character of lchar

And let us know what version worked for you.

Related