Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
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  
  how to detect how fast keys are pressed repetitively  (Read 1004 times)
0 Members and 1 Guest are viewing this topic.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Posted 2012-11-23 18:00:16 »

So, for my 4K game, I want the player to be able to move faster/slower based on how fast one or two keys are pressed repetitively(e.g. J and K, or just J). I don't really know how to go about doing this. Any suggestions? I don't absolutely need actual code.
Offline Agro
« Reply #1 - Posted 2012-11-23 18:25:48 »

Well, for one, you'd have to use a KeyListener. Make a class that implements a KeyListener.

Basically, every time a key you want to be included is pressed, add for example, 2 to the speed of the velocity in the keyPressed method. In your game logic, decrease the velocity by 1 or 1.5, as long as its greater than 0.

Tell me if this works. Smiley

Offline ReBirth
« Reply #2 - Posted 2012-11-23 23:42:16 »

Or get the time diff between key press and key release. (but I prefer Agro's)

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

JGO Knight


Medals: 12
Projects: 6
Exp: 10 years


Java games rock!


« Reply #3 - Posted 2012-11-24 05:58:52 »

Since this is for a java 4k project, I would suggest that you do not use a key listener explicitly, rather extend applet and enable key events and capture thoese events. e.g.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
public class a extends Applet implements Runnable {

  // keys
 private boolean[] a= new boolean[1]; // can be made larger and used by your game for storage.

  public void start() {
    enableEvents(8);
    new Thread(this).start();
  }

  public run()
  {
     
      // your game here
   
     // handle key press
    switch (this.a[0])
     {
        case KEY_CODE:
         ....
      }
       
     // and more game here
 }

  public final boolean handleEvent(Event paramEvent)
  {
    switch (paramEvent.id)
    {
    case 403: // key press
     this.a[0] = paramEvent.key;
    }
    return false;
  }
}

Offline Agro
« Reply #4 - Posted 2012-11-24 07:03:44 »

Well, it would be better not to introduce a keystate system, because he's trying to get how fast the keypresses are pressed repetitvely, not how long they're pressed. But yeah, for the 4K game, just handle the keypresses inside that handleEvent thing

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 131
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-11-28 00:56:52 »

@Agro -
your system works, but it can be bypassed just by pressing down and holding the key. Any good (small) ways to prevent this?
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.

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

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

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

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

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

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

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

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

Longarmx (35 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!