-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelloKeyEvent.java
176 lines (141 loc) · 5.81 KB
/
TelloKeyEvent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.*;
import java.net.SocketException;
import javax.swing.*;
public class TelloKeyEvent extends JFrame implements KeyListener, ActionListener {
private static final long serialVersionUID = 1L;
JTextArea displayArea;
JTextField typingArea;
static final String newline = System.getProperty("line.separator");
static Control ctlObj = new Control();
/**
* Establish Tello connection and GUI setup. Only executes once.
*/
public static void main(String[] args) {
// Tello setup
try {
ctlObj.connect();
} catch (SocketException e) {
e.printStackTrace();
}
//GUI setup
// Use an appropriate Look and Feel
try {
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
// Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
//Schedule a job for event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread. Only executes once.
*/
private static void createAndShowGUI() {
//Create and set up the window.
TelloKeyEvent frame = new TelloKeyEvent("Feedback Window");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
frame.addComponentsToPane();
//Display the window.
frame.pack();
frame.setVisible(true);
}
/**
* Add components to pane. Only executes once.
*/
private void addComponentsToPane() {
JButton button = new JButton("Clear");
button.addActionListener(this);
typingArea = new JTextField(20);
typingArea.addKeyListener(this);
//Uncomment this if you wish to turn off focus
//traversal. The focus subsystem consumes
//focus traversal keys, such as Tab and Shift Tab.
//If you uncomment the following line of code, this
//disables focus traversal and the Tab events will
//become available to the key event listener.
//typingArea.setFocusTraversalKeysEnabled(false);
displayArea = new JTextArea();
displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(displayArea);
scrollPane.setPreferredSize(new Dimension(375, 125));
getContentPane().add(typingArea, BorderLayout.PAGE_START);
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.PAGE_END);
}
public TelloKeyEvent(String name) {
super(name);
}
/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e) {
//displayInfo(e, "KEY TYPED: ");
}
/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e) {
//Clear text box
typingArea.setText("");
//Return the focus to the typing area.
typingArea.requestFocusInWindow();
displayInfo(e, "KEY PRESSED: ");
}
/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e) {
//displayInfo(e, "KEY RELEASED: ");
}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
displayArea.setText("");
typingArea.setText("");
//Return the focus to the typing area.
typingArea.requestFocusInWindow();
}
/*
* We have to jump through some hoops to avoid
* trying to print non-printing characters
* such as Shift. (Not only do they not print,
* but if you put them in a String, the characters
* afterward won't show up in the text area.)
*/
private void displayInfo(KeyEvent e, String keyStatus){
//You should only rely on the key char if the event
//is a key typed event.
int id = e.getID();
String keyString, telloResp;
byte sendData[];
// Capture keycode from action
int keyCode = e.getKeyCode();
sendData = ctlObj.decodeCmd(keyCode).getBytes();
telloResp = ctlObj.sendCmd(sendData);
keyString = ("Decoded message: " + ctlObj.decodeCmd(keyCode) + "\n"
+ "TELLO RESPONSE: " + telloResp);
displayArea.append(keyStatus + newline
+ " " + keyString + newline);
/*
+ " " + modString + newline
+ " " + actionString + newline
+ " " + locationString + newline);
*/
displayArea.setCaretPosition(displayArea.getDocument().getLength());
}
}