Reputation: 449
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
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
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