Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Hardware keyboard input (#36)
Browse files Browse the repository at this point in the history
Adds the ability to use a hardware keyboard with the math input. This allows basic input of numbers, operators, and the delete key.

In the future we could choose to allow multi character input (like != or >=) and complex input (like sin, cos, tan), and more movement keys (like arrows, control+a, control+e). All of these things will greatly increase the complexity in the implementation and might be platform dependent so we want to wait until learners give a clear signal that they want these features.
  • Loading branch information
nixterrimus committed Dec 5, 2019
1 parent ff2ba5f commit 4a7e359
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/components/input/math-input.js
Expand Up @@ -15,6 +15,7 @@ const {
} = require('../common-style');
const {keypadElementPropType} = require('../prop-types');
const {brightGreen, gray17} = require('../common-style');
const Keys = require("../../data/keys");

const i18n = window.i18n || {_: s => s};

Expand Down Expand Up @@ -697,6 +698,55 @@ class MathInput extends React.Component {
this._updateCursorHandle(true);
};

domKeyToMathQuillKey = (key) => {
const keyMap = {
"+": Keys.PLUS,
"-": Keys.MINUS,
"*": Keys.TIMES,
"/": Keys.DIVIDE,
".": Keys.DECIMAL,
"%": Keys.PERCENT,
"=": Keys.EQUAL,
">": Keys.GT,
"<": Keys.LT,
"^": Keys.EXP
};

// Numbers
if (['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'].includes(key)){
return `NUM_${key}`;
}

// Movement keys
else if (key == "Backspace"){
return Keys.BACKSPACE
}

// Operators
else if (key in keyMap){
return keyMap[key];

}

// The key pressed doesn't map to any of the math input operators
return null;
}

handleKeyUp = (event) => {
const mathQuillKey = this.domKeyToMathQuillKey(event.key);

if (mathQuillKey){
this.mathField.pressKey(mathQuillKey);

const value = this.mathField.getContent();
if (this.props.value !== value) {
this.mathField.setContent(this.props.value);
this.props.onChange(value, false);
this._hideCursorHandle();
}
}
};

render() {
const {focused, handle} = this.state;
const {style} = this.props;
Expand Down Expand Up @@ -753,6 +803,7 @@ class MathInput extends React.Component {
ref={node => {
this.inputRef = node;
}}
onKeyUp={this.handleKeyUp}
>
{/* NOTE(charlie): This element must be styled with inline
styles rather than with Aphrodite classes, as MathQuill
Expand Down

0 comments on commit 4a7e359

Please sign in to comment.