input-remapper icon indicating copy to clipboard operation
input-remapper copied to clipboard

Add more user-friendly syntax for generating a series of keystrokes

Open jose1711 opened this issue 3 years ago • 3 comments

While this is possible k(f).k(o).k(o).k(b).k(a).k(r) for longer text I would welcome something like: t(foobar) (t stands for text)

jose1711 avatar Aug 25 '21 06:08 jose1711

yeah, that makes sense.

If you or anyone else wants to contribute:

Here would be the place to add it and it would look quite similar to def keycode(self, symbol): https://github.com/sezanzeb/key-mapper/blob/main/keymapper/injection/macros.py

I would call it text though and not add any short macro names anymore. In the long term the macro editor should arrive and with it longer names for all functions.

Tests for that would go into this file: https://github.com/sezanzeb/key-mapper/blob/main/tests/testcases/test_macros.py, def test_0(self) could be used as a template

sezanzeb avatar Aug 25 '21 08:08 sezanzeb

macros support strings now, sotext("f,oo(") would work. The text macro would somehow have to look up in xmodmap which keys have to be combined to achieve a ( and , (if xmodmap is installed)

I think xmodmap information is available in the system_mapping object, but infos on combinations might be stripped from it.

As a start it would be absolutely enough to support text("abcd1234") without special characters.

sezanzeb avatar Nov 03 '21 09:11 sezanzeb

This would be a great feature. Very hacky, but as a workaround this could simply be done by parsing input text into the macro language. Something along these lines. Works reasonably well to create some simple latex macros

text = r"""
\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{.pdf}
    \caption{{\bf TODO} TODO}
    \label{fig:}
\end{figure}
"""

key_dict = {
 "[": "k(bracketleft)",
 "]": "k(bracketright)",
 "(": "k(parenleft)",
 ")": "k(parenright)",
 "{": "m(Shift_L, k(bracketleft))",
 "}": "m(Shift_L, k(bracketright))",
 "\\": "k(backslash)",
 "=": "k(equal)",
 "-": "k(minus)",
 "+": "m(Shift_L, k(equal))",
 "\n": "k(Return)",
 " ": "k(space)",
 "\t": "r(4, key(space))",
 ".": "k(period)",
 ":": "m(Shift_L, k(semicolon))",
 "\x08": "k(BackSpace)",
}

def text2macro(text, key_dict):
    macro = ""
    for c in text:
        try:
            if c.isalnum():
                if c.isupper():
                    macro += f"m(Shift_L, k({c}))"
                else:
                    macro += f"k({c})"
            else:
                # translate character to input-remapper symbol
                macro += key_dict[c]
        except KeyError:
            print(f"symbol for {c} not in key dict")
        # chain macro together
        macro += "."
    return macro


print(text2macro(text, key_dict))
# k(Return).k(backslash).k(b).k(e).k(g).k(i).k(n).m(Shift_L, k(bracketleft)).k(f).k(i).k(g).k(u).k(r).k(e).m(Shift_L, k(bracketright)).k(bracketleft).k(h).k(bracketright) ....

W-L avatar Feb 02 '22 15:02 W-L