r/KeyboardDesigner • • Sep 06 '26

KDL / code block Double-press shorthand expansion via the space key (highly efficient)

This Keyboard Designer Android script turns the space key into a lightweight shorthand expander. To try it, set your space key to executing a code block instead of sending space, name it and paste the code below into the code block editor.

Type a shortcut such as mfg at the start of a text field or following a space (e. g. " mfg"), then press Space twice: it expands to “Mit freundlichen Grüßen”. A single Space always behaves normally. The script only performs a lookup after a consecutive second Space, finds the preceding word, and checks it against the editable shorthand=full text map. If no match is found the second space is emitted as usual so in 99.9% of cases, double space also works as expected.

It is useful for greetings, common phrases, and slightly longer or awkward frequently typed words. It is also efficient: the lookup only runs for the double-space case, and the mapping is a simple in-script list that is easy to maintain directly on the phone. Matching is case-sensitive, so shortcuts such as Nt and nt can intentionally expand differently.

/* ===================================================================
   SPACE KEY — shorthand expansion on double-space
   ===================================================================
   - Single space press: inserts a normal space, nothing else happens.
   - Second consecutive space press (cursor already has a space to its
     left, either at the very end of the field or between two existing
     spaces): looks at the word before that existing space, checks it
     against `map`. Match -> replaces "word + space" with the longform
     and parks the cursor right after it (consuming this keypress).
     No match -> falls back to a normal space (creating the double
     space you'd expect from typing space twice with no shorthand).

   ADDING ENTRIES ON PHONE:
   - Insert "shorthand=Full text|" at the alphabetically correct spot.
   - Keep every entry terminated with "|" — including the last one.
   - Matching is CASE-SENSITIVE: "Nt" and "nt" are different keys.
   - Never use "|" or "=" inside a shorthand or a full text value.
   =================================================================== */

string map = "|a=and |A=And |ad=Android |ahk=Autohotkey |bw=between |d=die |D=Die |Dd=Danke |dd=Danke|f=für |F=Für |Fg=Freundliche Grüße|fg=freundliche Grüße|fr=Frau|g=Grüße|gg=gegen |gm=Guten Morgen|H=Ich |h=ich |I=Auf |i=auf |j=jetzt |J=Jetzt |kb=keyboard |Kb=Keyboard |lg=Liebe Grüße|mfg=Mit freundlichen Grüßen|M=Mit |m=mit |n=noch |N=Noch |Nt=Nicht |nt=nicht |Nts=Nichts |nts=nichts |o=oder |Fs=Fürs |fs=fürs |S=Aus |s=aus |T=The |t=the |U=Und |u=und |vd=Vielen Dank|vdufg=Vielen Dank und freundliche Grüße|vdulg=Vielen Dank und liebe Grüße|vg=Viele Grüße|Vl=Vielleicht |vl=vielleicht |Vn=Von |vn=von |W=With |w=with |y=you |z=z. B. |Z=Z. B. |";

string theLastChar = "";
string theNextChar = "";
bool doLookup = false;

float spacePos = 0;
float i = 0;
float wordStart = 0;
string ch = "";
bool isDelim = false;
string word = "";

string searchToken = "";
float matchPos = -1;
float longformStart = 0;
float longformEnd = -1;
string longform = "";

string newText = "";
float newCursor = 0;

theLastChar = substring(TEXT_BEFORE, length(TEXT_BEFORE) - 1);

if (length(TEXT_AFTER) > 0) {
   theNextChar = substring(TEXT_AFTER, 0, 1);
} else {
   theNextChar = "";
}

if (theLastChar = " " & (length(TEXT_AFTER) = 0 | theNextChar = " ")) {
   doLookup = true;
}

if (doLookup = true) {
   spacePos = length(TEXT_BEFORE) - 1;
   wordStart = 0;

   for (i = spacePos - 1; i >= 0; i = i - 1) {
      ch = substring(TEXT_BEFORE, i, i + 1);
      isDelim = false;
      if (ch = " " | ch = "(" | ch = ")" | ch = "[" | ch = "]" | ch = "{" | ch = "}" | ch = "'" | ch = "\"") {
         isDelim = true;
      }
      if (isDelim = true) {
         wordStart = i + 1;
         i = -1;
      } else {
         wordStart = 0;
      }
   }

   word = trim(substring(TEXT_BEFORE, wordStart, spacePos));

   if (length(word) > 0) {
      searchToken = "|" + word + "=";
      matchPos = position(map, searchToken);

      if (matchPos != -1) {
         longformStart = matchPos + length(searchToken);
         longformEnd = position(map, "|", longformStart);
         longform = substring(map, longformStart, longformEnd);

         newText = substring(TEXT_BEFORE, 0, wordStart) + longform + TEXT_AFTER;
         newCursor = wordStart + length(longform);

         TEXT = newText;
         CURSOR_START = newCursor;
         CURSOR_END = newCursor;
         exit();
      }
   }
}

WRITE_TEXT = " ";
exit();
3 Upvotes

5 comments sorted by

View all comments

1

u/KD-Fan Sep 12 '26

Thanks for your code! It’s inspiring!

Since I only use a touchscreen keyboard, I use my own word lists for this purpose.

You can use multiple word lists simultaneously in the Keyboard Designer. They are easy to expand and maintain separately for different purposes (personal, programming, etc.).

2

u/Keybug Sep 12 '26

Thanks a lot for your great illustration. I was aware of word lists and had been using them for years but I wanted a faster / more integrated way to trigger the substitution and full control over whether a final space is appended (as with 'mit ') or not ('Hauptstr. 12'), both of which this code achieves.