Quote:
Originally Posted by Platypi3
Hello, similar question...how would you program the arrow key control in Java? Sorry, I'm also a rookie, so a (simplified) explanation would be greatly appreciated!
|
Simplified. Of course :P What I think you want to do is to add a
KeyListener to your custom driverstation. This code is out of context and won't work as stand alone. You'll have to make your class implement a keyListener (i.e "public class myClass implements KeyListener { }" ) and have the following line in the constructor:
Code:
addKeyListener(this);
This is what the actual listening portion would look like:
Code:
public void keyPressed(KeyEvent ke) {
if (ke.getKeyCode() == KeyEvent.VK_UP) {/*instruction for Up-key*/} //VK_UP is a pre-defined constant value
else if (ke.getKeyCode() == KeyEvent.VK_DOWN) {/*instruction for Down-key*/}
else if (ke.getKeyCode() == KeyEvent.VK_LEFT) {/*instruction for Left-key*/}
else if (ke.getKeyCode() == KeyEvent.VK_RIGHT) {/*instruction for Right-key*/}
}
public void keyTyped(KeyEvent ke) {}
public void keyReleased(KeyEvent ke) {}
Whatever I've said that didn't make sense should be remedied by looking at the example code found
here.