2016-09-18 72 views
1

如何檢測ReactJS onKeyPress事件中的'Shift + Enter'?如何檢測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')); 

回答

7

你的答案是在檢測 '回車' 不是 '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.nativeEvent.keyCode === 13) {   
     if(e.nativeEvent.shiftKey){ 
      $('#app').append("<br/> Detected Shift+Enter") 
     } 
    } 
    } 
    render() { 
    return (< textarea defaultValue = { 
     "" 
     } 
     onKeyUp = { 
     this.handleKeyPress 
     } 
     /> 
    ); 
    } 
} 

ReactDOM.render(<App/>, document.getElementById('app')); 
+0

嗯...是不是一回事? –

+0

不是。如果您只按「Enter」,您將看不到任何內容。您需要按'Shift + Enter' –

+0

編輯評論..按錯誤輸入。 –