@ithaka/bonsai
Version:
ITHAKA core styling
111 lines (100 loc) • 2.62 kB
JavaScript
const KeyboardHelper = {
keys: {
TAB: 9,
ENTER: 13,
ESCAPE: 27,
SPACE: 32,
ARROW_LEFT: 37,
ARROW_UP: 38,
ARROW_RIGHT: 39,
ARROW_DOWN: 40
},
/**
* Checks if the key that was pressed was shift + tab together
*
* @param event {event} - event object
* @returns {boolean}
*/
wasShiftTab(event) {
try {
return event.shiftKey && this.wasTab(event.keyCode);
}
catch(error) {
console.error("KeyboardHelper.wasShiftTab didn't receive expected event object", event);
return false;
}
},
/**
* Checks if the key that was pressed was tab
*
* @param keycode
* @returns {boolean}
*/
wasTab(keycode) {
return keycode === this.keys.TAB;
},
/**
* Checks if the key that was pressed was enter
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasEnter(keycode) {
return keycode === this.keys.ENTER;
},
/**
* Checks if the key that was pressed was the spacebar
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasSpaceBar(keycode) {
return keycode === this.keys.SPACE;
},
/**
* Checks if the key that was pressed was escape
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasEscape(keycode) {
return keycode === this.keys.ESCAPE;
},
/**
* Checks if the key that was pressed was the up arrow
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasUpArrow(keycode) {
return keycode === this.keys.ARROW_UP;
},
/**
* Checks if the key that was pressed was the down arrow
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasDownArrow(keycode) {
return keycode === this.keys.ARROW_DOWN;
},
/**
* Checks if the key that was pressed was the left arrow
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasLeftArrow(keycode) {
return keycode === this.keys.ARROW_LEFT;
},
/**
* Checks if the key that was pressed was the right arrow
*
* @param keycode {number} - keyboard key mapped number
* @returns {boolean}
*/
wasRightArrow(keycode) {
return keycode === this.keys.ARROW_RIGHT;
}
};
export default KeyboardHelper;