Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Wrong keysyms for numpad keys. #94

Closed
daid opened this issue Sep 25, 2019 · 10 comments
Closed

Wrong keysyms for numpad keys. #94

daid opened this issue Sep 25, 2019 · 10 comments

Comments

@daid
Copy link
Contributor

daid commented Sep 25, 2019

On windows/linux
event.keysym.sym reports SDLK_DOWN for the 4 arrow key down, and SDLK_KP_2 for the down key on the numpad.
On emscripten
event.keysym.sym reports SDLK_DOWN for the 4 arrow key down, and SDLK_DOWN for the down key on the numpad or SDLK_2 depending on the numlock setting.

I think the source of the issue seems to stem from

scancode = emscripten_scancode_table[keyEvent->keyCode];

Which uses "keyEvent->keyCode" which comes from:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent

But a better option to get a scancode would be to use the keyEvent->code, which contains a string representation of the scancode.

@Daft-Freak
Copy link
Member

Something like this might also work:

if (keyEvent->location == DOM_KEY_LOCATION_NUMPAD) {
    switch (scancode) {
        case SDL_SCANCODE_0:
        case SDL_SCANCODE_INSERT:
            scancode = SDL_SCANCODE_KP_0;
            break;
        case SDL_SCANCODE_1:
        case SDL_SCANCODE_END:
            scancode = SDL_SCANCODE_KP_1;
            break;
        case SDL_SCANCODE_2:
        case SDL_SCANCODE_DOWN:
            scancode = SDL_SCANCODE_KP_2;
            break;
        case SDL_SCANCODE_3:
        case SDL_SCANCODE_PAGEDOWN:
            scancode = SDL_SCANCODE_KP_3;
            break;
        case SDL_SCANCODE_4:
        case SDL_SCANCODE_LEFT:
            scancode = SDL_SCANCODE_KP_4;
            break;
        case SDL_SCANCODE_5:
            scancode = SDL_SCANCODE_KP_5;
            break;
        case SDL_SCANCODE_6:
        case SDL_SCANCODE_RIGHT:
            scancode = SDL_SCANCODE_KP_6;
            break;
        case SDL_SCANCODE_7:
        case SDL_SCANCODE_HOME:
            scancode = SDL_SCANCODE_KP_7;
            break;
        case SDL_SCANCODE_8:
        case SDL_SCANCODE_UP:
            scancode = SDL_SCANCODE_KP_8;
            break;
        case SDL_SCANCODE_9:
        case SDL_SCANCODE_PAGEUP:
            scancode = SDL_SCANCODE_KP_9;
            break;
        case SDL_SCANCODE_RETURN:
            scancode = SDL_SCANCODE_KP_ENTER;
            break;
        case SDL_SCANCODE_DELETE:
            scancode = SDL_SCANCODE_KP_PERIOD;
            break;
    }
}

(The main reason this is using keyCode instead of code is that mapping a string would be a lot more work and keyCode was better supported at the time.)

@daid
Copy link
Contributor Author

daid commented Sep 25, 2019

Note, I only checked the numpad behavior. But potentially there are more differences.

Note that your code example changes the scancode, but also the keysym is wrong compared to native SDL. So it would be better to fix the keycode first, and then get the scancode from that as the current code is already doing.

It could be that this is a giant can of worms with all the different browsers, that I don't know.

@ghost
Copy link

ghost commented Sep 25, 2019

Yeah, that must be the nature of what I have been experiencing. Numpad is totally fucked.

Also of note (and maybe this only affects me for some reason) is that the keyboard is recognized when testing locally in the browser but not hosted on the net and testing with the same browser. As in, no keyboard input at all. Weird.

Anyhow, shouldn't the official SDL project be dealing with these issues? I feel sorry for you Emscripten guys having to handle all this browser compatibility bullshit yourselves.

@Daft-Freak
Copy link
Member

We only pass the scancode to SDL, we have no control over keysyms. Since the only use of the keyCode is mapping it to a scancode, it shouldn't matter which we modify. (We already do something similar for DOM_KEY_LOCATION_RIGHT)

@Daft-Freak
Copy link
Member

@fluffrabbit I'm the original author for a lot of this backend (including keyboard handling), so pretty much "official SDL" unfortunately (for me).

@ghost
Copy link

ghost commented Sep 25, 2019

@Daft-Freak Whatever that means. Ask Valve to hire some more SDL devs if this is to be a serious backend. I do hope they're paying you. Otherwise, I do not regret the time I have sunk into learning EGL thus far.

@ghost
Copy link

ghost commented Sep 25, 2019

I take it back; web platforms are a waste of time anyways. PC master race.

@daid
Copy link
Contributor Author

daid commented Sep 26, 2019

Ignoring fluffrabbits rambling.

@Daft-Freak the above patch seems to work. Except that KP_5 doesn't work when numlock is off. I do see the javascript event window.onkeydown firing when I press the key, but nothing happens at SDL level.

@daid
Copy link
Contributor Author

daid commented Sep 26, 2019

Oh, I just noticed, it reports keyCode 12 in javascript, which is mapped to SDL_SCANCODE_UNKNOWN. So mapping that to SDLK_SCANCODE_KP_5 would fix the 5.

However, according to https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
It's also the keyCode for "numpad equal" (and numlock on OSX)

Would mapping it to KP_5 be an acceptable solution? As that is the most common key?

daid added a commit to daid/SDL2 that referenced this issue Sep 26, 2019
@Daft-Freak
Copy link
Member

Yeah, that's fine. Just prefix that commit with "emscripten: " and I'll merge it.

@daid daid closed this as completed Sep 26, 2019
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue Sep 30, 2019
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue Sep 30, 2019
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue Sep 30, 2019
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue Apr 2, 2020
Sibras pushed a commit to ShiftMediaProject/SDL that referenced this issue Jan 10, 2021
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue May 7, 2021
Daft-Freak pushed a commit to Daft-Freak/SDL-emscripten that referenced this issue May 7, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants