Jogamp
bugfix: sorry the mouse goes crazy
[users/sgothel/jake2.git] / src / jake2 / sys / JOGLKBD.java
CommitLineData
cadbb632
HZ
1package jake2.sys;
2
3import jake2.client.Key;
4
a6286626
HZ
5import java.awt.*;
6import java.awt.event.*;
cadbb632
HZ
7
8import javax.swing.ImageIcon;
9
10final public class JOGLKBD extends KBD
11{
12 static Robot robot;
13 public static InputListener listener = new InputListener();
14 static Cursor emptyCursor = null;
15 static Component c = null;
16
17 static int win_w2 = 0;
18 static int win_h2 = 0;
19
20 static {
21 try {
22 robot = new Robot();
23 } catch (AWTException e) {
24 System.exit(1);
25 }
26 }
27
28 public void Init() {
29 }
30
31 public void Update() {
32 // get events
33 HandleEvents();
34 }
35
36 public void Close() {
37 }
38
39 private void HandleEvents()
40 {
a4d8750b 41 int key;
cadbb632
HZ
42
43 Jake2InputEvent event;
44 while ( (event=InputListener.nextEvent()) != null ) {
45 switch(event.type) {
46 case Jake2InputEvent.KeyPress:
47 case Jake2InputEvent.KeyRelease:
48 Do_Key_Event(XLateKey((KeyEvent)event.ev), event.type == Jake2InputEvent.KeyPress);
49 break;
50
51 case Jake2InputEvent.MotionNotify:
52// if (IN.ignorefirst) {
53// IN.ignorefirst = false;
54// break;
55// }
56 if (IN.mouse_active) {
57 mx = (((MouseEvent)event.ev).getX() - win_w2) * 2;
58 my = (((MouseEvent)event.ev).getY() - win_h2) * 2;
59 } else {
60 mx = 0;
61 my = 0;
62 }
63 break;
a4d8750b 64 // see java.awt.MouseEvent
cadbb632 65 case Jake2InputEvent.ButtonPress:
a4d8750b
CW
66 key = mouseEventToKey((MouseEvent)event.ev);
67 Do_Key_Event(key, true);
cadbb632
HZ
68 break;
69
70 case Jake2InputEvent.ButtonRelease:
a4d8750b
CW
71 key = mouseEventToKey((MouseEvent)event.ev);
72 Do_Key_Event(key, false);
cadbb632 73 break;
a6286626
HZ
74
75 case Jake2InputEvent.WheelMoved:
76 int dir = ((MouseWheelEvent)event.ev).getWheelRotation();
77 if (dir > 0) {
78 Do_Key_Event(Key.K_MWHEELDOWN, true);
79 Do_Key_Event(Key.K_MWHEELDOWN, false);
80 } else {
81 Do_Key_Event(Key.K_MWHEELUP, true);
82 Do_Key_Event(Key.K_MWHEELUP, false);
83 }
84 break;
85
cadbb632
HZ
86 case Jake2InputEvent.CreateNotify :
87 case Jake2InputEvent.ConfigureNotify :
88 Component c = ((ComponentEvent)event.ev).getComponent();
89 win_x = 0;
90 win_y = 0;
91 win_w2 = c.getWidth() / 2;
92 win_h2 = c.getHeight() / 2;
93 while (c != null) {
94 if (c instanceof Container) {
95 Insets insets = ((Container)c).getInsets();
24dbc3ec
CW
96 win_x += insets.left;
97 win_y += insets.top;
cadbb632
HZ
98 }
99 win_x += c.getX();
100 win_y += c.getY();
101 c = c.getParent();
102 }
103 break;
104 }
105 }
106
107 if (mx != 0 || my != 0) {
108 // move the mouse to the window center again
109 robot.mouseMove(win_x + win_w2, win_y + win_h2);
110 }
111 }
112
a4d8750b
CW
113 // strange button numbering in java.awt.MouseEvent
114 // BUTTON1(left) BUTTON2(center) BUTTON3(right)
115 // K_MOUSE1 K_MOUSE3 K_MOUSE2
116 private final int mouseEventToKey(MouseEvent ev) {
117 switch (ev.getButton()) {
118 case MouseEvent.BUTTON3:
119 return Key.K_MOUSE2;
120 case MouseEvent.BUTTON2:
121 return Key.K_MOUSE3;
122 default:
123 return Key.K_MOUSE1;
124 }
125 }
126
cadbb632
HZ
127 private static int XLateKey(KeyEvent ev) {
128
129 int key = 0;
130 int code = ev.getKeyCode();
131
132 switch(code) {
133// 00626 case XK_KP_Page_Up: key = K_KP_PGUP; break;
134 case KeyEvent.VK_PAGE_UP: key = Key.K_PGUP; break;
135
136// 00629 case XK_KP_Page_Down: key = K_KP_PGDN; break;
137 case KeyEvent.VK_PAGE_DOWN: key = Key.K_PGDN; break;
138
139// 00632 case XK_KP_Home: key = K_KP_HOME; break;
140 case KeyEvent.VK_HOME: key = Key.K_HOME; break;
141
142// 00635 case XK_KP_End: key = K_KP_END; break;
143 case KeyEvent.VK_END: key = Key.K_END; break;
144
145 case KeyEvent.VK_KP_LEFT: key = Key.K_KP_LEFTARROW; break;
146 case KeyEvent.VK_LEFT: key = Key.K_LEFTARROW; break;
147
148 case KeyEvent.VK_KP_RIGHT: key = Key.K_KP_RIGHTARROW; break;
149 case KeyEvent.VK_RIGHT: key = Key.K_RIGHTARROW; break;
150
151 case KeyEvent.VK_KP_DOWN: key = Key.K_KP_DOWNARROW; break;
152 case KeyEvent.VK_DOWN: key = Key.K_DOWNARROW; break;
153
154 case KeyEvent.VK_KP_UP: key = Key.K_KP_UPARROW; break;
155 case KeyEvent.VK_UP: key = Key.K_UPARROW; break;
156
157 case KeyEvent.VK_ESCAPE: key = Key.K_ESCAPE; break;
158
159
160 case KeyEvent.VK_ENTER: key = Key.K_ENTER; break;
161// 00652 case XK_KP_Enter: key = K_KP_ENTER; break;
162
163 case KeyEvent.VK_TAB: key = Key.K_TAB; break;
164
165 case KeyEvent.VK_F1: key = Key.K_F1; break;
166 case KeyEvent.VK_F2: key = Key.K_F2; break;
167 case KeyEvent.VK_F3: key = Key.K_F3; break;
168 case KeyEvent.VK_F4: key = Key.K_F4; break;
169 case KeyEvent.VK_F5: key = Key.K_F5; break;
170 case KeyEvent.VK_F6: key = Key.K_F6; break;
171 case KeyEvent.VK_F7: key = Key.K_F7; break;
172 case KeyEvent.VK_F8: key = Key.K_F8; break;
173 case KeyEvent.VK_F9: key = Key.K_F9; break;
174 case KeyEvent.VK_F10: key = Key.K_F10; break;
175 case KeyEvent.VK_F11: key = Key.K_F11; break;
176 case KeyEvent.VK_F12: key = Key.K_F12; break;
177
178 case KeyEvent.VK_BACK_SPACE: key = Key.K_BACKSPACE; break;
179
180 case KeyEvent.VK_DELETE: key = Key.K_DEL; break;
181// 00683 case XK_KP_Delete: key = K_KP_DEL; break;
182
183 case KeyEvent.VK_PAUSE: key = Key.K_PAUSE; break;
184
185 case KeyEvent.VK_SHIFT: key = Key.K_SHIFT; break;
186 case KeyEvent.VK_CONTROL: key = Key.K_CTRL; break;
187
188 case KeyEvent.VK_ALT:
189 case KeyEvent.VK_ALT_GRAPH: key = Key.K_ALT; break;
190
191// 00700 case XK_KP_Begin: key = K_KP_5; break;
192// 00701
193 case KeyEvent.VK_INSERT: key = Key.K_INS; break;
194 // toggle console for DE and US keyboards
195 case KeyEvent.VK_DEAD_ACUTE:
196 case KeyEvent.VK_CIRCUMFLEX:
197 case KeyEvent.VK_DEAD_CIRCUMFLEX: key = '`'; break;
198
199 default:
684416e4
HZ
200 key = ev.getKeyChar();
201
cadbb632
HZ
202 if (key >= 'A' && key <= 'Z')
203 key = key - 'A' + 'a';
204 break;
205 }
206 if (key > 255) key = 0;
207
208 return key;
209 }
210
211 public void Do_Key_Event(int key, boolean down) {
db84d8bc 212 Key.Event(key, down, Timer.Milliseconds());
cadbb632
HZ
213 }
214
215 public void centerMouse() {
216 robot.mouseMove(win_x + win_w2, win_y + win_h2);
217 }
218
219 public void installGrabs()
220 {
221 if (emptyCursor == null) {
222 ImageIcon emptyIcon = new ImageIcon(new byte[0]);
223 emptyCursor = c.getToolkit().createCustomCursor(emptyIcon.getImage(), new Point(0, 0), "emptyCursor");
224 }
225 c.setCursor(emptyCursor);
b5892681 226 centerMouse();
cadbb632
HZ
227 }
228
229 public void uninstallGrabs()
230 {
231 c.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
232 }
233}
http://JogAmp.org git info: FAQ, tutorial and man pages.