I was wondering if there was anyway that a webpage could wait for certain keypresses and then display information, such as I type something along the lines of aboutme and the webpage redirects to an about me page(however the text they type is not visible). I sort of gained inspiration when I learned that you can advance onto a unsafe website by typing thisisunsafe in google chrome and was wondering if that functionality could be implemented into a website.
Asked
Active
Viewed 115 times
1
-
Do you mean something like this: https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event ? – Paul Jun 13 '20 at 22:54
-
yes, you can do this with the keypress event https://stackoverflow.com/questions/16089421/simplest-way-to-detect-keypresses-in-javascript – Luca Kiebel Jun 13 '20 at 22:55
2 Answers
1
Use e.code
where e
the event listener defined in the function and .code
is the keys code
that is being triggered... See my example below taken from MDN Documentation...
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event
const log = document.getElementById('log');
document.addEventListener('keypress', logKey);
function logKey(e) {
log.textContent+= `${e.code}` + ' - ';
}
Get the key being pressed in your browser
<div id="log"></div>

dale landry
- 7,831
- 2
- 16
- 28
0
In javascript you can use an EventListener
and you're good to go
window.addEventListener("keypress", function(){
*do what you want to do here*
});

mentamarindo
- 539
- 9
- 16