我需要在我的反應應用程式的輸入形式中設定按鈕組合。
- "CTRL "向上箭頭"
- “向上箭頭”
我的代碼是這樣的。
//Form
<input onKeyDown={keyHandler}/>
//handler
const keyhandler = (e) => {
if(e.key === "ArrowUp") {//do something}
if(e.key === 'ArrowUp' && e.getModifierState("Control")) {//do another thing}
問題是當事件與組合一起觸發時,首先“如果”也是如此。如何使用 ctrl 按鈕分離“ArrowUp”和“ArrowUp”?
我也這樣嘗試(但沒有幫助):
if(e.key === 'ArrowUp' && event.ctrlKey){ //do another thing}
uj5u.com熱心網友回復:
只要按照你的邏輯,
if (Control is pressed) {
if (ArrowUp is pressed) {
// both are pressed
}
} else {
if (ArrowUp is pressed) {
// only arrow up is pressed
}
}
uj5u.com熱心網友回復:
我用這個解決了我的問題。
if(!e.getModifierState('Control')){
if(e.key == 'ArrowUp'){
//single button action
}
}
if(e.key == "ArrowUp" && e.getModifierState("Control")){
//combo buttons action
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/359742.html
上一篇:如果函式-替換為公式
下一篇:雖然回圈沒有中斷?C
