Binita Gyawali
Binita Gyawali

Reputation: 256

How to trigger a space key event with keycode

I am trying to trigger space key with keycode in JavaScript. I will be sending voice command with space and it should trigger a space event with a keycode.

This is what I have done so far

if(firstword =="space"){
  const ev = {
     type: 'space',
    keyCode: 32 
}
editor.triggerOnKeyDown(ev);

The code works perfectly if I use enter or other keycode but not working for space, any idea?

Upvotes: 0

Views: 6756

Answers (2)

Alex Ciu
Alex Ciu

Reputation: 148

You can try like this:

const ev = new KeyboardEvent('keydown',{'keyCode':32,'which':32});

Reference: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/KeyboardEvent

Upvotes: 2

G3ck
G3ck

Reputation: 252

Ty to create addEventListener for space

window.addEventListener('keypress', function (e) {
    if (e.keyCode == 32) {
       alert('Space pressed');
    }
}, false);

Upvotes: 0

Related Questions