gpt4 book ai didi

javascript - 如何在 ReactJS onKeyPress 事件中检测 'Shift + Enter'?

转载 作者:可可西里 更新时间:2023-11-01 02:16:44 25 4
gpt4 key购买 nike

如何检测 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com