Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
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  
  Detect when key is depressed?  (Read 2993 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2012-11-01 21:16:31 »

I know how to detect if a key is pressed using
1  
input.isKeyDown(Input.KEY_UP)

but how would i tell if the key is depressed? I couldnt figure out how to do this sucessfully.
using this code doesnt work:( vvv
1  
(!(input.isKeyDown(Input.KEY_UP))


any ideas are welcome!

Offline Regenuluz
« Reply #1 - Posted 2012-11-01 21:22:16 »

Use the keyReleased(KeyEvent e) that's defined in the KeyListener interface. Smiley
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2012-11-01 21:23:19 »

what are the paramaters to that?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Regenuluz
« Reply #3 - Posted 2012-11-01 21:25:15 »

This is my very basic key controller for my map editor. So yeah, this is how I handle key pressed and key released stuff.

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  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
package controls;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import model.MapEditor;

public class KeyController extends KeyAdapter {

   private MapEditor me;

   public KeyController(MapEditor mt) {
      this.me = mt;
   }
   
   @Override
   public void keyPressed(KeyEvent e) {
      switch(e.getKeyCode()) {
      case KeyEvent.VK_UP:
         this.me.key_up = true;
         break;
      case KeyEvent.VK_DOWN:
         this.me.key_down = true;
         break;
      case KeyEvent.VK_LEFT:
         this.me.key_left = true;
         break;
      case KeyEvent.VK_RIGHT:
         this.me.key_right = true;
         break;
      }
   }

   @Override
   public void keyReleased(KeyEvent e) {
      switch(e.getKeyCode()) {
      case KeyEvent.VK_UP:
         this.me.key_up = false;
         break;
      case KeyEvent.VK_DOWN:
         this.me.key_down = false;
         break;
      case KeyEvent.VK_LEFT:
         this.me.key_left = false;
         break;
      case KeyEvent.VK_RIGHT:
         this.me.key_right = false;
         break;
      }
   }
}


Hope it helps Smiley
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2012-11-01 21:38:42 »

thanks for that example but do you know what the parameters are for that? because i do not completely understand how to implement this

Offline philfrei
« Reply #5 - Posted 2012-11-01 22:29:29 »

Have you seen this tutorial?

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Also, it would be helpful to know more about your context. I don't recognize
1  
input.isKeyDown(Input.KEY_UP)


The KeyListener and KeyEvent are part of AWT. Are you using a library? Regenuluz's example is core Java. If you are using core Java, you have to use Event Listeners.

http://docs.oracle.com/javase/tutorial/uiswing/events/intro.html

But if it is part of a library, let us know which and there will be someone to show you the way, I'm pretty sure.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #6 - Posted 2012-11-01 23:01:22 »

I am using slick2d and lwjgl i am not sure of what code that is a part of. Do you know how i could detect when a key is depressed?

Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #7 - Posted 2012-11-01 23:11:46 »

I know how to detect if a key is pressed using
1  
input.isKeyDown(Input.KEY_UP)

but how would i tell if the key is depressed? I couldnt figure out how to do this sucessfully.
using this code doesnt work:( vvv
1  
(!(input.isKeyDown(Input.KEY_UP))


I've worked with Slick2D quite a bit.

If a key is depressed, isKeyDown() will always be true. If you want to check to see if a key is depressed, then just poll that function.

Offline davedes
« Reply #8 - Posted 2012-11-01 23:37:49 »

In Slick this would be done by overriding keyReleased in BasicGame or your BasicGameState.

This is covered in the wiki and in various tests, such as InputTest. Here are some more links I'd suggest browing through:

Main Site
New Wiki (old wiki)
JavaDoc
Tests
Game Examples (SlickVirium, SlickPuzzle, etc)

Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #9 - Posted 2012-11-02 01:47:11 »

what does it mean to pole a function?

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

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #10 - Posted 2012-11-02 01:47:46 »

I have often seen @ Override being used but what does it actually do?

Offline Vladiedoo
« Reply #11 - Posted 2012-11-02 01:56:28 »

I understand what you are asking for but the answer you will get to your problem is probably more complicated than you want it to be. You could store when they key is being held down and the first instance of it not being held down you would do whatever action you needed to do for when the key is released.


Instead, try to see if this following line will work for your needs.
1  
input.isKeyPressed(Input.KEY_UP)


Edit: Instead of saying "depressed" say released.
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #12 - Posted 2012-11-02 02:01:42 »

Thanks guys for all the help but i figure out how to use it using this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      if (quit == false) {
         if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
            up = true;
            right = false;
            left = false;
            down = false;
            movingUp = true;
            movingLeft = false;
            movingRight = false;
            movingDown = false;
            buckyPositionY += delta * .1f;
            if (buckyPositionY > -1059) {
               buckyPositionY -= delta * .1f;
            }
         }
         if (!(input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W))){
            upAnimation = spriteUp;
         }

Offline Vladiedoo
« Reply #13 - Posted 2012-11-02 02:10:38 »

You were actually asking for when the key was not down in that case.

The way you have your code set up could be improved. You check for a condition to be true and then you check if the same condition is false. Instead you could check if the condition once and based off of that perform an action.

Instead of two if statements, you can use an if else statement.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
         if (quit == false) {
            if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
               up = true;
               right = false;
               left = false;
               down = false;
               movingUp = true;
               movingLeft = false;
               movingRight = false;
               movingDown = false;
               buckyPositionY += delta * .1f;
               if (buckyPositionY > -1059) {
                  buckyPositionY -= delta * .1f;
               }
            } else {
               upAnimation = spriteUp;
            }
         }
Offline SHC
« Reply #14 - Posted 2012-11-02 04:20:00 »

See this code which uses core java.

http://www.java-gaming.org/topics/polling-input/27635/view.html

Offline EgonOlsen
« Reply #15 - Posted 2012-11-02 07:28:16 »

The one on the left side seems depressed to me...


Offline KittenKoder

Senior Member


Medals: 7



« Reply #16 - Posted 2012-11-02 08:09:33 »

The one on the left side seems depressed to me...



The one on the bottom looks more depressed.

I am no one else.
Offline Regenuluz
« Reply #17 - Posted 2012-11-02 10:11:51 »

I have often seen @ Override being used but what does it actually do?

That's summed up pretty neatly here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Override.html

It's not really something that you strictly need to use, I use it because I think it's easier to figure out why and where functions are coming from when I see that. (And I've set Eclipse to bitch at me, if it's missing. But then I've also set Eclipse to add it whenever I save. Tongue)
Offline KittenKoder

Senior Member


Medals: 7



« Reply #18 - Posted 2012-11-02 10:49:37 »

I have often seen @ Override being used but what does it actually do?

That's summed up pretty neatly here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Override.html

It's not really something that you strictly need to use, I use it because I think it's easier to figure out why and where functions are coming from when I see that. (And I've set Eclipse to bitch at me, if it's missing. But then I've also set Eclipse to add it whenever I save. Tongue)

I like the setting to make it bitch at me, but I prefer it not to be included automatically so I can know if I accidentally use the same method name of something important.

I am no one else.
Offline Regenuluz
« Reply #19 - Posted 2012-11-02 10:51:08 »

I usually spot that before I save, or right after I've saved. Smiley But yeah, I guess that could cause some trouble, if it isn't spotted. Tongue
Offline wreed12345

JGO Knight


Medals: 24
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #20 - Posted 2012-11-02 13:33:22 »

You were actually asking for when the key was not down in that case.

The way you have your code set up could be improved. You check for a condition to be true and then you check if the same condition is false. Instead you could check if the condition once and based off of that perform an action.

Instead of two if statements, you can use an if else statement.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
         if (quit == false) {
            if (input.isKeyDown(Input.KEY_UP) || input.isKeyDown(Input.KEY_W)) {
               up = true;
               right = false;
               left = false;
               down = false;
               movingUp = true;
               movingLeft = false;
               movingRight = false;
               movingDown = false;
               buckyPositionY += delta * .1f;
               if (buckyPositionY > -1059) {
                  buckyPositionY -= delta * .1f;
               }
            } else {
               upAnimation = spriteUp;
            }
         }

Thanks! this really helped clean up my code and would have saved me a lot of time when i originally wrote this. thanks again

Offline GabrielBailey74
« Reply #21 - Posted 2012-11-02 20:41:12 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public boolean[] key = new boolean[68836];

   public void keyPressed(KeyEvent k) {
      int keyCode = k.getKeyCode();
      if (keyCode > 0 && keyCode < key.length) {
         key[keyCode] = true;
      }
   }

   public void keyReleased(KeyEvent k) {
      int keyCode = k.getKeyCode();
      if (keyCode > 0 && keyCode < key.length) {
         key[keyCode] = false;
      }
   }


Handles multiple key presses/releases @ once.

Offline theagentd
« Reply #22 - Posted 2012-11-03 00:09:08 »

68836? Shouldn't that be 65536?

Myomyomyo.
Offline matheus23

JGO Kernel


Medals: 107
Projects: 3


You think about my Avatar right now!


« Reply #23 - Posted 2012-11-03 16:25:48 »

68836? Shouldn't that be 65536?
Yep.

KDE/Linux is awesome.

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #24 - Posted 2012-11-03 20:44:11 »

Sad key is sad Sad

Offline sproingie

JGO Kernel


Medals: 202



« Reply #25 - Posted 2012-11-03 22:53:07 »

An array of 65536 booleans is not likely to be packed, so this is ridiculously wasteful.  Seriously, use a Set<Integer>.  It's not going to impact performance unless your players are in the habit of holding down thousands of keys at a time.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #26 - Posted 2012-11-03 23:06:55 »

Premature optimisation. Period.

It works, is simple, doesn't have noticable overhead (both in CPU time and RAM usage).
There is no need to improve on it. One should work on the game instead.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline masteryoom

JGO Coder


Medals: 5
Projects: 2


If you look closely, you might see it turning...


« Reply #27 - Posted 2012-11-04 02:57:26 »

In LWJGL, the code is this:
http://pastebin.java-gaming.org/2c27562962f
Hope this helps.  Grin
(I took this from the Ninja Cave tutorials here:http://http://ninjacave.com/lwjglbasics2)

Smiley
Offline davedes
« Reply #28 - Posted 2012-11-04 07:13:31 »

Premature optimisation. Period.

It works, is simple, doesn't have noticable overhead (both in CPU time and RAM usage).
There is no need to improve on it. One should work on the game instead.
Personally I find coding standards more important in this situation. My rule of thumb: take the extra 5 to 10 minutes to solve the problem properly, rather than creating some brutish and wasteful hack. This is especially important for beginners; if they are never told to think about concepts like memory management, garbage collection, or what have you, they will inevitably end up with horribly inefficient game code.

Nothing worse than collaborating with a Java programmer who doesn't bother keeping his code clean and optimized. Eventually, shitty code will catch up to you.

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 (24 views)
2014-09-19 03:14:18

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

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

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

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

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

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

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

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

Longarmx (40 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!