-
Notifications
You must be signed in to change notification settings - Fork 1
/
KeyboardObserver.java
69 lines (52 loc) · 1.66 KB
/
KeyboardObserver.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
package com.javarush.task.task23.task2312;
import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
public class KeyboardObserver extends Thread {
private Queue<KeyEvent> keyEvents = new ArrayBlockingQueue<KeyEvent>(100);
private JFrame frame;
@Override
public void run() {
frame = new JFrame("KeyPress Tester");
frame.setTitle("Transparent JFrame Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setSize(400, 400);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setLayout(new GridBagLayout());
frame.setOpacity(0.0f);
frame.setVisible(true);
frame.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
//do nothing
}
@Override
public void focusLost(FocusEvent e) {
System.exit(0);
}
});
frame.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
//do nothing
}
public void keyReleased(KeyEvent e) {
//do nothing
}
public void keyPressed(KeyEvent e) {
keyEvents.add(e);
}
});
}
public boolean hasKeyEvents() {
return !keyEvents.isEmpty();
}
public KeyEvent getEventFromTop() {
return keyEvents.poll();
}
}