I may be doing this in a completely horrible way, but my idea for my game engine is to create a list that a user can add a KeyMap object to. So, I have a KeyMap class and a KeyMaps class, as shown:KeyMap

As you can see, my idea is for the user to be able to create a set of KeyListeners to handle KeyEvents. The KeyMaps class would hold this list while maintaining a single active KeyMap, and the ability to easily switch between them (So, you can have a different KeyMap for gameplay, the main menu, mini-games, etc.) My issue is that, I don't think this works for a reason I'm not aware of. I created a simple KeyMap that exits the program if the use presses esc:

EDITED: It's a bit of a weird build you've done, and you'll have to keep changing the keymap associated with your canvas, but that should work, assuming you make a getter for the currently active keymap.

Your solution worked, though, I needed to add the listener to the screen, not the canvas. Though, that is trivial.So, is there not a way to add the listeners to the KeyMaps object? Like my code shows? Or is it necessary that the Screen object have it? Due to the focus, I suppose.

Exactly, the focus is controlled by the actual "surface" you're using, and it is the KeyListener on that, which is activated.

EDIT2: Wow, now I read it again. You're assigning a new KeyListener for each key you want bound, if I'm reading this correctly. You shouldn't do that. A KeyListener can report to you which keys are pressed on the entire keyboard, not just for a single key.

Check this and the 2 next videos out. He explains it quite slowly, and the videos can't be followed as such because they assume you've followed the 27 preceeding videos, but just looking at what he does, should get you the right idea about how this works.Key elements are:- He has a class that implements the KeyListener.- He makes an instance of this class, and sets it to be the KeyListener on his canvas, window or frame, depending on what you're doing.- Then he can use his KeyListener class to check whether some key is down- You should not have your game-class implement KeyListener like he does in this example. You can, but it is easier to abstract it from the code.

I did it like this in my first game, where my Game-class implemented KeyListener:

Exactly, the focus is controlled by the actual "surface" you're using, and it is the KeyListener on that, which is activated.

EDIT2: Wow, now I read it again. You're assigning a new KeyListener for each key you want bound, if I'm reading this correctly. You shouldn't do that. A KeyListener can report to you which keys are pressed on the entire keyboard, not just for a single key.

That's not correct. Each KeyMap is designed to layout the functionality of the entire keyboard. The reason for a list of them is to easily switch between interfaces while changing what the keys do. Like in my example before, you could have your keys mapped for the game play, and then have a different mapping for when you are navigating menus. Stuff like that.

I'm trying to design the engine in a way where that option is available, but not necessary. Like, the SysemKeyMap that is applied by default could be overwritten and used as the only Listener for the whole game.

Exactly, the focus is controlled by the actual "surface" you're using, and it is the KeyListener on that, which is activated.

EDIT2: Wow, now I read it again. You're assigning a new KeyListener for each key you want bound, if I'm reading this correctly. You shouldn't do that. A KeyListener can report to you which keys are pressed on the entire keyboard, not just for a single key.

That's not correct. Each KeyMap is designed to layout the functionality of the entire keyboard. The reason for a list of them is to easily switch between interfaces while changing what the keys do. Like in my example before, you could have your keys mapped for the game play, and then have a different mapping for when you are navigating menus. Stuff like that.

I'm trying to design the engine in a way where that option is available, but not necessary. Like, the SysemKeyMap that is applied by default could be overwritten and used as the only Listener for the whole game.

I was hoping that was the case I really shouldn't read through other people's code at 5 in the morning ^^

That does seem a lot simpler. I know my implementation is kind of crude, as far as the actual key presses go. My main concern at the moment was properly implementing my KeyMapping system. Though, I'll likely be using this method for my actual KeyMap class. Thanks for that!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org