Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  A question out of curiousity  (Read 1945 times)
0 Members and 2 Guests are viewing this topic.
Offline Del-ONE

JGO n00b
*

Posts: 39



« on: 2008-12-08 01:37:51 »

I just ran into something that might be strange.

Since switching over to ubuntu, I've haven't done anything directly within a KeyListeners methods, so this is the first chance I've had to notice something that is possibly different from java under Windows.  The thing is; I can't check it myself.

When I run the program and hold down a key, the system begins firing both keyPressed() and keyReleased() continuously.  However, the way I remember it from the last time I needed to know this (under Windows), the system would only keep performing keyPressed() while the button was held, and its seems like some old code is meant to work like that (and it did do what I needed it too).  So I'm wondering if this really is a difference, or am I just insane?
Offline anemian

JGO n00b
*

Posts: 3



« Reply #1 on: 2008-12-09 21:16:58 »

Yes it's different from what it does on all the window machines I use.
Offline cghislai

JGO n00b
*

Posts: 7



« Reply #2 on: 2008-12-23 19:26:55 »

Youre right.

If I remember, its KDE wich fires these KeyPressed/KeyReleased event for autorepeat.
You can use something like this to know how long a key has been hold down :
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
protected long leftKeyTimer = 0;
protected long leftKeyFinalTimer = 0;

// On Key Press Event:
if (leftKeyTimer == 0) {
    leftKeyTimer = System.currentTimeMillis();
}
// On Key Release Event:
leftKeyFinalTimer += (System.currentTimeMillis() - leftKeyTimer);
leftKeyTimer = 0;

// When you update your game logics:
if (leftKeyTimer != 0) {
    leftKeyFinalTimer += (System.currentTimeMillis() - leftKeyTimer);
    leftKeyTimer = System.currentTimeMillis();
}
long leftTime = leftKeyFinalTimer;
leftKeyFinalTimer  =0;
// use leftTime here


The thing is you cant rely on key release event to know when a key has been really released. If it has not been really released, a keyPress event is fired right after the KeyRelease one.
So you might also try to wait sonething like 10ms (you should check how really close in time these 2 events are fired) when you receive a keyRelease event before processing it and cancel it if a keyPress event is received during this time.

Regards,

Charly

Games published by our own members! Go get 'em!
Offline Abuse

JGO Kernel
*****

Posts: 1866
Medals: 5


falling into the abyss of reality


« Reply #3 on: 2008-12-23 22:53:01 »

Has this bug still not been fixed  Huh

It's been there since 1.1, more than a decade!  Huh
Offline Renoria

Full Member
**

Posts: 209


...


« Reply #4 on: 2008-12-24 01:00:58 »

Has this bug still not been fixed  Huh

It's been there since 1.1, more than a decade!  Huh

o.O! wtf I don't have this problem...
Offline pjt33

JGO Strike Force
***

Posts: 913
Medals: 17



« Reply #5 on: 2009-01-20 15:14:01 »

Youre right.

If I remember, its KDE wich fires these KeyPressed/KeyReleased event for autorepeat.
Yes. Thanks: I've disabled it in KDE and now I'm going to go back to the 4k competition entries which were giving me key handling problems to see whether they're any better.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.071 seconds with 19 queries.