How to catch event.keyCode and change it to another keyCode?
Asked Answered
D

4

16

I have checked other questions here at SO, however they do not answer my question. I want to simply catch certain keyCode and replace it with another. I am working with characters, not white spaces, and I do not need to loose focus or the like.

Below is my code. But you can replace those keyCodes with your (eg. when capital "A" is pressed, it should replace with zero 0, etc.). The idea is to replace the keyCode.

phrase.keypress(function(event)
{
    if (event.shiftKey)
    {
        switch (event.keyCode)
        {
            // Cyrillic capitalized "Н" was pressed
            case 1053: event.keyCode = 1187; event.charCode = 1187; event.which = 1187; break;
            // Cyrillic capitalized "О" was pressed
            case 1054: event.keyCode = 1257; event.charCode = 1257; event.which = 1257; break;
            // Cyrillic capitalized "У" was pressed
            case 1059: event.keyCode = 1199; event.charCode = 1199; event.which = 1199; break;
        }
    }
});

I tried with keydown and keyup as well. They do not alter the keyCode. How can I do that?

P.S. If possible, I am looking for a solution which does not "event.preventDefault() and manually insert desired key to input field, then move cursor to the end". I want cleaner and "right" solution. Thank you.

Destructor answered 8/1, 2012 at 9:27 Comment(0)
I
13

Keyboard event properties are all READ-only. You cannot capture one keyCode and change it to another.

See reference from MDN - Keyboard Events - All are read only can't be set.

As you mentioned in your post. -- If you wan't to handle, then you have to stop browser default key press and set the desired value to the element yourself.

Isadoraisadore answered 8/1, 2012 at 9:57 Comment(2)
Thank you. Now I know they are Read-only. And I implemented that "manual" solution.Destructor
Just somthing that might help. A list I posted on jquery forums of "which" codes. Could be helpful to ya. if not, could be helpful to someone else who comes across here. forum.jquery.com/topic/eventwhich-code-list-just-for-helpColdshoulder
C
6

While the properties on the KeyboardEvent instance is READ ONLY, you can override KeyboardEvent's prototype and create a getter for whatever you want to change. Here is an example which changes the keycodes of hjkl to act like arrow keys.

Object.defineProperty(KeyboardEvent.prototype, 'keyCode', {
    get: function() {
        switch (this.key) {
            case 'h': return 37; // left
            case 'j': return 40; // down
            case 'k': return 38; // up
            case 'l': return 39; // right
            default: return this.which
        }
    }
})
Chandelle answered 23/3, 2019 at 18:41 Comment(1)
That's a good solution.Guthrun
S
4

I am using the following code to achieve the same result as if I had changed the keyCode, without actually being able to change it.

function inputValidation() {
    var srcField = event.srcElement;
    var sKey = event.keyCode;
    var inputLetter = String.fromCharCode(sKey);
    if (typeof(srcField) !== "undefined" && srcField !== null) {
        inputLetter = transformInput(inputLetter);
        var caretPos = srcField.selectionStart;
        var startString = srcField.value.slice(0, srcField.selectionStart);
        var endString = srcField.value.slice(srcField.selectionEnd, srcField.value.length);
        srcField.value = startString + inputLetter + endString;
        setCaretPosition(srcField, caretPos+1); // '+1' puts the caret after the input
        event.preventDefault ? event.preventDefault() : event.returnValue = false; //for IE8
   }
}

srcField.selectionStart gives the starting position of the text you have selected and srcField.selectionEnd gives the end position of the selection, if you haven't selected any text srcField.selectionStart equals srcField.selectionEnd.

The function setCaretPosition came from this answer by kd7. I only changed it to make it receive the element instead of its Id

function setCaretPosition(elem, caretPos) {
    if (elem != null) {
        if (elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        } else {
            if (elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            } else
                elem.focus();
        }
    }
}
Subsidy answered 27/5, 2014 at 14:14 Comment(0)
P
2

you can change value as manuel and handle keypress. if you want to check key declare a variable and check your keycode.

this.event.target.value = this.event.target.value + ".";
return false;

full code:

function isNumberKeyDec(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var apos = 0;
    if (charCode == 44) {
        apos = 1;
        charCode = 46;
    }
    if (this.event.target.value.length == 0 && charCode == 46) {
        return false;
    }
    if (this.event.target.value.length == 1 && this.event.target.value == "0" && charCode == 48) {
        return false;
    }
    if (this.event.target.value.length == 1 && this.event.target.value == "0" && charCode != 46) {
        this.event.target.value = "";
    }    
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {

        return false;
    }
    if (charCode == 46 && this.event.target.value.indexOf(".") != -1) {
        return false;
    }
    if (this.event.target.value.indexOf(".") != -1) {
        if (this.event.target.value.substring(this.event.target.value.toString().indexOf(".")).length >= 2) {
            return false;
        }        
    }
    if (this.event.target.value.indexOf(".") == -1 && charCode != 46) {
        if (this.event.target.value.length >= 3 && charCode != 46) {
            return false;
        }
    }
    if (apos == 1) {
        this.event.target.value = this.event.target.value + ".";
        return false;
    }
    return true;
}
Promotion answered 13/4, 2020 at 14:50 Comment(1)
Thansk worked for me. I used: this.event.target.value = this.event.target.value + ","; this.event.preventDefault(false);Hyphenate

© 2022 - 2025 — McMap. All rights reserved.