Is there a way to disable repeat text input?


#1

I’m handling how repeat key presses are timed with my own code, and it works well with keydown events since they tell me if they’re repeats and I can filter them out, but text input events don’t have this information, and so I can’t merely filter them.

Is there anyway I can disable key repeating in SDL2?


#2

Sorry to just post a link, but:
https://stackoverflow.com/questions/22156815/how-to-disable-key-repeat-in-sdl2


#3

That solution works for SDL_Event::key events, but not SDL_Event::text events which lack a field to indicate if the characters were repeated or not, so filtering is impossible in this case.


#4

Sorry it took so long to reply. SDL_TextEditingEvent At the bottom of that link, there are two functions to start and stop text events from being created. If you use the SDL_StopTextInput function when you are in “game mode”, does that resolve your issue? If it doesn’t, you could always create a flag for your “game mode” that your event handler can use to ignore text events with.


#5

AIUI the OP doesn’t want to suppress all text events, he wants to suppress text events resulting from keyboard auto-repeats, which makes sense in the context of a game.

I would suggest setting a flag in the SDL_KEYDOWN event (when the key.repeat member is zero) and accepting input from the SDL_TEXTINPUT event only when that flag is set - clearing it at the same time. That way a text event would be accepted so long as at least one non-repeat keydown event had occurred since the last text input, but not if only auto-repeats had occurred. It’s not a perfect solution, but I can’t think of a better workaround.


#6

That feels like a really good solution for this kind of thing, though I am hesitant to trust it entirely… I think it might be best to just live with the behavior for now, hope the player likes how their OS text repeat is timed, and treat backspace, tab, delete, arrow keys, etc to specially accept repeat events so their behavior matches how text input behaves.


#7

Admittedly I have a problem communicating so allow me to clarify, (what I just said, and the rest of this isn’t meant to sound salty or anything, so I apologize if it does). What I thought I said is that there are these two functions that can start and stop text editing inputs from being created from the Event system. This should do exactly what I believe OP wants them to do. So when your program is not capable of using/handling text events, “game mode”, run the SDL_StopTextInput function. When you need text again, run the SDL_StartTextInput function to start receiving text again.

If that doesn’t work, create a flag for when your game can or cannot handle text events, “game mode”, and have Text Input events ignored if they’re not needed. Do this inside of your text input handler, and I recommend only putting it into the text handling portion to avoid unnecessary checks in the rest of the event handler since “game mode” should be a key part of your program. Here’s some quick C\C++ code as an example:

if(!gamemode)
  {
    /* Handle text input events. */
  };
/*Else should not be used since all text events if we're in game mode.*/

#8

The OP wants to disable (only) keyboard auto-repeat events. Unless I’ve misunderstood something, your proposal disables all text input events, even normal key-presses, which is not what is wanted.


#9

Normal key presses are not disabled, only text inputs, (“Hello” text event vs. Shift down, h down, h up, Shift up, e down, e up, etc.). They keyboard should still be updated as normal.


#10

Once more, the OP wants to discard only keyboard auto-repeat events. For example, if the user presses the H key and holds it down, the program should receive a single H text event; the additional events resulting from auto-repeat should be discarded. You may find it helpful to read the OP’s initial post again.


#11

Why do you want to handle the repeats yourself? I’m assuming this is about text input inside a game and from the first message I’m understanding that you want to basically reimplement the OS functionality yourself.

The repeat functionality you are getting by default is something the user can normally configure himself on OS level. For example change the delay time and repeat rate. There could be some accessibility options related to this too, but I’m not really familiar with those. If you implement it yourself you’ll lose all that.

I’m sure there is some kind of valid reason for wanting to do this. I’m just curious.