Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (496)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Keylistener on Linux  (Read 1854 times)
0 Members and 1 Guest are viewing this topic.
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Posted 2004-04-26 07:45:40 »

I couldn't find the old post about this, but wasn't there an issue where on windows with a keylistener you'd get

KeyPressed
-
-
-
KeyReleased

But on linux with key repeat you'd get

KeyPressed
KeyReleased
KeyPressed
KeyReleased
KeyPressed
KeyReleased

which cause lots of annoying issues. Did anyone come up with a work around for the issue?

Kev

Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #1 - Posted 2004-04-26 08:28:18 »

The only thing i've seen is where people write a wrapper over the top and have a delay, so the keyListener i listened to by another key listener, and that is the one the game listens too, that one, when it gets the first keyPressed starts a timer, if it doesn't get another keyPressed in the keyboard repeat time, then *and only then* send the keyReleased even.

HTH

Endolf

Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2004-04-26 08:39:42 »

I think I prefer my current workaround  :-/

When the key released event comes in, I just peek() on the AWT event queue for the next KeyPressed event. If its for the same key code at the same time then ignore the key released.

This however seems a bit ugly.

Kev

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

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #3 - Posted 2004-04-26 08:59:29 »

Aye, sounds like a better solution. Still not ideal, but them's the breaks. Probably find osX does it like linux, but don't count on it Smiley

Endolf

Offline TheAnalogKid

JGO Coder


Projects: 2



« Reply #4 - Posted 2004-08-06 10:53:58 »

Quote
I think I prefer my current workaround  :-/

When the key released event comes in, I just peek() on the AWT event queue for the next KeyPressed event. If its for the same key code at the same time then ignore the key released.


Well I tried your idea but it wasn't successfull for me. Here is my code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
    eventQueue = gameWindow.getToolKit().getSystemEventQueue();
    ...
    public void keyReleased(KeyEvent e) {
          AWTEvent event = eventQueue.peekEvent();
          if (event != null && event instanceof KeyEvent && ((KeyEvent) event).getKeyCode() == keyPressedCode) {
                return;
          }
          keyReleasedCode = e.getKeyCode();
    }


Where keyPressedCode has been stored in previous keyPressed() method invocation.

Why is it not working?

Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #5 - Posted 2004-08-12 08:37:55 »

I see another potential problem with this solition. Imagine I am using VK_UP as my 'move forwards' and VK_Z as my fire key. If I want to move forwards whilst continuously fireing, the player would hold both keys down.

so in the event key you get

1  
VK_UP pressed VK_Z pressed VK_UP released VK_Z released VK_UP pressed VK_Z pressed VK_UP released VK_Z released


now, when you get to the VK_UP released it's fine, scan for the next KEY_PRESSED and you get VK_UP, so you ignore it, then you get the VK_Z released and check, but unless you can consume the VK_UP pressed event, you'll find that again.  Is it possible/wise to start consuming events that arn't at the front of the queue and have not been dispatched. Possible is easy to solve, try it (which I plan on doing, but not now), wise is another issue. What are the implications of removing a key pressed even, noting that when you are doing it, some things may have already been notified of the key released, and others may not (who knows where you are in the chain of listeners).

Just some thoughts.

Endolf

Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2004-08-12 08:43:43 »

Actually, I don't think you get that with multiple keys held down. Only one repeats (I think its actually caused by standard key repeat?)..

So you actually get..

VK_UP pressed
VK_Z pressed
<pause for a bit>
VK_Z release
VK_Z pressed
VK_Z release
VK_Z pressed
VK_Z release
VK_UP release

Kev

Offline endolf

JGO Coder


Medals: 7


Current project release date: sometime in 3003


« Reply #7 - Posted 2004-08-12 08:52:47 »

Hmm, interesting, I feel some testing comming on tomorrow Smiley

Endolf

Offline luisoft

JGO Coder


Projects: 6


Java games rock!


« Reply #8 - Posted 2004-08-12 15:32:52 »

kev, could post your solution here?
Offline kevglass

JGO Kernel


Medals: 164
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2004-08-12 15:46:37 »

The class I use to wrap keyboard input is actually available as part of the SpaceInvaders 103 tutorial, heres the class:

http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../spaceinvaders103/org/newdawn/spaceinvaders/util/Keyboard.java

the bit in particular that I did to get round linux was:

1  
2  
3  
4  
5  
6  
7  
8  
public void keyReleased(KeyEvent e) {
         KeyEvent nextPress = (KeyEvent) Toolkit.getDefaultToolkit().getSystemEventQueue().peekEvent(KeyEvent.KEY_PRESSED);
              
          if ((nextPress == null) || (nextPress.getWhen() != e.getWhen())) {
                keys[e.getKeyCode()] = false;
         }
              
}


Please understand, I'm not saying this is the only way to do it but it has worked for me in the past quite happily.

Kev

PS. I'm really thinking about adding a signature like NeHe's:

Quote

I am not a guru programmer. I am an average programmer, learning new things.. etc..


but I assuming most people can tell that from the state of my code Smiley

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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (32 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (80 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (46 views)
2014-09-07 01:13:42

Longarmx (33 views)
2014-09-07 01:12:14

Longarmx (37 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!