Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registering for the ESC key on the document appears not to work. #2264

Closed
EisenbergEffect opened this issue Jul 17, 2015 · 4 comments

Comments

Projects
None yet
4 participants
@EisenbergEffect
Copy link

commented Jul 17, 2015

Given the following code:

document.addEventListener('keypress', e => {
  console.log('keypress', e);
});

The event object will be logged to the console for alpha-numerics, enter, backspace, etc. but not for the escape key. I've double checked my code in a browser to ensure I haven't made a mistake and the escape key triggers correctly in that context, but does not in electron. Is there something electron-specific I should be doing to handle this? It does seem like a bug though as it will cause various JS libraries to function incorrectly. A specific example of that is a modal dialog library that wants to enable dialogs to be closed by pressing the escape key.

Operating System: OSX 10.10.4
Electron Version: 0.26.0

@anaisbetts

This comment has been minimized.

Copy link
Contributor

commented Jul 17, 2015

A specific example of that is a modal dialog library that wants to enable dialogs to be closed by pressing the escape key.

Not a solution, but to work around this, you can use the dialog tag which definitely handles the escape key

@zcbenz

This comment has been minimized.

Copy link
Member

commented Jul 20, 2015

Chrome browser doesn't seems to handle it, so if you think it should you can create an issue in Chromium project.

I reproduced by:

  1. Open Chrome 43
  2. Open Google and open devtools
  3. Run document.addEventListener('keypress', function(e) { console.log('keypress', e); }); in devtools
  4. Press ESC and there is no output

@zcbenz zcbenz closed this Jul 20, 2015

@anaisbetts

This comment has been minimized.

Copy link
Contributor

commented Jul 20, 2015

Afaik, keypress only captures key events that can be turned into strings, keyup seems to work:

document.addEventListener('keyup', 
  function(e) { console.log('keyup capture', e); }, 
  true /* grab event on tunnel, not on bubble */);
@akashnimare

This comment has been minimized.

Copy link

commented Aug 30, 2016

@EisenbergEffect this works perfectly -

document.addEventListener('keydown', event => {
    if (event.key === 'Escape' || event.keyCode === 27) {
        window.close();
    }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.