MCPcopy
hub / github.com/autoNumeric/autoNumeric / _onKeypress

Method _onKeypress

src/AutoNumeric.js:6698–6763  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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;

Callers 1

_createEventListenersMethod · 0.95

Calls 11

_formatValueMethod · 0.95
_triggerEventMethod · 0.95
testMethod · 0.80
getElementValueMethod · 0.80
getElementSelectionMethod · 0.80
setElementSelectionMethod · 0.80

Tested by

no test coverage detected