2017-03-10 26 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'); 
    } 
});