I am trying to handle an event when the user presses the Backspace button.
I saw this, and I guess I can find Backspace key code using
console.log("Did you delete it? " + e.keyCode);
but the value of e.keyCode
is undefined.
Here is the code:
define(["react"], (React) => {
var TypingContainer = React.createClass({
keypressed(e) {
console.log("Did you delete it? " + e.keyCode);
},
handleChange: function(e) {
// if (e.keycode == 8)
console.log("Did you delete it? " + e.keyCode);
},
render: function() {
return (
<div>
<input
className="typing-container"
value={this.state.message}
onChange={this.handleChange}
onKeyPress={this.keypressed}
/>
</div>
);
}
})
return TypingContainer;
});
Update: With the onKeyPress
event, I always get 0.
keyCode
is deprecated. You should probably be usingkey
instead. With that,e.keycode == 8
would becomee.key === 'Backspace'
. – Eulogistic