View Single Post
  #11   Spotlight this post!  
Unread 15-02-2014, 23:29
ekapalka's Avatar
ekapalka ekapalka is offline
Registered User
FRC #3216
 
Join Date: Dec 2012
Location: Bermuda
Posts: 277
ekapalka has a spectacular aura aboutekapalka has a spectacular aura about
Re: Using Keyboard to Drive

Quote:
Originally Posted by Platypi3 View Post
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.
Reply With Quote