Skip to content
Sign UpLog In
Profile icon

Angie

@iamangie
Good morning/afternoon/night, this is my repl.it profile. My name is Angie.
Repls
Community
iamangie
iamangie
shared a Post
3 years ago
Run function when a key is pressed
Hello, how do I run a function when a key is pressed in HTML? Thank you
eco27
eco27
you can use the addEventListener method: document.addEventListener("keypress", () => { console.log("key pressed!") }) `3 years ago
DynamicSquid
DynamicSquid
Try this: function detectKey(event) { event = event || window.event; var key = event.key || event.which || event.keyCode; if (key === 84) { doThing(); } } Put this under the script tag.3 years ago