* Handler for 'keypress' events. * The user is still pressing the key, which will output a character (i.e. '2') continuously until he releases the key. * Note: 'keypress' events are not sent for delete keys like Backspace/Delete. * * @param {KeyboardEvent} e
(e)
| 6696 | * @param {KeyboardEvent} e |
| 6697 | */ |
| 6698 | _onKeypress(e) { |
| 6699 | if (this.compositioning) { |
| 6700 | return; |
| 6701 | } |
| 6702 | if (this.formulaMode) { |
| 6703 | // Accept the backspace, delete, arrow, home and end keys |
| 6704 | if (this._acceptNonPrintableKeysInFormulaMode()) { |
| 6705 | return; |
| 6706 | } |
| 6707 | |
| 6708 | //TODO Prevent keys to be entered on the left-hand side of the '=' sign?...Or just let the user see what they are wrongly doing? |
| 6709 | if (this.settings.formulaChars.test(this.eventKey)) { // Accept the custom decimal character too |
| 6710 | return; // Accept the key in the formula (and do not accept the '=' character here again) |
| 6711 | } else { |
| 6712 | e.preventDefault(); // Reject the key |
| 6713 | } |
| 6714 | |
| 6715 | return; |
| 6716 | } |
| 6717 | |
| 6718 | if (this.eventKey === AutoNumericEnum.keyName.Insert) { |
| 6719 | return; |
| 6720 | } |
| 6721 | |
| 6722 | const processed = this.processed; |
| 6723 | this._updateInternalProperties(e); |
| 6724 | |
| 6725 | if (this._processNonPrintableKeysAndShortcuts(e)) { |
| 6726 | return; |
| 6727 | } |
| 6728 | |
| 6729 | if (processed) { |
| 6730 | e.preventDefault(); |
| 6731 | |
| 6732 | return; |
| 6733 | } |
| 6734 | |
| 6735 | const isCharacterInsertionAllowed = this._processCharacterInsertion(); |
| 6736 | if (isCharacterInsertionAllowed) { |
| 6737 | this._formatValue(e); |
| 6738 | const targetValue = AutoNumericHelper.getElementValue(e.target); |
| 6739 | if ((targetValue !== this.lastVal) && this.throwInput) { |
| 6740 | // Throws input event on adding a character |
| 6741 | this._triggerEvent(AutoNumeric.events.native.input, e.target); |
| 6742 | e.preventDefault(); // ...and immediately prevent the browser to add a second character |
| 6743 | } else { |
| 6744 | if ((this.eventKey === this.settings.decimalCharacter || this.eventKey === this.settings.decimalCharacterAlternative) && |
| 6745 | (AutoNumericHelper.getElementSelection(e.target).start === AutoNumericHelper.getElementSelection(e.target).end) && |
| 6746 | AutoNumericHelper.getElementSelection(e.target).start === targetValue.indexOf(this.settings.decimalCharacter)) { |
| 6747 | const position = AutoNumericHelper.getElementSelection(e.target).start + 1; |
| 6748 | AutoNumericHelper.setElementSelection(e.target, position); |
| 6749 | } |
| 6750 | |
| 6751 | e.preventDefault(); |
| 6752 | } |
| 6753 | |
| 6754 | this.lastVal = AutoNumericHelper.getElementValue(e.target); |
| 6755 | this.throwInput = true; |
no test coverage detected