We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4844 - Android/iOS keys handling issue
Summary: Android/iOS keys handling issue
Status: RESOLVED WONTFIX
Alias: None
Product: SDL
Classification: Unclassified
Component: events (show other bugs)
Version: 2.0.10
Hardware: All Android (All)
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-10-23 11:32 UTC by Skarpeys
Modified: 2019-11-01 07:32 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Skarpeys 2019-10-23 11:32:21 UTC
Backspace/Arrows Keys: get SDL_KEYUP right after SDL_KEYDOWN (Android/iOS only), even when we don't release them.

Example (in a loop):
static bool triggered = false;
if (event->type == SDL_KEYDOWN && e.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
  DoFunction1();
  triggered = true;
}

if (event->type == SDL_KEYUP && e.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
  DoFunction2();
  triggered = false;
}

if (triggered) {
  DoFunction3();
}

DoFunction1 & DoFunction2 work fine but DoFunction3 never gets called. This easily leads to unexpected behavior compare to Desktop OS. Example issue: ImGui - https://github.com/ocornut/imgui/issues/2838

-----------------

There's another related issue: keys don't get SDL_KEYUP event (Android/iOS Simulator - use physical keyboard instead of virtual Android/iOS keyboard). Inside above ImGui link too.
Comment 1 Alex Szpakowski 2019-10-26 15:12:21 UTC
I'm not sure about Android, but it doesn't look like Apple provides APIs to determine separate press and release events for hardware keys. There's not much SDL can do to work around that.

(In reply to Skarpeys from comment #0) 
> DoFunction1 & DoFunction2 work fine but DoFunction3 never gets called. This
> easily leads to unexpected behavior compare to Desktop OS.

The approach your code example is taking will fail to detect events on all operating systems and all keyboard setups, depending on circumstances outside your code's control. For example if a user presses and releases a key very quickly, the press and release events may happen within the same frame (even if your game is running at a high framerate) which means DoFunction3 will not be called. The same thing can happen if a user presses and releases a key more slowly but there's a framerate hitch during that frame (and the cause of framerate hitches can be completely out of your control, eg if another process on the user's system does something CPU-intensive).

I really recommend avoiding that style of performing actions. If running the action code inside the input event loop itself is not feasible, your code could at least be changed to run DoFunction3 if the key press event happened in that frame, regardless of whether the key release event also happened in the same frame
Comment 3 Skarpeys 2019-10-30 12:02:02 UTC
@Alex:
That example is what that ImGUI does - not my own code. I make it so you guys can understand it easily. I doubt "ocornut" gonna change that structure.

I'm pretty sure DoFunction3 works fine on Desktop OS (I tried all 3: Linux (KDE Neon)/Windows 10/Hackintosh Mojave).


@Sylvain:
Thanks, I will try to test around, I have too little time for this but I will.
Comment 4 Sylvain 2019-10-30 12:31:14 UTC
@Skarpeys,
I also agree with Alex, the pattern is broken, and you'll be missing keys if they are pressed/released too quickly anyway / or simply if  you're unlucky with timings.
Here, you're missing transitions of KeysDown[key] !

Funny thing: 
- you don't miss transitions for Mouse event, because only SDL_MOUSEBUTTONDOWN events are handled, not SDL_MOUSEBUTTONUP
- you could also miss SDL_MOUSEWHEEL when it flickers +1 -1


The code you talked about is:

https://github.com/ocornut/imgui/blob/master/examples/example_sdl_opengl2/main.cpp#L85
https://github.com/ocornut/imgui/blob/master/examples/imgui_impl_sdl.cpp#L111


bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
{
    ...
    case SDL_KEYDOWN:
    case SDL_KEYUP:
        {
            io.KeysDown[key] = (event->type == SDL_KEYDOWN);
    ...
}

and the main loop:

    while (!done)
    {
        while (SDL_PollEvent(&event))
        {
            ImGui_ImplSDL2_ProcessEvent(&event);
            if (event.type == SDL_QUIT)
                done = true;
        }

        rendering
   }
Comment 5 Skarpeys 2019-11-01 01:23:41 UTC
@Sylvain:
Hmm ... Okay. I can make a temporary fix for that one but I can't apply it to live ImGUI.


One more: as I mentioned, when make Run with Virtual Android Device, Android Virtual Keys work as we were talking about, but Physical Keys (my laptop) don't have SDL_KEYUP event. For real Android Phone (my Sony Z5) then all good, Bluetooth Keyboard Keys work same as Virtual Keys. I think it's worth to mention since it's kinda strange.


Thanks again, Alex & Sylvain.
Comment 6 Sylvain 2019-11-01 07:32:18 UTC
@Skarpeys: report the behavior issue to ImGUI and they'll fix it (hopefully!)

About the android virtual devices: the emulator, right ? I think the hardware keyboard is not working inside android. but I'm not 100% sure. 
- compare with other apps !
- if needed, adds some printf inside the SDL java class to see what happens ...