简体   繁体   中英

Pressing Tab to active Enter key

I have a question in react/JavaScript that when I press Tab on the keyboard, I need the Enter key to be triggered or active, so any idea how can I do it.

Although I'm not sure what you're motivation for this behavior is, your logic is even possible with Vanilla JS:

Minimal example for your use case (listen for 'Tab' keydown, trigger 'Enter' keydown if Tab occurred):

 let element = document.querySelector('input'); element.onkeydown = e => { alert('key pressed: ' + e.key); if (e.key === 'Tab') { element.dispatchEvent(new KeyboardEvent('keydown', { 'key': 'enter' })); } }
 <input/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM