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