Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (653)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Parallel Keyboard Inputs  (Read 1763 times)
0 Members and 1 Guest are viewing this topic.
Offline Wolfner

Senior Newbie

« Posted 2010-06-26 20:54:24 »

Hi there,

I'm working on my first Java game at the moment (which is a sidescroller like R-Type) and I have a little problem:

I added a KeyAdapter to my JFrame in order to move the spaceship on the screen.
Actually that works fine but with this approach I can only do one thing at the same time. So I can only move the ship OR shoot but i can't move the ship AND shoot at the same time.
I thought about parallelizing the input with threads somehow but I really have no clue how to do this.
Any ideas?


This is how my KeyAdapter looks at the moment:

private KeyAdapter keyAdapter1 = new KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent e) {
         if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
         } else if (e.getKeyCode() == KeyEvent.VK_UP) {
         }else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
         }else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
         }else if(e.getKeyCode() == KeyEvent.VK_SPACE){
      public void keyReleased(java.awt.event.KeyEvent e) {

Click to Play
Offline zoto

Senior Devvie

Medals: 4

« Reply #1 - Posted 2010-06-26 21:53:34 »

A standard approach is to store all the key strokes in a boolean array using the integer returned from e.getKeyCode() as the index. A quick search of these forums came up with this post but I'm sure there is others.

Then in your update method of your ship you can check for each key you care about and handle it.
Offline Wolfner

Senior Newbie

« Reply #2 - Posted 2010-06-26 23:59:29 »

Oh, much simpler than i thought.
Thanks a lot!

Click to Play
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wolfner

Senior Newbie

« Reply #3 - Posted 2010-06-30 00:05:35 »

Hi again,

I implemented the controls and it worked out so far but I have one problem:
I can go in all 8 directions while firing (3-button combination) but one!
If I go "Up-left" the application just bleeps once and the ship doesn't move anywhere.


Click to Play
Offline OverKill

Junior Devvie

Java games rock!

« Reply #4 - Posted 2010-06-30 07:32:49 »

Yes, the boolean array is ok, but one thing you have to watch out for might be presses that are to quick to catch.
During my mobile dev days I did a similar approach and had this problem.
The problems happens when you store the key presses/releases and then in the main loop, poll the array. By the next iteration, they key once pressed might have been unpressed.

The alternative is to store presses and releases in different arrays. Then after each processing in the main loop, untoggle the pressed by checking if it had been released.

 if ( keyreleased[i] ) keypressed[i] = keyreleased[i] false;

This way you can even catch those quick presses.

Also, and this is why it was a problem with mobiles, some phones did not support repeats and some would not register a release if you were pressing two keys at once.

(posted this out of memory, I hope it is not failing me!)
Offline Wolfner

Senior Newbie

« Reply #5 - Posted 2010-06-30 11:03:24 »

Thanks a lot!
Just one further question:
Is there any way to override the delay between "KeyTyped" and "KeyPressed"?
(For example if you want to write "BBBBBBBBBBBBBBBBB", then there is a delay between the first B and the remaining Bs)

Click to Play
Offline Riven
« League of Dukes »

« JGO Overlord »

Medals: 1020
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #6 - Posted 2010-06-30 12:11:23 »

That delay is OS dependent (and configurable) but you shouldn't really be worrying about that.

Simply give your *weapon* a timestamp of when it was last fired, and give that weapon an interval between shots. Each gametick you check the weapon.canFire(), checking the whether currentTime > lastShotTime + weaponInterval, and if it returns true, call

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline zege

Junior Newbie

« Reply #7 - Posted 2010-06-30 13:42:32 »

Timestamps might make the game act wierd, rather use a counter ;-)
Offline Riven
« League of Dukes »

« JGO Overlord »

Medals: 1020
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #8 - Posted 2010-06-30 19:47:01 »

Timestamps might make the game act wierd, rather use a counter ;-)

True, but only in a fixed rate engine update.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (28 views)
2015-08-01 03:58:20

Jesse (20 views)
2015-07-29 04:35:27

Riven (40 views)
2015-07-27 16:38:00

Riven (22 views)
2015-07-27 15:35:20

Riven (24 views)
2015-07-27 12:26:13

Riven (15 views)
2015-07-27 12:23:39

BurntPizza (36 views)
2015-07-25 00:14:37

BurntPizza (46 views)
2015-07-24 22:06:39

BurntPizza (31 views)
2015-07-24 06:06:53

NoxInc (37 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!