can we dispatch a KeyboardEvent of press & hold of a keyboard key?

Viewed 29

how can i dispatch a KeyboardEvent in window of pressing a keyboard key ? in javascript ? i have scenario in which user press and hold a specific keyboard button and then drag the mouse on screen it will draw a box , but i want to press that specific button programmatically on load of document , so whenever user will clickdown and drag mouse it will draw , is this possible ?

what is have a this keydown but its not working

 window.dispatchEvent(new KeyboardEvent('keydown', {
    'key': 'control'
}));
1 Answers

It's a tiny bit trickier to do the "mousedown" than, for example, onlick. Here's what I'm using:

$('div').on('mousedown mouseup', function mouseState(a) {
    if (a.type == "mousedown") {
        //whatever you like
    }
});

Tell me if it helped :D

Related