Skip to content

Commit 27135b2

Browse files
authored
Merge pull request #1 from jartu/develop
Adding new Button example that demonstrates the new EV3Key implementation.
2 parents 56b3f0f + 3f1be81 commit 27135b2

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package ev3dev.sensors;
2+
3+
import lejos.hardware.Key;
4+
import lejos.hardware.KeyListener;
5+
6+
import java.time.LocalTime;
7+
import java.time.format.DateTimeFormatter;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
public class ButtonListenersExample {
13+
14+
private static final DateTimeFormatter TIMESTAMP_FORMAT = DateTimeFormatter.ofPattern(
15+
"HH:mm:ss.SSS");
16+
17+
private static class MyKeyListener implements KeyListener {
18+
private final Key registeredForKey;
19+
20+
public MyKeyListener(final Key registeredForKey) {
21+
this.registeredForKey = registeredForKey;
22+
}
23+
24+
@Override
25+
public void keyPressed(final Key key) {
26+
System.out.format("%s - [%s listener] %s pressed%n", LocalTime.now().format(TIMESTAMP_FORMAT),
27+
this.registeredForKey.getName(), key.getName());
28+
}
29+
@Override
30+
public void keyReleased(final Key key) {
31+
System.out.format("%s - [%s listener] %s released%n", LocalTime.now().format(TIMESTAMP_FORMAT),
32+
this.registeredForKey.getName(), key.getName());
33+
}
34+
}
35+
36+
private static final List<Key> BUTTONS_FOR_LISTENERS = Arrays.asList(new Key[] {
37+
Button.UP,
38+
Button.DOWN,
39+
new EV3Key(EV3Key.BUTTON_ALL) // a special key that stands for 'any' key
40+
});
41+
42+
public static void main(final String[] args){
43+
44+
// registering the listeners
45+
System.out.println("Registering key listeners...");
46+
BUTTONS_FOR_LISTENERS.forEach((key) -> {
47+
key.addKeyListener(new MyKeyListener(key));
48+
System.out.format("Listener for %s key registered%n", key.getName());
49+
});
50+
51+
// printing some on-screen help
52+
final Key exitButton = Button.ESCAPE;
53+
final Key anyButton = new EV3Key(EV3Key.BUTTON_ALL);
54+
final List<String> registeredKeyNamesExceptAll = BUTTONS_FOR_LISTENERS.stream()
55+
.filter((button) -> ! button.equals(anyButton))
56+
.map(Key::getName).collect(Collectors.toList());
57+
System.out.format("%nKeep pressing any buttons to see listeners getting key press/release events.%n");
58+
System.out.format("Notice that if you press or release one of the %s buttons, you'll get two events:%n", registeredKeyNamesExceptAll);
59+
System.out.format(" 1) one for the listener registered on that specific key%n");
60+
System.out.format(" 2) another one for the listener registered on the special %s key%n", anyButton.getName());
61+
62+
// printing the key events (from the listeners) until the ESCAPE key is pressed (i.e. the call above blocks)
63+
System.out.format("%nPress the %s key to terminate the program...%n", exitButton.getName());
64+
exitButton.waitForPress();
65+
66+
// terminating
67+
System.out.format("%n%s press detected, terminating the program.%n", exitButton.getName());
68+
}
69+
}

0 commit comments

Comments
 (0)