Products Technologies Demo Docs Blog Support Company

Implementing Text-As-You-Type Auto Correct Replacements using JavaScript

Text-As-You-Type Auto Correct is a popular feature that replaces a specific phrase or character combination with a longer text while typing. This sample shows how to implement this feature using JavaScript.

Implementing Text-As-You-Type Auto Correct Replacements using JavaScript

Text-as-you-type auto correct is a popular feature that replaces a specific phrase or character combination with a longer text while typing. Consider you are repeating specific phrases again and again such as "Thanks for your request". In this case, auto correct would convert the typed characters TY into this phrase while typing text.

AutoCorrect with TX Text Control

Keypress Event

For performance reasons, the JavaScript keypress event is used to store contiguous character codes. If the SPACE key is pressed, the function is trynig to find the typed word in the dictionary of shortcuts. In case the word has been found, the shortcut is selected and replaced with the long text.

// 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);

Exceptions

The shortcut should be only replaced in case the SPACE key is pressed. In other situations such as the ENTER or TAB key, the shortcut should be ignored. The same is valid in case the input position has been changed to another location. In these cases, the character array charCodes of typed characters is emptied again.

TXTextControl.addEventListener("inputPositionChanged", function (position) {

    // if input position is is not coherent, reset character array
    if (position.textPosition < trackedInputPosition - 1 || position.textPosition > trackedInputPosition + 1) {
        charCodes = [];
    }

    // set current input position
    trackedInputPosition = position.textPosition;
})

document.getElementById("txTemplateDesignerContainer").addEventListener("keydown", (event) => {

    // reset character array for all keys including ENTER, TAB and others except SPACE
    if (event.keyCode < 0x30 && event.keyCode != 32) {
        charCodes = [];
    }

}, true);

Live Demo

We implemented this code as part of our online demos. Test this live by launching the demo application.

Auto Text Replacement Demo

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETASP.NET CoreConference

Back from KCDC 2025 - Some Impressions

In this blog post, we will share our impressions of the KCDC 2025 conference and our key takeaways from it. We showcased our latest features and collected valuable feedback from attendees.


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETASP.NET CoreExtraction

Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas

Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…


ASP.NETASP.NET CoreForms

Streamline Data Collection with Embedded Forms in C# .NET

Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…


ASP.NETASP.NET CoreDocument Viewer

High-Performance Text Replacement in Large DOCX Files using C# .NET

Learn how to efficiently replace text in large DOCX files using C# .NET and the ServerTextControl component from Text Control. This article demonstrates the performance benefits of using the…