# Key/button repeat

**URL:** <https://discourse.libsdl.org/t/key-button-repeat/20529>\
**Category:** SDL Development\
**Created:** [March 21, 2014, 10:31am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529 "2014-03-21T10:31:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dark\_Oppressor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Dark\_Oppressor](https://discourse.libsdl.org/u/Dark_Oppressor)\
**Post date:** [March 21, 2014, 10:31am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/1 "2014-03-21T10:31:54Z")

</div>

So, I’m currently checking for SDL\_KEYDOWN events to navigate around in a menu in my game. If I hold down the ‘down’ key, I can move quickly down through the menu, due to key repeat. This is what I want to happen, but I am curious how exactly key repeat works/is controlled in SDL2.

So, how does it work exactly?

Also, is there any way to enable the same kind of repeating for controller buttons? I looked in the SDL\_ControllerButtonEvent struct and didn’t see anything about repeat, which I did see in SDL\_KeyboardEvent. I’m guessing there is no such option, but I thought I’d ask. Thanks!

---

<div class="post-metadata">

**Author:** ![Javier\_Flores](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/javier_flores/32/2577_2.png) [@Javier\_Flores](https://discourse.libsdl.org/u/Javier_Flores)\
**Post date:** [March 21, 2014, 10:47am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/2 "2014-03-21T10:47:47Z")

</div>

[http://sdl.beuc.net/sdl.wiki/SDL\_EnableKeyRepeat](http://sdl.beuc.net/sdl.wiki/SDL_EnableKeyRepeat) exaplains a little bit  
about it.

not really sure about the delay in repeating in joysticks.

2014-03-21 3:31 GMT-07:00 Dark\_Oppressor :\> So, I’m currently checking for SDL\_KEYDOWN events to navigate around in

> a menu in my game. If I hold down the ‘down’ key, I can move quickly down  
> through the menu, due to key repeat. This is what I want to happen, but I  
> am curious how exactly key repeat works/is controlled in SDL2.
> 
> So, how does it work exactly?
> 
> Also, is there any way to enable the same kind of repeating for controller  
> buttons? I looked in the SDL\_ControllerButtonEvent struct and didn’t see  
> anything about repeat, which I did see in SDL\_KeyboardEvent. I’m guessing  
> there is no such option, but I thought I’d ask. Thanks!
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
_Javier Flores_

---

<div class="post-metadata">

**Author:** ![Dark\_Oppressor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Dark\_Oppressor](https://discourse.libsdl.org/u/Dark_Oppressor)\
**Post date:** [March 22, 2014, 1:57am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/3 "2014-03-22T01:57:17Z")

</div>

But that is for SDL 1.2, isn’t it? I’m working with SDL2, which doesn’t seem to have SDL\_EnableKeyRepeat. Unless I’m missing something?

javierecf wrote:\> [http://sdl.beuc.net/sdl.wiki/SDL\_EnableKeyRepeat](http://sdl.beuc.net/sdl.wiki/SDL_EnableKeyRepeat) ([http://sdl.beuc.net/sdl.wiki/SDL\_EnableKeyRepeat](http://sdl.beuc.net/sdl.wiki/SDL_EnableKeyRepeat)) exaplains a little bit about it.

> not really sure about the delay in repeating in joysticks.
> 
> 2014-03-21 3:31 GMT-07:00 Dark\_Oppressor \<DarkOppressor at [gmail.com](http://gmail.com) (DarkOppressor at [gmail.com](http://gmail.com))\>:
> 
> > ```
> > So, I'm currently checking for SDL_KEYDOWN events to navigate around in a menu in my game. If I hold down the 'down' key, I can move quickly down through the menu, due to key repeat. This is what I want to happen, but I am curious how exactly key repeat works/is controlled in SDL2.
> > 
> > ```
> > 
> > So, how does it work exactly?
> > 
> > Also, is there any way to enable the same kind of repeating for controller buttons? I looked in the SDL\_ControllerButtonEvent struct and didn’t see anything about repeat, which I did see in SDL\_KeyboardEvent. I’m guessing there is no such option, but I thought I’d ask. Thanks!
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [lists.libsdl.org](http://lists.libsdl.org) (SDL at [lists.libsdl.org](http://lists.libsdl.org))  
> > [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org) ([http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org))
> 
> –  
> Javier Flores

---

<div class="post-metadata">

**Author:** ![Javier\_Flores](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/javier_flores/32/2577_2.png) [@Javier\_Flores](https://discourse.libsdl.org/u/Javier_Flores)\
**Post date:** [March 22, 2014, 2:13am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/4 "2014-03-22T02:13:36Z")

</div>

oh yes, mmm i guess sdl2 now uses the OS settings?, not sure. or maybe the  
holding key is just passed as nothing and always is 0 time between  
repeating. so its up to you to code flags and timers along  
SDL\_GetKeyboardState(). to know the state of the keys… not really sure  
tho.

2014-03-21 18:57 GMT-07:00 Dark\_Oppressor :\> But that is for SDL 1.2, isn’t it? I’m working with SDL2, which doesn’t

> seem to have SDL\_EnableKeyRepeat. Unless I’m missing something?
> 
> javierecf wrote:
> 
> [http://sdl.beuc.net/sdl.wiki/SDL\_EnableKeyRepeat](http://sdl.beuc.net/sdl.wiki/SDL_EnableKeyRepeat) exaplains a little bit  
> about it.
> 
> not really sure about the delay in repeating in joysticks.
> 
> 2014-03-21 3:31 GMT-07:00 Dark\_Oppressor \<\>:
> 
> Quote:
> 
> So, I’m currently checking for SDL\_KEYDOWN events to navigate around in  
> a menu in my game. If I hold down the ‘down’ key, I can move quickly down  
> through the menu, due to key repeat. This is what I want to happen, but I  
> am curious how exactly key repeat works/is controlled in SDL2.
> 
> So, how does it work exactly?
> 
> Also, is there any way to enable the same kind of repeating for controller  
> buttons? I looked in the SDL\_ControllerButtonEvent struct and didn’t see  
> anything about repeat, which I did see in SDL\_KeyboardEvent. I’m guessing  
> there is no such option, but I thought I’d ask. Thanks!
> 
> * * *
> 
> SDL mailing list
> 
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)
> 
> –  
> Javier Flores
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

–  
_Javier Flores_

---

<div class="post-metadata">

**Author:** ![mogumbo](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/mogumbo/32/704_2.png) [@mogumbo](https://discourse.libsdl.org/u/mogumbo)\
**Post date:** [March 22, 2014, 4:39am UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/5 "2014-03-22T04:39:44Z")

</div>

> But that is for SDL 1.2, isn’t it? I’m working with SDL2, which doesn’t seem to have SDL\_EnableKeyRepeat. Unless I’m missing something?

Look in the SDL\_KeyboardEvent struct in SDL\_events.h:

Uint8 repeat; /\*\*\< Non-zero if this is a key repeat \*/

I haven’t used it myself. Give it a try.–  
Terry Welsh

> **[Really Slick - Indie game dev and classic 3D screensavers](http://www.reallyslick.com)**
>
> The indie game Retrobooster is on its way. It's an old-school 2D shooter with 3D graphics, explosions, monsters, fast flying, and more explosions. As evil aliens attempt to lay waste to the galaxy, fight your way through dozens of worlds with an...

---

<div class="post-metadata">

**Author:** ![Daniel\_Gibson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/daniel_gibson/32/3010_2.png) [@Daniel\_Gibson](https://discourse.libsdl.org/u/Daniel_Gibson)\
**Post date:** [March 22, 2014, 5:05pm UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/6 "2014-03-22T17:05:00Z")

</div>

I think it uses the OS settings.  
Not sure what you mean with “holding key is just passed as nothing”, but  
key repeats do generate multiple SDL\_KEYDOWN events and event.key.repeat  
is != 0 for the repeated events.  
You’ll only get an SDL\_KEYUP once (when the key is not pressed anymore),  
not for each repeat.

Cheers,  
DanielAm 22.03.2014 03:13, schrieb Javier Flores:

> oh yes, mmm i guess sdl2 now uses the OS settings?, not sure. or maybe  
> the holding key is just passed as nothing and always is 0 time between  
> repeating. so its up to you to code flags and timers along  
> SDL\_GetKeyboardState(). to know the state of the keys… not really  
> sure tho.
> 
> 2014-03-21 18:57 GMT-07:00 Dark\_Oppressor \<DarkOppressor at [gmail.com](http://gmail.com)  
> \<mailto:DarkOppressor at [gmail.com](http://gmail.com)\>\>:
> 
> ```
> __
> But that is for SDL 1.2, isn't it? I'm working with SDL2, which
> doesn't seem to have SDL_EnableKeyRepeat. Unless I'm missing something?
> 
> javierecf wrote:	
> 
> http://sdl.beuc.net/sdl.wiki/SDL_EnableKeyRepeat exaplains a little
> bit about it.
> 
> not really sure about the delay in repeating in joysticks.
> 
> 2014-03-21 3:31 GMT-07:00 Dark_Oppressor <>:
> 
> Quote:	
> 
> So, I'm currently checking for SDL_KEYDOWN events to navigate around
> in a menu in my game. If I hold down the 'down' key, I can move
> quickly down through the menu, due to key repeat. This is what I
> want to happen, but I am curious how exactly key repeat works/is
> controlled in SDL2.
> 
> So, how does it work exactly?
> 
> Also, is there any way to enable the same kind of repeating for
> controller buttons? I looked in the SDL_ControllerButtonEvent struct
> and didn't see anything about repeat, which I did see in
> SDL_KeyboardEvent. I'm guessing there is no such option, but I
> thought I'd ask. Thanks!
> 
> _______________________________________________
> SDL mailing list
> 
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> 
> 	
> 
> --
> Javier Flores	
> 
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> 
> ```
> 
> –  
> /Javier Flores/
> 
> * * *
> 
> SDL mailing list  
> SDL at [lists.libsdl.org](http://lists.libsdl.org)  
> [http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org](http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org)

---

<div class="post-metadata">

**Author:** ![Dark\_Oppressor](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@Dark\_Oppressor](https://discourse.libsdl.org/u/Dark_Oppressor)\
**Post date:** [March 23, 2014, 8:43pm UTC](https://discourse.libsdl.org/t/key-button-repeat/20529/7 "2014-03-23T20:43:58Z")

</div>

OK, thanks guys!
