如何检测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'));
解决方法
您的答案是检测到“输入”而不是“切换输入”.这应该有帮助!
https://jsfiddle.net/Pranesh456/c2hzt27g/3/
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'));