wreed12345
|
 |
«
Posted
2012-11-01 22: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!
|
|
|
|
|
Regenuluz
|
 |
«
Reply #1 - Posted
2012-11-01 22:22:16 » |
|
Use the keyReleased(KeyEvent e) that's defined in the KeyListener interface. 
|
|
|
|
|
wreed12345
|
 |
«
Reply #2 - Posted
2012-11-01 22:23:19 » |
|
what are the paramaters to that?
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Regenuluz
|
 |
«
Reply #3 - Posted
2012-11-01 22: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 
|
|
|
|
|
wreed12345
|
 |
«
Reply #4 - Posted
2012-11-01 22: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
|
|
|
|
|
philfrei
|
 |
«
Reply #5 - Posted
2012-11-01 23:29:29 » |
|
Have you seen this tutorial? http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.htmlAlso, 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.htmlBut 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
|
|
|
wreed12345
|
 |
«
Reply #6 - Posted
2012-11-02 00: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?
|
|
|
|
|
ctomni231
|
 |
«
Reply #7 - Posted
2012-11-02 00: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.
|
|
|
|
|
davedes
|
 |
«
Reply #8 - Posted
2012-11-02 00: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) JavaDocTestsGame Examples (SlickVirium, SlickPuzzle, etc)
|
|
|
|
wreed12345
|
 |
«
Reply #9 - Posted
2012-11-02 02:47:11 » |
|
what does it mean to pole a function?
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
wreed12345
|
 |
«
Reply #10 - Posted
2012-11-02 02:47:46 » |
|
I have often seen @ Override being used but what does it actually do?
|
|
|
|
|
Vladiedoo
|
 |
«
Reply #11 - Posted
2012-11-02 02: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.
|
|
|
|
|
wreed12345
|
 |
«
Reply #12 - Posted
2012-11-02 03: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; } |
|
|
|
|
|
Vladiedoo
|
 |
«
Reply #13 - Posted
2012-11-02 03: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; } } |
|
|
|
|
|
SHC
|
 |
«
Reply #14 - Posted
2012-11-02 05:20:00 » |
|
|
|
|
|
EgonOlsen
|
 |
«
Reply #15 - Posted
2012-11-02 08:28:16 » |
|
The one on the left side seems depressed to me... 
|
|
|
|
KittenKoder
|
 |
«
Reply #16 - Posted
2012-11-02 09: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.
|
|
|
Regenuluz
|
 |
«
Reply #17 - Posted
2012-11-02 11: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.htmlIt'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.  )
|
|
|
|
|
KittenKoder
|
 |
«
Reply #18 - Posted
2012-11-02 11: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.htmlIt'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.  ) 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.
|
|
|
Regenuluz
|
 |
«
Reply #19 - Posted
2012-11-02 11:51:08 » |
|
I usually spot that before I save, or right after I've saved.  But yeah, I guess that could cause some trouble, if it isn't spotted. 
|
|
|
|
|
wreed12345
|
 |
«
Reply #20 - Posted
2012-11-02 14: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
|
|
|
|
|
GabrielBailey74
|
 |
«
Reply #21 - Posted
2012-11-02 21: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.
|
|
|
|
theagentd
|
 |
«
Reply #22 - Posted
2012-11-03 01:09:08 » |
|
68836? Shouldn't that be 65536?
|
Myomyomyo.
|
|
|
matheus23
|
 |
«
Reply #23 - Posted
2012-11-03 17:25:48 » |
|
68836? Shouldn't that be 65536?
Yep.  KDE/Linux is awesome.
|
|
|
|
ra4king
|
 |
«
Reply #24 - Posted
2012-11-03 21:44:11 » |
|
Sad key is sad 
|
|
|
|
sproingie
|
 |
«
Reply #25 - Posted
2012-11-03 23: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.
|
|
|
|
|
Riven
|
 |
«
Reply #26 - Posted
2012-11-04 00: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.
|
|
|
|
|
|
davedes
|
 |
«
Reply #28 - Posted
2012-11-04 08: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.
|
|
|
|
|