Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  InputHandler help ?  (Read 1683 times)
0 Members and 1 Guest are viewing this topic.
Offline PixelDeBurner

Junior Devvie


Medals: 1



« Posted 2012-08-03 23:43:27 »

Hello guys, I just registered here, to ask you a question.

I'm trying to make input handler, and I'm using keys[e.getKeyCode] etc for it.

My question is, how to make isKeyPressed method, where it detects if key is pressed, but returns true just once ?

For example, I have int variable number. When I press and hold key space, it should add 1 to number, but only once, no matter how long i hold the spacebar.

I hope you can help me !
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-08-03 23:50:39 »

Hey PixelDeBurner and welcome to JGO!

The best way to do this is to put all key codes you get from the keyPressed method into a HashSet, removing them in keyReleased.

Then it's a matter of calling keyCodeSet.contains(...) to check if the key has been pressed.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
HashSet<Integer> keyCodeSet = new HashSet<Integer>();

public void keyPressed(KeyEvent key) {
    keyCodeSet.add(key.getKeyCode());
}

public void keyReleased(KeyEvent key) {
    keyCodeSet.remove(key.getKeyCode());
}

public boolean isKeyPressed(int key) {
    return keyCodeSet.contains(key);
}

Offline PixelDeBurner

Junior Devvie


Medals: 1



« Reply #2 - Posted 2012-08-03 23:52:01 »

How does this way effect performance, I read somewhere something about Set, that it's a bit slower ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PixelDeBurner

Junior Devvie


Medals: 1



« Reply #3 - Posted 2012-08-03 23:55:21 »

That doesn't work... what it does is the same as keyDown, I want to test is keyPressed, but return true just once !
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-08-04 01:41:45 »

So you want it to only return true once? You can add change isKeyPressed to:
1  
2  
3  
4  
5  
public boolean isKeyPressed(int key) {
    boolean isPressed = keyCodeSet.contains(key);
    keyCodeSet.remove(key);
    return isPressed;
}

Offline PixelDeBurner

Junior Devvie


Medals: 1



« Reply #5 - Posted 2012-08-05 23:39:39 »

Nope, doesn't work ...
Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #6 - Posted 2012-08-06 00:05:06 »

Depends on your operating system. The KeyDown and KeyTyped events are sometimes repeated, sometimes not. Some OSs have a pause before the repeat, others don't. Just test it and see.

Storing the last key pressed and checking if it is still down should be enough, though.

Offline jonjava
« Reply #7 - Posted 2012-08-06 06:08:56 »

AFAIK KeyPressed and KeyReleased triggers only once (keyTyped triggers multiply times). But you could have a boolean value check whether the key has been released before it can be used again.

Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #8 - Posted 2012-08-06 09:23:24 »

AFAIK KeyPressed and KeyReleased triggers only once (keyTyped triggers multiply times).

As I said, it depends on how the OS handles the keyboard input.

Best way to find out is to simply add a System.out.println() message to the Key Listener method for each event, and see how it behaves.

In my case, on a WidowsXP machine, the KeyPressed and KeyTyped events keep repeating if I hold the key down.

Offline PixelDeBurner

Junior Devvie


Medals: 1



« Reply #9 - Posted 2012-08-06 15:45:45 »

I managed to do it !!!

here is how:

Made boolean keyUp[keyCode]

if key is pressed, and it's up set the keyUp to false, and return true.

the Code :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public boolean isKeyPressed(int key) {
      if (keyDown[key]) {
         if (keyUp[key]) {
            keyUp[key] = false;
            return true;
         } else
            return false;
      } else
         return false;
   }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #10 - Posted 2012-08-06 18:50:43 »

That makes no sense whatsoever. How can a key be down and up at the same time? How do you set these down and up keys?

Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-08-06 20:44:01 »

I don't see a lot of point in an array of keys that are up.  Most keys are up all the time.  Makes more sense to simply have a single "keysDown" set and update it on every keyUp and keyDown event.
Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #12 - Posted 2012-08-06 21:27:33 »

I think his problem is actually checking when the key changes state, so a single pulse is sent regardless of the key being kept down or up.

If the validation is done against a single flag, the pulse will be sent continually once the flag is set.

Maybe have a hasChanged boolean that is set once the key changes, and reset on the next frame (would need an updateKeys method call each frame)

Offline ra4king

JGO Kernel


Medals: 356
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-08-06 21:44:19 »

My earlier post of resetting the key should work too.

Offline PixelDeBurner

Junior Devvie


Medals: 1



« Reply #14 - Posted 2012-08-06 22:45:46 »

It's like, if the key is down, but was not ( keyUp )...

I tried other things also, this one worked...
Offline Oskuro

JGO Knight


Medals: 40
Exp: 6 years


Coding in Style


« Reply #15 - Posted 2012-08-07 09:15:49 »

It can get a bit confusing sometimes. Good thing is, if it works, you can always refine it in the future.

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.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!