I'm building a screen that has 4 different inputs, 1 character each. When the user types a character, the cursor should be automatically moved to the next input.
It's working perfectly on PC but not on Android Google Chrome.
I researched a bit and found out that onKeyDown and onKeyUp doesn't work properly on Android - the events are triggered but both handlers return keyCode 229 and key "Unidentified. It's apparently related to auto suggest keyboard from Android or something like that.
I've tried onKeyPress and it doesn't trigger at all.
Lastly I've tried onInput. It triggers but there's no keyCode or key and the target.value comes empty.
How should I get the value/key/keyCode from input event handler on Android?
Here's my form:
<input autoFocus className="shadowed color-blue" type="text" maxLength="1" id="first" onInput={(e) => this.moveOnMax(e,'first','second')} />
<input className="shadowed color-red" type="text" maxLength="1" id="second" onInput={(e) => this.moveOnMax(e,'first','third')} />
<input className="shadowed color-yellow" type="text" maxLength="1" id="third" onInput={(e) => this.moveOnMax(e,'second','fourth')} />
<input className="shadowed color-green" type="text" maxLength="1" id="fourth" onInput={(e) => this.moveOnMax(e,'third','fourth')} />
Here's my event handler:
moveOnMax (event, previousFieldID, nextFieldID) {
event.preventDefault();
if (event.which === 37) //left arrow key
document.getElementById(previousFieldID).focus();
else if (event.which === 39) //right arrow key
document.getElementById(nextFieldID).focus();
else if (event.which === 8) { //backspace
event.currentTarget.value = "";
document.getElementById(previousFieldID).focus();
}
if (event.which >= 48 && event.which <= 105) {//valid character, fill the form
if (event.currentTarget.value.length === 0) {
event.currentTarget.value = event.key.toUpperCase();
document.getElementById(nextFieldID).focus();
} else {
document.getElementById(nextFieldID).value = event.key.toUpperCase();
document.getElementById(nextFieldID).focus();
}
}
}