如何检测 ReactJS onKeyPress 事件中的“Shift + Enter”?
这可能吗?
https://jsfiddle.net/jacobgoh101/cyLqfcts/3/
class App extends React.Component {
constructor(props) {
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
handleKeyPress(e) {
console.log(e.key);
$('#app').append("<br/>" + e.key);
}
render() {
return ( < textarea defaultValue = {
""
}
onKeyPress = {
this.handleKeyPress
}
/>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
最佳答案
您的答案是检测“Enter”而不是“Shift+Enter”。这应该有帮助! https://jsfiddle.net/Pranesh456/c2hzt27g/3/
class App extends React.Component {
constructor(props) {
super(props);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
handleKeyPress(e) {
if (e.key === 'Enter' && e.shiftKey) {
$('#app').append("<br/> Detected Shift+Enter")
}
}
render() {
return (
<textarea
defaultValue = {""}
onKeyUp={this.handleKeyPress}
/>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
关于javascript - 如何在 ReactJS onKeyPress 事件中检测 'Shift + Enter'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559071/