mian Hassan
mian Hassan

Reputation: 21

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

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'
}));

Upvotes: 2

Views: 189

Answers (1)

lordsanken
lordsanken

Reputation: 58

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

Upvotes: 1

Related Questions