Reporter: RobertBColton | Status: closed | Last Modified: June 29, 2020, 04:25:54 pm
This stops it from crashing by giving it the scancode like it wants instead of the keycode. It does actually work now, except for CTRL, ALT, & Shift which will be fixed by #2058 later.
The documentation is clear that it expects scancodes, not keycodes, which do have different values.
https://wiki.libsdl.org/SDL_GetKeyboardState
Here's a test I devised that works in Win32 master but not SDL master, only this pull request.```cppvar text;text = "";text += string(keyboard_check_direct(vk_alt)) + " ";text += string(keyboard_check_direct(vk_lalt)) + " ";text += string(keyboard_check_direct(vk_ralt)) + "#";text += string(keyboard_check_direct(vk_control)) + " ";text += string(keyboard_check_direct(vk_lcontrol)) + " ";text += string(keyboard_check_direct(vk_rcontrol)) + "#";text += string(keyboard_check_direct(vk_shift)) + " ";text += string(keyboard_check_direct(vk_lshift)) + " ";text += string(keyboard_check_direct(vk_rshift)) + "#";draw_text(0, 0, text);
Alright, you piqued my interest, so I devised a little test. GMSv1.4 gave me 49891.30, this pull request gave me 4839.6 in SDL, and ENIGMA Win32 master didn't respond. I am guessing GMS either doesn't use SDL on Windows or they actually use something lower level like our Win32 does, because the SDL docs do say this state seems to be message derived.
var avg, i;
avg = 0;
for (i = 0; i < 10; i += 1) {
var delta;
delta = get_timer();
repeat (100000) {
keyboard_check_direct(vk_left);
}
delta = get_timer() - delta;
avg += (delta - avg) / (i + 1);
}
show_message("avg : " + string(avg));
```
Also another interesting smaller test, apparently the logical `vk_nokey` and `vk_anykey` were never supported by `keyboard_check_direct()`, not in GM8.1or GMSv1.4 afaict.
```cpp
var text;
text = string(keyboard_check_direct(vk_nokey)) + "" + string(keyboard_check_direct(vk_anykey));
draw_text(0,0,text);
```
Some additional testing I've done on #2060 shows that `SDL_GetKeyboardState` is in fact not low-level. It won't receive input when the window isn't focused. Looking around the internet does indicate that most people are aware that you would have to us the OS-level functions to get input when not in focus with SDL, so it is what it is.
Alright, now #2060 found absolute definitive proof that SDL's version is not actually direct and kind of useless. Only `GetAsyncKeyState` can behave like GMSv1.4 and GM8.1 actually do. SDL does not appear to wrap this function anywhere in its codebase, thus in order for `keyboard_check_direct()` to ever work in our SDL it would need to compile and link in our Win32/xlib equivalents instead. We'll cross that bridge when we come to it and somebody notices it/requests it.