1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.melloware;
21
22 import java.awt.BorderLayout;
23 import java.awt.Event;
24 import java.awt.Frame;
25 import java.awt.GraphicsEnvironment;
26 import java.awt.Point;
27 import java.awt.Rectangle;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30
31 import javax.swing.JButton;
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34 import javax.swing.JScrollPane;
35 import javax.swing.JTextArea;
36 import javax.swing.border.EtchedBorder;
37
38 import com.melloware.jintellitype.HotkeyListener;
39 import com.melloware.jintellitype.IntellitypeListener;
40 import com.melloware.jintellitype.JIntellitype;
41
42
43
44
45
46
47
48
49
50 public class JIntellitypeTester extends JFrame implements HotkeyListener, IntellitypeListener {
51
52 private static JIntellitypeTester mainFrame;
53 private static final int WINDOWS_A = 88;
54 private static final int ALT_SHIFT_B = 89;
55 private static final int CTRL_SHIFT_C = 90;
56 private static final int PRINT_SCREEN = 91;
57 private static final int F9 = 92;
58 private static final int F12 = 93;
59 private JButton btnRegisterHotKey = new JButton();
60 private JButton btnUnregisterHotKey = new JButton();
61 private JPanel bottomPanel = new JPanel();
62 private JPanel mainPanel = new JPanel();
63 private JPanel topPanel = new JPanel();
64 private JScrollPane scrollPane = new JScrollPane();
65 private JTextArea textArea = new JTextArea();
66
67
68
69
70 public JIntellitypeTester() {
71 initComponents();
72 }
73
74
75
76
77
78
79 public static void main(String[] args) {
80
81
82 if (JIntellitype.checkInstanceAlreadyRunning("JIntellitype Test Application")) {
83 System.exit(1);
84 }
85
86
87
88 if (!JIntellitype.isJIntellitypeSupported()) {
89 System.exit(1);
90 }
91
92 mainFrame = new JIntellitypeTester();
93 mainFrame.setTitle("JIntellitype Test Application");
94 center(mainFrame);
95 mainFrame.setVisible(true);
96 mainFrame.initJIntellitype();
97 }
98
99
100
101
102
103 public void onHotKey(int aIdentifier) {
104 output("WM_HOTKEY message received " + Integer.toString(aIdentifier));
105 }
106
107
108
109
110
111 public void onIntellitype(int aCommand) {
112
113 switch (aCommand) {
114 case JIntellitype.APPCOMMAND_BROWSER_BACKWARD:
115 output("BROWSER_BACKWARD message received " + Integer.toString(aCommand));
116 break;
117 case JIntellitype.APPCOMMAND_BROWSER_FAVOURITES:
118 output("BROWSER_FAVOURITES message received " + Integer.toString(aCommand));
119 break;
120 case JIntellitype.APPCOMMAND_BROWSER_FORWARD:
121 output("BROWSER_FORWARD message received " + Integer.toString(aCommand));
122 break;
123 case JIntellitype.APPCOMMAND_BROWSER_HOME:
124 output("BROWSER_HOME message received " + Integer.toString(aCommand));
125 break;
126 case JIntellitype.APPCOMMAND_BROWSER_REFRESH:
127 output("BROWSER_REFRESH message received " + Integer.toString(aCommand));
128 break;
129 case JIntellitype.APPCOMMAND_BROWSER_SEARCH:
130 output("BROWSER_SEARCH message received " + Integer.toString(aCommand));
131 break;
132 case JIntellitype.APPCOMMAND_BROWSER_STOP:
133 output("BROWSER_STOP message received " + Integer.toString(aCommand));
134 break;
135 case JIntellitype.APPCOMMAND_LAUNCH_APP1:
136 output("LAUNCH_APP1 message received " + Integer.toString(aCommand));
137 break;
138 case JIntellitype.APPCOMMAND_LAUNCH_APP2:
139 output("LAUNCH_APP2 message received " + Integer.toString(aCommand));
140 break;
141 case JIntellitype.APPCOMMAND_LAUNCH_MAIL:
142 output("LAUNCH_MAIL message received " + Integer.toString(aCommand));
143 break;
144 case JIntellitype.APPCOMMAND_MEDIA_NEXTTRACK:
145 output("MEDIA_NEXTTRACK message received " + Integer.toString(aCommand));
146 break;
147 case JIntellitype.APPCOMMAND_MEDIA_PLAY_PAUSE:
148 output("MEDIA_PLAY_PAUSE message received " + Integer.toString(aCommand));
149 break;
150 case JIntellitype.APPCOMMAND_MEDIA_PREVIOUSTRACK:
151 output("MEDIA_PREVIOUSTRACK message received " + Integer.toString(aCommand));
152 break;
153 case JIntellitype.APPCOMMAND_MEDIA_STOP:
154 output("MEDIA_STOP message received " + Integer.toString(aCommand));
155 break;
156 case JIntellitype.APPCOMMAND_VOLUME_DOWN:
157 output("VOLUME_DOWN message received " + Integer.toString(aCommand));
158 break;
159 case JIntellitype.APPCOMMAND_VOLUME_UP:
160 output("VOLUME_UP message received " + Integer.toString(aCommand));
161 break;
162 case JIntellitype.APPCOMMAND_VOLUME_MUTE:
163 output("VOLUME_MUTE message received " + Integer.toString(aCommand));
164 break;
165 default:
166 output("Undefined INTELLITYPE message caught " + Integer.toString(aCommand));
167 break;
168 }
169 }
170
171
172
173
174
175
176 private static void center(JFrame aFrame) {
177 final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
178 final Point centerPoint = ge.getCenterPoint();
179 final Rectangle bounds = ge.getMaximumWindowBounds();
180 final int w = Math.min(aFrame.getWidth(), bounds.width);
181 final int h = Math.min(aFrame.getHeight(), bounds.height);
182 final int x = centerPoint.x - (w / 2);
183 final int y = centerPoint.y - (h / 2);
184 aFrame.setBounds(x, y, w, h);
185 if ((w == bounds.width) && (h == bounds.height)) {
186 aFrame.setExtendedState(Frame.MAXIMIZED_BOTH);
187 }
188 aFrame.validate();
189 }
190
191
192
193
194
195
196 private void btnRegisterHotKey_actionPerformed(ActionEvent aEvent) {
197
198 JIntellitype.getInstance().registerHotKey(WINDOWS_A, JIntellitype.MOD_WIN, (int) 'A');
199 JIntellitype.getInstance().registerHotKey(ALT_SHIFT_B, JIntellitype.MOD_ALT + JIntellitype.MOD_SHIFT, (int) 'B');
200 JIntellitype.getInstance().registerSwingHotKey(CTRL_SHIFT_C, Event.CTRL_MASK + Event.SHIFT_MASK, (int) 'C');
201
202
203 JIntellitype.getInstance().registerHotKey(PRINT_SCREEN, 0, 44);
204 JIntellitype.getInstance().registerHotKey(F9, 0, 120);
205 JIntellitype.getInstance().registerHotKey(F12, JIntellitype.MOD_ALT, 123);
206
207 textArea.setText("");
208 output("RegisterHotKey WINDOWS+A was assigned uniqueID 88");
209 output("RegisterHotKey ALT+SHIFT+B was assigned uniqueID 89");
210 output("RegisterHotKey CTRL+SHIFT+C was assigned uniqueID 90");
211 output("RegisterHotKey PRINT_SCREEN was assigned uniqueID 91");
212 output("RegisterHotKey F9 was assigned uniqueID 92");
213 output("RegisterHotKey F12 was assigned uniqueID 93");
214 output("Press WINDOWS+A or ALT+SHIFT+B or CTRL+SHIFT+C in another application and you will see the debug output in the textarea.");
215 }
216
217
218
219
220
221
222 private void btnUnregisterHotKey_actionPerformed(ActionEvent aEvent) {
223 JIntellitype.getInstance().unregisterHotKey(WINDOWS_A);
224 JIntellitype.getInstance().unregisterHotKey(ALT_SHIFT_B);
225 JIntellitype.getInstance().unregisterHotKey(CTRL_SHIFT_C);
226 JIntellitype.getInstance().unregisterHotKey(PRINT_SCREEN);
227 JIntellitype.getInstance().unregisterHotKey(F9);
228 JIntellitype.getInstance().unregisterHotKey(F12);
229 output("UnregisterHotKey WINDOWS+A");
230 output("UnregisterHotKey ALT+SHIFT+B");
231 output("UnregisterHotKey CTRL+SHIFT+C");
232 output("UnregisterHotKey PRINT_SCREEN");
233 output("UnregisterHotKey F9");
234 output("Press WINDOWS+A or ALT+SHIFT+B in another application and you will NOT see the debug output in the textarea.");
235 }
236
237
238
239
240 private void initComponents() {
241 mainPanel.setLayout(new BorderLayout());
242 topPanel.setBorder(new EtchedBorder(1));
243 bottomPanel.setLayout(new BorderLayout());
244 bottomPanel.setBorder(new EtchedBorder(1));
245 btnRegisterHotKey.setText("RegisterHotKey");
246 btnRegisterHotKey.addActionListener(new ActionListener() {
247 public void actionPerformed(ActionEvent e) {
248 btnRegisterHotKey_actionPerformed(e);
249 }
250 });
251 btnUnregisterHotKey.setText("UnregisterHotKey");
252 btnUnregisterHotKey.addActionListener(new ActionListener() {
253 public void actionPerformed(ActionEvent e) {
254 btnUnregisterHotKey_actionPerformed(e);
255 }
256 });
257 topPanel.add(btnRegisterHotKey);
258 topPanel.add(btnUnregisterHotKey);
259 scrollPane.getViewport().add(textArea);
260 bottomPanel.add(scrollPane, BorderLayout.CENTER);
261 mainPanel.add(topPanel, BorderLayout.NORTH);
262 mainPanel.add(bottomPanel, BorderLayout.CENTER);
263
264 this.addWindowListener(new java.awt.event.WindowAdapter() {
265 public void windowClosing(java.awt.event.WindowEvent evt) {
266
267 JIntellitype.getInstance().cleanUp();
268 System.exit(0);
269 }
270 });
271
272 this.getContentPane().add(mainPanel);
273 this.pack();
274 this.setSize(800, 600);
275 }
276
277
278
279
280
281 public void initJIntellitype() {
282 try {
283
284
285
286 JIntellitype.getInstance().addHotKeyListener(this);
287 JIntellitype.getInstance().addIntellitypeListener(this);
288 output("JIntellitype initialized");
289 } catch (RuntimeException ex) {
290 output("Either you are not on Windows, or there is a problem with the JIntellitype library!");
291 }
292 }
293
294
295
296
297
298
299 private void output(String text) {
300 textArea.append(text);
301 textArea.append("\n");
302 }
303
304 }