New answers tagged


Instead of having the nextString method in the key event method, have it in a update method. Similar to the comment, it depends on the OS, so they won't always be the same. This might have sounded confusing, here's an example: public void keyPressed(KeyEvent evt) { if(evt.getKeyCode() == KeyEvent.VK_LEFT) { player.runMotion = -1; } ...


An alternative way (Josh's approach is great too!) would be to setup an boolean on SDL_KEYDOWN, and possibly also ignoring all repeated key events. That you can do by checking the repeat member of the key event. Then you could implement your own timer, which doesn't have to be anything fancy, and implement key repetition your self. You could either trigger ...


By waiting for key-down events to be fired, you are likely at the mercy of the key event repeat rate that the OS controls (and which users can specify themselves). Instead, you may want to call SDL_GetKeyboardState at the top of your game update loop (the part of the update that happens every frame, whether or not an event has come in) to get the state of ...


SDL2 actually generates key repeat events automatically. You need to manually filter repeated key events out by checking the repeat member of a SDL_KeyboardEvent. This is something that did not automatically happen in SDL1, so that's why the behaviour is different. See SDL_KeyboardEvent for more info.

Top 50 recent answers are included