Patrick Harden
Patrick Harden

Reputation: 45

How do I get KeyListener working?

I do apologize, I know this has been asked before. The other posts suggest just using keybindings, but I'd much prefer to use this method as I would like the movement of the robot to stop with keyrelease.

import java.io.BufferedReader;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.Button;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BoxLayout;

public class Robot implements KeyListener {
    public static JButton a, w, s, d, space, ctrl;
    public static char input;
    public static String takeoff = "{\"op\":\"publish\",\"topic\":\"/ardrone/takeoff\",\"msg\":{}}";
    public static String landing = "{\"op\":\"publish\",\"topic\":\"/ardrone/land\",\"msg\":{}}";
    public static String stop = "{\"op\":\"publish\",\"topic\":\"/cmd_vel\",\"msg\":{\"linear\":{\"x\":0,\"y\":0,\"z\":0},\"angular\":{\"x\":0,\"y\":0,\"z\":0}}}";


    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("Robot Controller");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponentsToPane(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
        frame.setSize(800, 600);
        frame.add(a);
        frame.add(w);
        frame.add(d);
        frame.add(s);
        frame.add(space);
        frame.add(ctrl);


        Socket mysocket = null;
        PrintStream output = null;
        try {
            // opens socket
            mysocket = new Socket("lear.cs.okstate.edu", 9095);     
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        try {
            // opens stream to write to socket
            output = new PrintStream(mysocket.getOutputStream());
            output.print(takeoff);
            } catch (IOException e){
            System.out.print(e);
            }
        }
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_A) {
            System.exit(0);
        }
        if (e.getKeyCode() == KeyEvent.VK_S) {

        }
        if (e.getKeyCode() == KeyEvent.VK_D) {

        }
        if (e.getKeyCode() == KeyEvent.VK_W) {

        }
        if (e.getKeyCode() == KeyEvent.VK_S) {

        }
        if (e.getKeyCode() == KeyEvent.VK_SPACE) {

        }
        if (e.getKeyCode() == KeyEvent.VK_CONTROL) {

        }
    }

    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub
    }

    public static void addComponentsToPane(Container pane) {
        pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
        a = new JButton("Move left: A");       
        w = new JButton("Move up: W");
        d = new JButton("Move right: D");
        s = new JButton("Move down: S");
        space = new JButton("Takeoff: spacebar");
        ctrl = new JButton("Land: control");
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

Upvotes: 0

Views: 330

Answers (2)

Gacci
Gacci

Reputation: 1398

You have the listener definition, but you have not attached the listener to any component. Add the listener to the component you want to listen events from.

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70899

Somewhere in you createAndShowGui() add the key listener to one of the swing components. Then, when that component has focus, it will receive the key events. If you want the keys for all of the application, then you need a key listener on the largest container component your application has.

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Upvotes: 1

Related Questions