Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Super easy KeyAdapter question  (Read 2018 times)
0 Members and 1 Guest are viewing this topic.
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Posted 2008-05-29 02:04:27 »

Greetings!

I'm trying to write a KeyAdapter which will set values in a boolean array to true is that key is down, and false when they are up.  My code looks like this:

Quote

private boolean[] keys;

...

// some other code


addKeyListener( new KeyAdapter() {
   public void keyPressed(KeyEvent e)
   {
      keys[e.getKeyCode()] = true;
   }
         
   public void keyReleased(KeyEvent e)
   {
      keys[e.getKeyCode()] = false;
   }
} );

This way, I can worry about when to do with key presses in my main game loop.  My question is, what size should I make the array keys?  Right now, I guessed at 130, but is that reasonable?

Thanks in advance.

Hello!
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #1 - Posted 2008-05-29 03:44:43 »

Some keys (e.g. win+menu) have a keycode of 65536.

Use a size of 256 and keep the values in range with bitwise-and.

Like this:

keys[e.getKeyCode()&0xff] = true;

弾幕 ☆ @mahonnaiseblog
Offline DavidW

Junior Member


Medals: 3
Exp: 7 years



« Reply #2 - Posted 2008-05-29 06:25:18 »

Hey, thanks for the tip.  Smiley

Hello!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #3 - Posted 2008-06-26 18:28:48 »

If tracking the boolean on/off state of keys is sufficient for the type of game you are writing, you might want to go for a more complete solution.

Obviously it's more expensive, but as this code will not be a performance bottleneck - who cares. (though if so inclined, you could replace the HashSet<Integer> with a more performant proprietary IntHashSet implementation)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
private HashSet<Integer> keys = new HashSet<Integer>();


// note, HashSet is not Thread-safe, hence the need for synchronization.
public synchronized boolean isKeyDown(int keyCode) {
   return keys.contains(keyCode);
}

...

// some other code


addKeyListener( new KeyAdapter() {
   public synchronized void keyPressed(KeyEvent e)
   {
      keys.add(e.getKeyCode());
   }
         
   public synchronized void keyReleased(KeyEvent e)
   {
      keys.remove(e.getKeyCode());
   }
} );

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2008-06-26 19:18:58 »

I usually use an ArrayList of buttonsPressed, because it's rare you're pressing more than a few buttons at a time and therefore the adding operation is cheaper.

See my work:
OTC Software
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #5 - Posted 2008-06-27 03:20:27 »

I usually use an ArrayList of buttonsPressed, because it's rare you're pressing more than a few buttons at a time and therefore the adding operation is cheaper.

Indeed. Though I hope you are synchronizing access to the ArrayList? (assuming you are querying it from a Thread other than the EDT)

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #6 - Posted 2008-06-27 07:14:22 »

I've actually never bothered with synchronizing it. Probably should have!

See my work:
OTC Software
Offline cylab

JGO Ninja


Medals: 38



« Reply #7 - Posted 2008-06-27 18:52:18 »

Before you pull your hair out: AWT keyboard events are unreliable with multiple keys pressed due to key repeat. It tends to "forget" KeyReleased events, so you will probably have the problem with "sticking" keys :/

Mathias - I Know What [you] Did Last Summer!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #8 - Posted 2008-06-28 03:10:37 »

Before you pull your hair out: AWT keyboard events are unreliable with multiple keys pressed due to key repeat. It tends to "forget" KeyReleased events, so you will probably have the problem with "sticking" keys :/
I actually never have had that problem, even without synchronicity.

See my work:
OTC Software
Offline fruitmaze

Senior Member


Medals: 3
Projects: 2



« Reply #9 - Posted 2010-06-27 18:18:40 »

Before you pull your hair out: AWT keyboard events are unreliable with multiple keys pressed due to key repeat. It tends to "forget" KeyReleased events, so you will probably have the problem with "sticking" keys :/
I have had that problem as well a few times. Does anyone have any solution for that?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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
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!