Aniket Kudale
Aniket Kudale

Reputation: 449

How to detect keypress event in browser console?

Can we detect keypress events in browser console? If so, how?

window.addEventListener( 'keyup', function (e) {
  if ( e.keyCode == 13 ) {
    // Should log the message, if user presses 'enter' key, in browser console?
    console.log('Hello World');
  }
});

P.S Can we add an eventListener to browser's console?

Upvotes: 0

Views: 2005

Answers (2)

CIll
CIll

Reputation: 35

You would like so:

const handleKeyUp = (e: KeyboardEvent): void => {
  console.log(
    '\n:::::::::::::::::::::handleKeyUp:::::::::::::::::::::::',
    '\n:: e.code ::',
    e.code,
    '\n:: e.key ::',
    e.key,
    '\n:::::::::::::::::::::::::::::::::::::::::::::::::::::::'
  );

  if (e.code === 'Escape') {
    setMyThing(true);
  }
};

document.addEventListener('keyup', handleKeyUp);

Not sure which key code to use? https://keycode.info/

Example: https://jsfiddle.net/ClI1/owrb9vhz/4/

Upvotes: 0

user10108359
user10108359

Reputation:

No, you can not detect a keypress event in the browser console. Javascript can manipulate the DOM, which the browser console is not part of and as such, it can not be manipulated with Javascript.

Upvotes: 4

Related Questions