Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created January 17, 2023 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/668ea4d2f861c22ff944ac0134d0f4c4 to your computer and use it in GitHub Desktop.
Save bjoerntx/668ea4d2f861c22ff944ac0134d0f4c4 to your computer and use it in GitHub Desktop.
// dynamic character array
var charCodes = [];
// shortcut dictionary
var dict = {
"TY": "Thanks for your request.",
"AQ": "Let me know if you have additional questions.",
"MY": "I look forward to meeting you.",
"GD": "Have a great day.",
"LONG": "This is another longer text.",
};
document.getElementById("txTemplateDesignerContainer").addEventListener("keypress", (event) => {
// if SPACE is pressed
if (event.charCode === 32) {
// convert array to string to get typed word
var typedWord = String.fromCharCode(...charCodes);
// check word in dictionary
if (dict[typedWord] != undefined) {
// cancel SPACE for TXTextControl
event.cancelBubble = true;
// select shortcut and replace with long text
TXTextControl.inputPosition.getTextPosition(function (startPos) {
TXTextControl.select(startPos - typedWord.length, typedWord.length, function () {
TXTextControl.selection.setText(dict[typedWord] + " ");
})
})
}
// reset character array
charCodes = [];
} else {
// add character to array
charCodes.push(event.charCode);
}
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment