How to detect 'Shift + Enter' in ReactJS onKeyPress event?
Is it possible ?
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'));
Your answer is detecting on 'Enter' not 'Shift+Enter'. This should help! 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'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With