We changed our privacy policy. Read more.
10

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 ...


8

Firstly, you need to use a switch case statement and decide which direction overrides the others, for example, if they press all four buttons, which button should be listened to for input? This gets put in order within the switch statements. Edit: For clarity about the above statement. You do need to use a switch case statement (or similar structural logic) ...


6

In your keyPressed and keyReleased you can use a Map<Integer, GameInput> to map the KeyEvent.VK_* to GameInput Make a new enum with the actions you want to be controllable enum GameInput{ FORWARD, LEFT, RIGHT, BACK,PAUZE,... } And in Controller you have a Map<Integer, GameInput> that you use: public void keyPressed(KeyEvent e) { game....


6

To answer your first Question "why does the Engine behave like this" I would add the following: The Engine does not actually detect the key being pressed multiple times. What you need to keep in mind here is that the "was pressed" state of the key does not reset until the next frame, and your code will be executed in a single frame. Furthermore, every if ...


6

This is an excellent question because you are experiencing a problem that's quite commonplace with less experienced programmers: attempting to solve your problem with a purely imperative approach. Luckily this problem has been encountered and solved by people much smarter than I, thanks to the mathematical concept of a finite state machine. A finite state ...


5

The best way to solve your concerns for flexibility and comfort of the user is to certainly allow them to define their bindings as you have described. Whether you opt to do this through a configuration file they manually edit or an in-game screen that allows them to select the action and then press the key configurations for said binding is entirely up to ...


5

Your issues does not seems to be "How do I accept only one directional input per update?" but rather "How do I prevent my snake to go backward?" From the many comments and discussion, you seem to be having a frame-rate that is not appropriate for the game you're developing. Traditionally, Snake is a game with a very low frame-rate, the snake moved only 2-3 ...


5

It is due to your flow is confusing for engine as it will try to execute your every check in every frame. Try to implement with else or with single statement like, using UnityEngine; using System.Collections; public class TestingKeyGetRecursion : MonoBehaviour { int counter = 1; void Update () { if (Input.GetKeyDown(KeyCode.F)) ...


5

Just as a heads up, most keyboards have a finite limit of keys that can be pressed. For example, I have an Asus Transformer Book and you can only press around ~5 keys at a time. Some keyboards also won't allow more than a few keys pressed if shift or control is being pressed. (Like my Transformer Book...) And yes, most of the time, this is hardware limited, ...


5

From your recapitulation this might be an key rollover problem. The term key rollover describes a tradeoff in keyboard design complexity for reduced production cost. Many standard keyboards have issues detecting more than three keys being pressed simultaneously. As you are using Swing, that means you are using java. So in order to find out if it is a ...


4

Behind the scenes, typically input is polled anyway, just by the os behind your back ;) Regardless, even if you poll you're going to need to package state changes up in some way in order to use that information elsewhere in your game. And odds on you're going to package that up in an event. So realistically, going straight to events is going to save you ...


4

Polling mechanism The software check for a condition repeated times until something is ready. E.g. Childs in a car: Child: Did we arrived yet? Mom: No; Child: Did we arrived yet? Mom: No; .... (repeat many times) Child: Did we arrived yet? Mom: Yes Events The condition itself is able to inform the software when it is ready. E.g. ...


4

You may be dealing with keyboard ghosting, the phenomenon where certain keys cannot be pressed down simultaneously. This differs between different keyboards, but you can test it out in the link I've provided, which has an interactive program for testing key presses. You may be able to fix this problem by finding different keys that can be pressed together, ...


4

A summary Instead of mapping the input direcly to the action (and using enums will keep doing that because you would still have to map the key to the enums), you should make the input consume a method it holds. If it was an X-Box, for example, the X button can perfom an action defined by the player Use the inferface above to create something like bellow ...


4

Try this: @Override public void keyboardFocusChanged(FocusEvent event, Actor actor, boolean focused) { super.keyboardFocusChanged(event, actor, focused); if (!focused) setOnscreenKeyboardVisible(false); } Otherwise you could go for a better work around. You place a big clickable transparent actor behind everything. When you click anywhere ...


4

Good news! This code: bool ctrl = Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl); already respects the user's OS settings for sticky keys and key remapping on Windows and macOS at least. And this works whether you specify the key by KeyCode, by a "Conventional Game Input" string, or by a remappable GetButton("Some Custom ...


3

Let the user configure the bindings from an options menu in the application. Players might be using QWERTZ (German) or AZERTY (French) layout keyboards, Dvorak layout keyboards, joysticks, etc., and they don't want to have to learn Yet Another Config File Language. But still, a lot of players will want to jump in and play without changing any settings. So ...


3

Most big budget games (those with an international distributor) will work out of the box, using ZQSD instead of WASD for azerty layouts. A decent game will also introduce the layout in the first level, others will let the player discover it through the customisation screen. The implementation strategy can be detected by asking the OS to switch layouts. ...


3

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.


3

You need a Listener indeed. Then you can store the pressed keys in an array, and manage it like this. bool keyPressed[256]; // Do not forget to initialize all to false if java does not do it; onKeyPressed(event e) { keyPressed[e.keyCode] = true; } onKeyReleased(event e) { keyPressed[e.keyCode] = false; } // in game loop you can then check like ...


3

This could be a hardware issue as stated by @Quentin. Anyways, you could write a workaround tracking the KEYDOWN and KEYUP events instead of trying to fetch the keyboard state, i'm not pretty sure how it works in C#, i've never worked with SDL2 in that language, but here's an example of how do I manage it in C++, i suppose it's pretty similar. Instead of ...


3

You can use the built-in method Input.GetAxisRaw From the docs: Returns the value of the virtual axis identified by axisName with no smoothing filtering applied. The value will be in the range -1...1 for keyboard and joystick input. Since input is not smoothed, keyboard input will always be either -1, 0 or 1. This is useful if you want to do ...


3

glutSpecialFunc is used to register a callback handler for special keys; as it's documentation notes: The special keyboard callback is triggered when keyboard function or directional keys are pressed. For all other non-special keys you should register a second callback using glutKeyboardFunc, then just test for 'w', 'a', 's', 'd', etc in your code. Again, ...


3

I think what was happening is that SDL2 would pollevent once and not again if the key being pressed is still the same. If it's the same it would default to event type 771 (I couldn't find on the internet what that corresponds to). So I added the code to make the program run sdl_pollevent once more if it gets the code 771. Solved the problem: if ...


3

You're mixing two methods of input detection SDL provides. There is the event API which lets you get input via events, and there is the keyboard state API, which allows you to get the state of the entire keyboard with a single function and query it later. You're mixing them, and it's not working well. In your code, you're detecting an SDL_KEYDOWN event ...


3

With "Shortcut", there is the implication that there is a more intuitive but also more elaborate way to perform the action (like navigating through a menu). "Hotkey" is a more general term which can also cover actions which can only be performed with that keypress. But in general, these two terms are pretty interchangeable. Another term you can use to ...


Only top voted, non community-wiki answers of a minimum length are eligible