[java] Keyboard Input

Started by
5 comments, last by greensam 22 years, 9 months ago
hello. i have a question about keyboard input, now i might screw up a bit in my explanations since my isdn card has some problems so i have to surf at my brother''s computer and out network between them also has some problems so i cant paste in parts of the source except from my memory. now i am trying to create a simple space invaders clone just to learn stuff. i''ve got a thread set up that draws a background on a frame and a space ship on it. then i tried adding a keylistener which extends keyadapter (i might have confused these two names here) to the frame. my keyadapter class looks something like this: (can be errors here due to me having to run back and forth between two rooms remembering the lines of code in my head) public void keyPressed (keyEvent e) { if (e.getKeyCode() == e.VK_KP_LEFT) ship.moveleft(); else if (e.getKeyCode() == e.VK_KP_RIGHT) ship.moveright(); } this passes through the compiler but the ship won''t move at all. i''d be very grateful for any help thanks sam
Advertisement
Well for one thing you only get one event per keypress, so it might be moving, just a tiny ammount. I''m assuming that top have properly done a frame.addKeyListener(this?).

Really what you should be doing is more like..

public void keyPressed (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT ) ship.moveleft();
else if (e.getKeyCode() == e.VK_KP_RIGHT ) ship.moveright();
}

public void keyReleased (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT ) ship.stop();
else if (e.getKeyCode() == e.VK_KP_RIGHT ) ship.stop();
}

ship.moveleft and moveright should set a variable to indicate that it is moving. When you redread and it is set move it left or right, the stop command clears the movment.

The reason for the change is the key pressed ( as opposed to keyType event ) is that it is only sent ONCE when the person depresses the key. This is why to need to trap both otherwise you would never know when the key was released. If you just want what would have showed up in a text window "a..a..aaaaaa" capture the keyTyped event and not the keyPressed event.

Cheers


Thanks a lot for taking your time to help me out, but you have in this case helped me solve a problem i didn''t have yet.

I am not yet concerned with making input work great but make it work AT ALL. I added a drawString to my thread so that it writes the coordinates of the player sprite to check if they change at all but they don''t.

So I have done the:

class playercontrol extends KeyAdapter () {

here i have the methods that check for input just like you proposed they should be

}

container.addKeyListener(control);

and this all passes through the compiler.

It''s strange. I think it should work, because I''ve managed to add a WindowAdapter too which checks for windowClosing and does a System.exit(0); so I think I''ve sorted out how it''s supposed to work when adding inpurtlisteners.

very grateful for further help.

sam
Check these two functions to see if it helps any...

Component.requestFocusInWindow()
Component.enableEvents(mask)

and

KeyboardFocusManager.setGlobalFocusManager(compnent)

I''m sure the events are get getting redirected, do you have any other components in your windows that might be capturing events?
Thanks again for your help.

I have already sorted out part of the problem though. It seems that the fields VK_KP_LEFT and VK_KP_RIGHT in KeyEvent didn''t corresponf to my keyboard''s left and right arrow keys. I tried changing the controls to a and d. Well it worked. Now thanks to you it works pretty well. At least much better than it would have done the way i had it before when it would have only moved one pixel per keypress.

The problem is, it''s still a bit sloppy. For example if you push right first, then start pushing left immediately after you release right (perhaps in the same "execution-cycle" of the thread) the ship stops for a short while before changing direction.

And if you push right then start pushing left while you push right it changes direction smoothly. But if you then let go of the left key it stops and fails to notice that you haven''t lifted your finger off the right key.

Sam
The last one is simple to fix.

public void keyReleased (keyEvent e) {
if (e.getKeyCode() == e.VK_KP_LEFT && ship.movingleft) ship.stop();
else if (e.getKeyCode() == e.VK_KP_RIGHT && ship.movingright ) ship.stop();
}

So it only stops movement if the key released is the direction it''s moving in. By chance is your keyboard international? You could always set up a little keyapp to show you what keys it THINKS are being pressed.

Check these two functions to see if it helps any...

Component.requestFocusInWindow()
Component.enableEvents(mask)

and

KeyboardFocusManager.setGlobalFocusManager(compnent)


Is all of that needed? Surely all you need is to call requestFocus()?

Also VK_KP_LEFT/VK_KP_RIGHT or for the the key pad (th bit on the right with all of the numbers), not the arrow kes. Use VK_LEFT/VK_RIGHT instead. Particulary because not all keyboards have a keypad with arrows on, but nearly all have arrow keys.

cheers,
John

This topic is closed to new replies.

Advertisement