Using Keyboard to Drive

Hey Guys,
I am a rookie programmer on my team and was assigned to work on using arrow keys to drive. I was wondering if anybody has experience and what they used to make their arrow key drive work. I have tried many times, but could not succeed. Any help on making keyboard drive work in C++ would be great. Thanks.

You would have a lot more control if you used the mouse.

We were using arrow keys to test the robot,not to use in the competition. We wanted to use arrow key for non-variable drive. Thanks for your input, however.

We put some keyboard control features on our robot last year. The general scheme was to read the keys in a LabVIEW Dashboard program and send them to the robot using Smart Dashboard variables. The robot can read those variables using any of the supported programming languages.

Could you please upload the Vi you used to get and stream keyboard input to the robot? I am a beginner developer with LabView and understand the concept. The C++ code used to retrieve the data would be nice as well. Thank you very much.

The functions used in the Dashboard to read the keyboard are in the Connectivity -> Input Device Control function palette.

Use the Initialize Keyboard function to get a “device ID” output, and pass that into the loop that reads the keyboard using the Acquire Input Data function. The result will be an array of keys being pressed. If the length of the array is zero, no keys are pressed. If you don’t want to be able to handle multiple keys pressed at the same time, just use an Index Array function to pick out the first element of the array.

The Smart Dashboard functions used to send data to the robot are in the WPI Robotics Library -> Dashboard function palette.

Use the SD Write String function to put the value of the key being pressed (or a null string if no keys are pressed) into a Smart Dashboard variable.

In the C++ code, use the NetworkTables support to read the value of the variable out of the SmartDashboard table. gives a lot of information and some example code for using NetworkTables.

Reminds me of computer games we used to play with our kids 15-20 years ago. :rolleyes:

My team really wanted to do this, too, but we got worried about potential operator errors and haven’t really gone through with it yet. The basic idea for us was to create a custom dashboard (using Processing) and having that take the WASDShift/arrow keys/mouse commands and send them to the robot via NetworkTables (we got the programme written but never tested it). The issue we were worried about was the driver accidentally pressing the space bar or return key while driving and potentially disabling the robot mid-match. In order to solve this potential issue, we were thinking of having something like a Raspberry Pi in-between a USB keyboard and the DriverStation computer which would take the input from the keyboard and send it to the DriverStation like a joystick. We never tried that out either, though…

If the FMS is present, the spacebar doesn’t estop the robot, the big red mushroom button does. When plugged into the FMS, only the F1 key does anything on the DS, and it scans for new joysticks and redoes the joystick connections.

Greg McKaskle

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 :stuck_out_tongue: 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:


This is what the actual listening portion would look like:

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.

Hey thanks! The key listener tutorial was really helpful. I think I got the listening part, but I can’t figure out how to add the actual keyListener… Er…do I need to import something special for it?

KeyListener isn’t a part of Java by default but instead a part of the awt package. The import statement would be
import java.awt.event.*;

This is kind of late but this won’t work because when you run it on the robot you’d need like a keyboard on the Rio or whatever. So this doesn’t send the commands for the robot to run it’ll just run it on your computer only.

TBH if I were to implement something similar, the way I would do it is to just have a standalone program run on the DS laptop in the background that is separate from the DS altogether. All it needs to do is to listen for global key presses, and update values in NetworkTables when it detects a key press. Then, the robot side can just listen for the NT value change and perform actions. However, making the key hook global (i.e. it can still detect your key presses even when you’re focused on another window) would mean making it platform-dependent in C++ and very hard to do with Java.