Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1398 times)
0 Members and 1 Guest are viewing this topic.
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Posted 2004-04-26 09: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
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #1 - Posted 2004-04-26 10: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
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #2 - Posted 2004-04-26 10: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!
Play the free demo of Revenge of the Titans!
Offline endolf
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #3 - Posted 2004-04-26 10: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
« League of Dukes »

JGO Coder


Projects: 3



« Reply #4 - Posted 2004-08-06 12: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
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #5 - Posted 2004-08-12 10: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
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #6 - Posted 2004-08-12 10: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
« League of Dukes »

JGO Coder


Medals: 4
Projects: 1


Current project release date: sometime in 3003


« Reply #7 - Posted 2004-08-12 10: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 17:32:52 »

kev, could post your solution here?
Offline kevglass
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #9 - Posted 2004-08-12 17: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (129 views)
2013-05-17 21:29:12

alaslipknot (136 views)
2013-05-16 21:24:48

gouessej (167 views)
2013-05-16 00:53:38

gouessej (159 views)
2013-05-16 00:17:58

theagentd (171 views)
2013-05-15 15:01:13

theagentd (156 views)
2013-05-15 15:00:54

StreetDoggy (200 views)
2013-05-14 15:56:26

kutucuk (224 views)
2013-05-12 17:10:36

kutucuk (223 views)
2013-05-12 15:36:09

UnluckyDevil (227 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.148 seconds with 21 queries.