☑ Problem with keyCodes (use keydown instead of keypress)

I’m trying to use keyCode 127 for the delete key, but when I press the key, my function does not run. When I type Alt + 0127 it runs my function. Can someone explain to me what is wrong… I’ll give you a code snippet below.

By the way,

f = true
m = true
key = 127

document.body.addEventListener('keypress', function(e){

    if(e.keyCode == key){
        if(f === true){
                    console.info('Enabled Fullscreen');
                    console.error('Enabling Fullscreen Failed!');
        if(m === true){
                console.info('Enabled Pointer Lock');
                console.error("Enabling Pointer Lock Failed!");

I think DELETE and BACKSPACE are keycodes 46 and 8 respectively.

No, 46 is the periord, “.” button. I’ve tried http://keycode.info/, but those codes are incorrect in this case, you have to use the ASCII keycodes found on http://www.theasciicode.com.ar/ascii-codes.txt, these are the keycodes that I know work for a-z, A-Z, and 0-9. On there it says delete is 127 and it works with Alt + 0127 but not the delete button itself.

I’ve also found that if you try to use the keycodes for F1 - F12, they don’t work, at least on google chrome. I feel these keys are reserved in google chrome’s browser, but I could be wrong and its the case for all browsers. I would try Microsoft Edge but you know, it’s a Microsoft browser, therefore it doesn’t like me.

You are looking at ASCII codes. The event.keyCode value isn’t ASCII.

Use this table: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode

Yes, but when I use keycode 46, it works for the “.” key and not the delete key like you say. Its weird and I don’t understand why this is the case but even with the “f” key, it should be 70, but it is 102.

. is 190
f is 70
Here is small app to test keyCodes: http://keycode.info/
And table with keyCodes: https://css-tricks.com/snippets/javascript/javascript-keycodes/

I understand what the keyCodes are, but if you put that inside of my script, they don’t work for the keys they should. Is there possibly something wrong with my script?

Possibly, without link to simplified replication code, is hard to tell :slight_smile:

Here is the link to my script, I hope that someone can come to a conclusion to my problem.


UPD: look below for answer, basically use keydown instead of keypress event.

So I’ve made small project, with similar example.
And it works all as intended. Launch it and press Delete, look in console.

In your code you call setup once, then it uses value of that key, so it means if you change enableKey attribute during your app running, it wont update.
Although code I’ve shared, will do take in account actual enableKey field, so it changeable in runtime.

That doesn’t work for me… Maybe my browser just doesn’t allow me to use that button or reconizes it for some reason.

So Delete button didn’t trigger event?
What keyboard layout and system language you use, what browser and OS?

I’m using Windows 10 with the Google Chrome browser

What is your system language (precisely) and keyboard layout?
And on this website: http://keycode.info/ what it shows if you press Delete?

System language: English, United States

What do you mean by keyboard layout?

Delete says 46, even though when I use it, it works for the period ‘.’ button

Just realised, you use keypress, which returns different value than keydown. Use keydown or keyup instead, and it will be right keyCode.

Thank you @max! I can’t beleive how one wrong move can make this much commotion!

1 Like

hey my spike tank or anything for that matter will not move. the code editor does not understand keyboard or arrow_key or anything else can you help me out

Have you tried debugging your code? What are the results of trying to investigate and solve the problem?

i dont know how im a new person at this