Honduriel
Honduriel

Reputation: 117

How to send multiple backticks

Im trying to send multiple backticks with autohotkey, but I can't get it to work consistently. The goal is to automatically create a code block when writing a markdown file. So it should look like this:

```

```

Then it should go up behind the last backtick of the first line so that I can specify the language there. I hope someone can help me with this.

I tried the following:

!^c:: ; <--- Generate code blocks for markdown files
    SendInput, {Raw}`{Raw}`{Raw}`
    SendInput, {enter}
    SendInput, {enter}
    SendInput, {Raw}`{Raw}`{Raw}`
    SendInput, {up}
    SendInput, {up}
Return

Result:

{Raw}{Raw}

{Raw}{Raw}

!^c:: ; <--- Generate code blocks for markdown files
    SendInput, ``````
    SendInput, {enter}
    SendInput, {enter}
    SendInput, ``````
    SendInput, {up}
    SendInput, {up}
Return

Result:

``

``

!^c:: ; <--- Generate code blocks for markdown files
    SendInput, Chr(0x60)Chr(0x60)Chr(0x60)
    SendInput, {enter}
    SendInput, {enter}
    SendInput, Chr(0x60)Chr(0x60)Chr(0x60)
    SendInput, {up}
    SendInput, {up}
Return

Result:

Chr(0x60)Chr(0x60)Chr(0x60)Chr(0x60)Chr(0x60)Chr(0x60)

I'm on Windows 10 with a QWERTZ keyboard set to german language, if that's any help.

Upvotes: 2

Views: 363

Answers (2)

0x464e
0x464e

Reputation: 6489

Good that you found a way, here's some extra info for you:
First, the problem, the backtick key doesn't exist on your keyboard layout, so you get rather unexpected, buggy even, behavior when trying to send it normally.
Switch to e.g. English (US) keyboard layout and you'll see how {`` 3}`n`n{`` 3}{Up 2} works just fine.

And this brings me to another point, there's no need use multiple send commands. All in one line will work just fine, and you can tell a key to repeat by enclosing it in {} and following it up by a number.
But yes, you escape in AHK with a backtick, so this is what it would look like without using the key repeating trick ````````n`n``````{Up}{Up}.
Also I'm using `n for newline here rather than sending enter. I don't have really a strong opinion for why one or the other would be better, so use whichever you prefer I guess.
Maybe you could say that enter is better, so whatever program you're writing in can decide what line endings to use for you (maybe CR LF instead of only LF).

And then about your attempt to try something with {Raw}. To use a different sending mode, you specify it only once at the very start of your string and it takes effect for the whole send command.
Read the documentation for way more and detailed info about all the different send modes than I could bother to type in here.
But in any case, Raw mode wont do anything useful for us here. It only makes the characters ^+!#{} be interpreted literally instead of as modifier keys (^+!#) or as otherwise special keys ({}).

And then about usage of the Chr() function. To run a function, and use its return value on the command, you'll want to enter expression mode the current parameter you're in (the one and only parameter of the send command). To do that, you specify a % as the very first character of the parameter. So your command would look like this:
:SendInput, % Chr(0x60)Chr(0x60)Chr(0x60) "{Enter 2}" Chr(0x60)Chr(0x60)Chr(0x60) "{Up 2}"
However, this is the same as literally sending the backtick key, so we encounter the problem I talked about in the very beginning of this post. Not an ideal solution.


Ok, so now onto the solution to which you had already arrived at, sending it via the unicode value of the key.
So here's your final nice clean one-liner:
!^c::SendInput, {U+0060}{U+0060}{U+0060}{Enter 2}{U+0060}{U+0060}{U+0060}{Up 2}
In a one-liner hotkey you don't even need the code execution ending trailing return, very nice.
But, unfortunately the key repeat functionality wont work in that unicode value block.

Upvotes: 2

Honduriel
Honduriel

Reputation: 117

So, I actually found the answer myself, it works when you use the Unicode representation of the backtick:

!^c:: ; <--- Generate code blocks for markdown files
    SendInput, {U+0060}{U+0060}{U+0060}
    SendInput, {enter}
    SendInput, {enter}
    SendInput, {U+0060}{U+0060}{U+0060}
    SendInput, {up}
    SendInput, {up}
Return

More info can be found here:

Send - Syntax and Usage

List of Unicode characters on Wikipedia

Upvotes: 1

Related Questions