show different keyboard character from the typed one in google chrome

后端 未结 1 984
夕颜
夕颜 2020-12-02 02:41

I\'m working on a javascript keyboard that seeks to enable users to type in various African languages.Currently, this works fine in IE8 and firefox but not google chrome, an

相关标签:
1条回答
  • 2020-12-02 03:06

    It would be easier not to create a new event, which as you observed is not universally supported, and instead cancel the event and insert the mapped character corresponding to the character typed.

    Assuming the user is typing into a textarea with id "ta", the following will handle keyboard input for that textarea in all the major browsers, mapping q to ɛ and all other characters to "X" for illustrative purposes.

    Be aware that there are issues in IE <= 8 with the code for finding the selection to do with line breaks that the following code doesn't handle for the sake of brevity. You can get my cross browser function for handling this correctly here: Is there an Internet Explorer approved substitute for selectionStart and selectionEnd?

    var charMap = {
        "q": "ɛ"
    };
    
    document.getElementById("ta").onkeypress = function(evt) {
        var val = this.value;
        evt = evt || window.event;
    
        // Ensure we only handle printable keys, excluding enter and space
        var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;
        if (charCode && charCode != 13 && charCode != 32) {
            var keyChar = String.fromCharCode(charCode);
    
            // Get the mapped character (default to "X" for illustration purposes)
            var mappedChar = charMap[keyChar] || "X";
    
            var start, end;
            if (typeof this.selectionStart == "number" && typeof this.selectionEnd == "number") {
                // Non-IE browsers and IE 9
                start = this.selectionStart;
                end = this.selectionEnd;
                this.value = val.slice(0, start) + mappedChar + val.slice(end);
    
                // Move the caret
                this.selectionStart = this.selectionEnd = start + 1;
            } else if (document.selection && document.selection.createRange) {
                // For IE up to version 8
                var selectionRange = document.selection.createRange();
                var textInputRange = this.createTextRange();
                var precedingRange = this.createTextRange();
                var bookmark = selectionRange.getBookmark();
                textInputRange.moveToBookmark(bookmark);
                precedingRange.setEndPoint("EndToStart", textInputRange);
                start = precedingRange.text.length;
                end = start + selectionRange.text.length;
    
                this.value = val.slice(0, start) + mappedChar + val.slice(end);
                start++;
    
                // Move the caret
                textInputRange = this.createTextRange();
                textInputRange.collapse(true);
                textInputRange.move("character", start - (this.value.slice(0, start).split("\r\n").length - 1));
                textInputRange.select();
            }
    
            return false;
        }
    };
    
    0 讨论(0)
提交回复
热议问题