0

I have Escape key function code, Please suggest how to execute instead of button onclick the same function. Kindly suggest.

//escape key function
Brav1Toolbox.addListener(window, "keyup", onKeyUp);
function onKeyUp(e) {
//code
}
SRK
  • 23
  • 7

3 Answers3

2

What you're looking for are known as keyCodes. The escape key is keyCode 27. Simply run a conditional that checks that keyCode 27 is pressed:

// Escape key function
Brav1Toolbox.addListener(window, "keyup", onKeyUp);

function onKeyUp(e) {
  const keyCode = e.key || e.keyIdentifier || e.keyCode;
  if (keyCode === 27) {
    console.log("Escape was pressed");
  }
}

Hope this helps! :)

Bamieh
  • 10,358
  • 4
  • 31
  • 52
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
1

Use window.onkeydown and keyCode.

window.onkeydown = function(e) {
  var keyCode = e.key || e.keyIdentifier || e.keyCode;
  if (keyCode == 27 || keyCode == 'Escape') {
    alert("The ESC key was pressed!");
    change_screen(27); //Run function
  }
}
Chris Happy
  • 7,088
  • 2
  • 22
  • 49
  • request to need to,
    CLICK
    How to write the escape function to click event
    – SRK Sep 05 '17 at 04:32
  • I need to reverse order, if (e==27){ e.keyCode=27 will be executed } It is posible – SRK Sep 05 '17 at 04:40
  • You can do that. However, it gets [pretty interesting](https://stackoverflow.com/questions/596481/is-it-possible-to-simulate-key-press-events-programmatically). I would recommend running the function that is triggered when "ESC" is pressed. – Chris Happy Sep 05 '17 at 04:43
  • Pls, No need to escape key code function, When mouse click "button" that time kecode=27 need to run. – SRK Sep 05 '17 at 04:50
0

Try this

window.addEventListener('keydown', function(e){
    if((e.key=='Escape'||e.key=='Esc'||e.keyCode==27) && (e.target.nodeName=='BODY')){
        console.log("Escape pressed");
        return false;
    }
}, true);
Znaneswar
  • 3,329
  • 2
  • 16
  • 24