2017-03-10 52 views
0

所以我想觸發一個向下按鍵的'tab'事件和一個向上按鍵的'shift + tab'事件。這是迄今爲止我所得到的,但我似乎無法設置Keypress事件的keyCode。我讀過那個keyCode已被棄用,但我也嘗試了代碼(它確實設置了),但它並沒有觸發輸入操作。觸發一個特定的按鍵

if (e.keyCode === 38) { 
    // trigger a shift tab 
    console.log('up key was pressed') 
} 

if (e.keyCode === 40) { 
    console.log('down key was pressed') 
    let event = new KeyboardEvent('keyPress', { keyCode: 9 }) 
    window.dispatchEvent(event) 
} 
+0

你想多個字段例如之間選項卡在一組輸入中來回移動? – G0dsquad

+0

你需要爲這些使用keydown –

回答

0

https://jsfiddle.net/0Lv9bthd/18/捕獲向上和向下箭頭上按下你需要的keydown,不能使用按鍵

window.addEventListener("keydown", function(e) { 
    console.log(e.keyCode); 
    if (e.keyCode == 38) { 
    console.log('up key was pressed'); 
    } 
});