1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.embed.swt; 27 28 import org.eclipse.swt.SWT; 29 //import org.eclipse.swt.events.MouseEvent; 30 31 import java.lang.reflect.Method; 32 33 //import com.sun.glass.events.KeyEvent; 34 import com.sun.javafx.embed.AbstractEvents; 35 import org.eclipse.swt.widgets.Event; 36 37 import java.lang.reflect.InvocationTargetException; 38 39 /** 40 * An utility class to translate event types between embedded 41 * application and SWT. 42 * 43 */ 44 class SWTEvents { 45 46 /* 47 static int mouseIDToEmbedMouseType(int id) { 48 switch (id) { 49 case MouseEvent.MOUSE_PRESSED: 50 return AbstractEvents.MOUSEEVENT_PRESSED; 51 case MouseEvent.MOUSE_RELEASED: 52 return AbstractEvents.MOUSEEVENT_RELEASED; 53 case MouseEvent.MOUSE_CLICKED: 54 return AbstractEvents.MOUSEEVENT_CLICKED; 55 case MouseEvent.MOUSE_MOVED: 56 return AbstractEvents.MOUSEEVENT_MOVED; 57 case MouseEvent.MOUSE_DRAGGED: 58 return AbstractEvents.MOUSEEVENT_DRAGGED; 59 case MouseEvent.MOUSE_ENTERED: 60 return AbstractEvents.MOUSEEVENT_ENTERED; 61 case MouseEvent.MOUSE_EXITED: 62 return AbstractEvents.MOUSEEVENT_EXITED; 63 } 64 return 0; 65 } 66 */ 67 static int mouseButtonToEmbedMouseButton(int button, int extModifiers) { 68 switch (button) { 69 case 1: return AbstractEvents.MOUSEEVENT_PRIMARY_BUTTON; 70 case 2: return AbstractEvents.MOUSEEVENT_MIDDLE_BUTTON; 71 case 3: return AbstractEvents.MOUSEEVENT_SECONDARY_BUTTON; 72 } 73 return AbstractEvents.MOUSEEVENT_NONE_BUTTON; 74 } 75 76 static double getWheelRotation(Event e) { 77 int divisor = 1; 78 if ("win32".equals(SWT.getPlatform()) && e.type == SWT.MouseVerticalWheel) { 79 int [] linesToScroll = new int [1]; 80 //OS.SystemParametersInfo (OS.SPI_GETWHEELSCROLLLINES, 0, linesToScroll, 0); 81 try { 82 Class clazz = Class.forName("org.eclipse.swt.internal.win32.OS"); 83 Method method = clazz.getDeclaredMethod("SystemParametersInfo", new Class []{int.class, int.class, int [].class, int.class}); 84 method.invoke(clazz, 104 /*SPI_GETWHEELSCROLLLINES*/, 0, linesToScroll, 0); 85 } catch (IllegalAccessException iae) { 86 } catch (InvocationTargetException ite) { 87 } catch (NoSuchMethodException nme) { 88 } catch (ClassNotFoundException cfe) { 89 //Fail silently 90 } 91 if (linesToScroll [0] != -1 /*OS.WHEEL_PAGESCROLL*/) { 92 divisor = linesToScroll [0]; 93 } 94 } else if ("gtk".equals(SWT.getPlatform())) { 95 divisor = 3; 96 } 97 else if ("cocoa".equals(SWT.getPlatform())) { 98 divisor = Math.abs(e.count); 99 } 100 return e.count / (double) Math.max(1, divisor); 101 } 102 103 static int keyIDToEmbedKeyType(int id) { 104 switch (id) { 105 case SWT.KeyDown: 106 return AbstractEvents.KEYEVENT_PRESSED; 107 case SWT.KeyUp: 108 return AbstractEvents.KEYEVENT_RELEASED; 109 // case KeyEvent.KEY_TYPED: 110 // return AbstractEvents.KEYEVENT_TYPED; 111 } 112 return 0; 113 } 114 115 static final int [] [] KeyTable = { 116 117 {0x0 /*KeyEvent.VK_UNDEFINED*/, SWT.NULL}, 118 119 // SWT only 120 {'\n' /*KeyEvent.VK_?????*/, SWT.CR}, 121 122 // Misc 123 {'\n' /*KeyEvent.VK_ENTER*/, SWT.LF}, 124 {'\b' /*KeyEvent.VK_BACKSPACE*/, SWT.BS}, 125 {'\t' /*KeyEvent.VK_TAB*/, SWT.TAB}, 126 // {KeyEvent.VK_CANCEL SWT.???}, 127 // {KeyEvent.VK_CLEAR SWT.???}, 128 // {KeyEvent.VK_PAUSE SWT.???}, 129 {0x1B /*KeyEvent.VK_ESCAPE*/, SWT.ESC}, 130 {0x20 /*KeyEvent.VK_SPACE*/, 0x20}, 131 {0x7F /*KeyEvent.VK_DELETE*/, SWT.DEL}, 132 // {KeyEvent.VK_PRINTSCREEN SWT.???; 133 {0x9B /*KeyEvent.VK_INSERT*/, SWT.INSERT}, 134 {0x9C /*KeyEvent.VK_HELP*/, SWT.HELP}, 135 136 // Modifiers 137 {0x10 /*KeyEvent.VK_SHIFT*/, SWT.SHIFT}, 138 {0x11 /*KeyEvent.VK_CONTROL*/, SWT.CONTROL}, 139 {0x12 /*KeyEvent.VK_ALT*/, SWT.ALT}, 140 {0x020C /*(KeyEvent.VK_WINDOWS*/, SWT.COMMAND}, 141 // {KeyEvent.VK_CONTEXT_MENU, SWT.???}, 142 {0x14 /*KeyEvent.VK_CAPS_LOCK*/, SWT.CAPS_LOCK}, 143 {0x90 /*KeyEvent.VK_NUM_LOCK*/, SWT.NUM_LOCK}, 144 {0x91 /*KeyEvent.VK_SCROLL_LOCK*/, SWT.SCROLL_LOCK}, 145 146 // Navigation keys 147 {0x21 /*KeyEvent.VK_PAGE_UP*/, SWT.PAGE_UP}, 148 {0x22 /*KeyEvent.VK_PAGE_DOWN*/, SWT.PAGE_DOWN}, 149 {0x23 /*KeyEvent.VK_END*/, SWT.END}, 150 {0x24 /*KeyEvent.VK_HOME*/, SWT.HOME}, 151 {0x25 /*KeyEvent.VK_LEFT*/, SWT.ARROW_LEFT}, 152 {0x26 /*KeyEvent.VK_UP*/, SWT.ARROW_UP}, 153 {0x27 /*KeyEvent.VK_RIGHT*/, SWT.ARROW_RIGHT}, 154 {0x28 /*KeyEvent.VK_DOWN*/, SWT.ARROW_DOWN}, 155 156 // Misc 2 157 // NOTE: suspect this only works for English keyboard 158 {0x2C /*KeyEvent.VK_COMMA*/, ','}, // ',' 159 {0x2D /*KeyEvent.VK_MINUS*/, '-'}, // '-' 160 {0x2E /*KeyEvent.VK_PERIOD*/, '.'}, // '.' 161 {0x2F /*KeyEvent.VK_SLASH*/, '/'}, // '/' 162 {0x3B /*KeyEvent.VK_SEMICOLON*/, ';'}, // ';' 163 {0x3D /*KeyEvent.VK_EQUALS*/, '='}, // '=' 164 {0x5B /*KeyEvent.VK_OPEN_BRACKET*/, '['}, // '[' 165 {0x5C /*KeyEvent.VK_BACK_SLASH*/, '\\'}, // '\' 166 {0x5D /*KeyEvent.VK_CLOSE_BRACKET*/, ']'}, // ']' 167 168 // Numeric key pad keys 169 {0x6A /*KeyEvent.VK_MULTIPLY*/, SWT.KEYPAD_MULTIPLY}, // '*' 170 {0x6B /*KeyEvent.VK_ADD*/, SWT.KEYPAD_ADD}, // '+' 171 // {0x6C /*KeyEvent.VK_SEPARATOR*/, SWT.???}, 172 {0x6D /*KeyEvent.VK_SUBTRACT*/, SWT.KEYPAD_SUBTRACT}, 173 {0x6E /*KeyEvent.VK_DECIMAL*/, SWT.KEYPAD_DECIMAL}, 174 {0x6F /*KeyEvent.VK_DIVIDE*/, SWT.KEYPAD_DIVIDE}, 175 // {0x?? /*KeyEvent.VK_????*/, SWT.KEYPAD_EQUAL}, 176 // {0x?? /*KeyEvent.VK_????*/, SWT.KEYPAD_CR}, 177 178 {0x96 /*KeyEvent.VK_AMPERSAND*/, '@'}, 179 {0x97 /*KeyEvent.VK_ASTERISK*/, '*'}, 180 181 {0x98 /*KeyEvent.VK_DOUBLE_QUOTE*/, '"'}, // '"' 182 {0x99 /*KeyEvent.VK_LESS*/, '<'}, // '<' 183 {0xa0 /*KeyEvent.VK_GREATER*/, '>'}, // '>' 184 {0xa1 /*KeyEvent.VK_BRACELEFT*/, '{'}, // '{' 185 {0xa2 /*KeyEvent.VK_BRACERIGHT*/, '}'}, // '}' 186 {0xC0 /*KeyEvent.VK_BACK_QUOTE*/, '`'}, // '`' 187 {0xDE /*KeyEvent.VK_QUOTE*/, '\''}, // ''' 188 {0x0200 /*KeyEvent.VK_AT*/, '@'}, // '@' 189 {0x0201 /*KeyEvent.VK_COLON*/, ':'}, // ':' 190 {0x0202 /*KeyEvent.VK_CIRCUMFLEX*/, '^'}, // '^' 191 {0x0203 /*KeyEvent.VK_DOLLAR*/, '$'}, // '$' 192 // {KeyEvent.VK_EURO_SIGN, 0x0204}, 193 {0x0205 /*KeyEvent.VK_EXCLAMATION*/, '!'}, // '!' 194 // {KeyEvent.VK_INV_EXCLAMATION, 0x0206}, 195 {0x0207 /*KeyEvent.VK_LEFT_PARENTHESIS*/, '('}, // '(' 196 {0x0208 /*KeyEvent.VK_NUMBER_SIGN*/, '#'}, // '#' 197 {0x0209 /*KeyEvent.VK_PLUS*/, '+'}, // '+' 198 {0x020A /*KeyEvent.VK_RIGHT_PARENTHESIS*/, ')'}, // ')' 199 {0x020B /*KeyEvent.VK_UNDERSCORE*/, '_'}, // '_' 200 201 // Numeric keys 202 // NOTE: suspect this only works for English keyboard 203 {0x30 /*KeyEvent.VK_0*/, '0'}, // '0' 204 {0x31 /*KeyEvent.VK_1*/, '1'}, // '1' 205 {0x32 /*KeyEvent.VK_2*/, '2'}, // '2' 206 {0x33 /*KeyEvent.VK_3*/, '3'}, // '3' 207 {0x34 /*KeyEvent.VK_4*/, '4'}, // '4' 208 {0x35 /*KeyEvent.VK_5*/, '5'}, // '5' 209 {0x36 /*KeyEvent.VK_6*/, '6'}, // '6' 210 {0x37 /*KeyEvent.VK_7*/, '7'}, // '7' 211 {0x38 /*KeyEvent.VK_8*/, '8'}, // '8' 212 {0x39 /*KeyEvent.VK_9*/, '9'}, // '9' 213 214 // Alpha keys 215 // NOTE: suspect this only works for English keyboard 216 {0x41 /*KeyEvent.VK_A*/, 'a'}, // 'A' 217 {0x42 /*KeyEvent.VK_B*/, 'b'}, // 'B' 218 {0x43 /*KeyEvent.VK_C*/, 'c'}, // 'C' 219 {0x44 /*KeyEvent.VK_D*/, 'd'}, // 'D' 220 {0x45 /*KeyEvent.VK_E*/, 'e'}, // 'E' 221 {0x46 /*KeyEvent.VK_F*/, 'f'}, // 'F' 222 {0x47 /*KeyEvent.VK_G*/, 'g'}, // 'G' 223 {0x48 /*KeyEvent.VK_H*/, 'h'}, // 'H' 224 {0x49 /*KeyEvent.VK_I*/, 'i'}, // 'I' 225 {0x4A /*KeyEvent.VK_J*/, 'j'}, // 'J' 226 {0x4B /*KeyEvent.VK_K*/, 'k'}, // 'K' 227 {0x4C /*KeyEvent.VK_L*/, 'l'}, // 'L' 228 {0x4D /*KeyEvent.VK_M*/, 'm'}, // 'M' 229 {0x4E /*KeyEvent.VK_N*/, 'n'}, // 'N' 230 {0x4F /*KeyEvent.VK_O*/, 'o'}, // 'O' 231 {0x50 /*KeyEvent.VK_P*/, 'p'}, // 'P' 232 {0x51 /*KeyEvent.VK_Q*/, 'q'}, // 'Q' 233 {0x52 /*KeyEvent.VK_R*/, 'r'}, // 'R' 234 {0x53 /*KeyEvent.VK_S*/, 's'}, // 'S' 235 {0x54 /*KeyEvent.VK_T*/, 't'}, // 'T' 236 {0x55 /*KeyEvent.VK_U*/, 'u'}, // 'U' 237 {0x56 /*KeyEvent.VK_V*/, 'v'}, // 'V' 238 {0x57 /*KeyEvent.VK_W*/, 'w'}, // 'W' 239 {0x58 /*KeyEvent.VK_X*/, 'x'}, // 'X' 240 {0x59 /*KeyEvent.VK_Y*/, 'y'}, // 'Y' 241 {0x5A /*KeyEvent.VK_Z*/, 'z'}, // 'Z' 242 243 // Numpad keys 244 {0x60 /*KeyEvent.VK_NUMPAD0*/, SWT.KEYPAD_0}, 245 {0x61 /*KeyEvent.VK_NUMPAD1*/, SWT.KEYPAD_1}, 246 {0x62 /*KeyEvent.VK_NUMPAD2*/, SWT.KEYPAD_2}, 247 {0x63 /*KeyEvent.VK_NUMPAD3*/, SWT.KEYPAD_3}, 248 {0x64 /*KeyEvent.VK_NUMPAD4*/, SWT.KEYPAD_4}, 249 {0x65 /*KeyEvent.VK_NUMPAD5*/, SWT.KEYPAD_5}, 250 {0x66 /*KeyEvent.VK_NUMPAD6*/, SWT.KEYPAD_6}, 251 {0x67 /*KeyEvent.VK_NUMPAD7*/, SWT.KEYPAD_7}, 252 {0x68 /*KeyEvent.VK_NUMPAD8*/, SWT.KEYPAD_8}, 253 {0x69 /*KeyEvent.VK_NUMPAD9*/, SWT.KEYPAD_9}, 254 255 // Function keys 256 {0x70 /*KeyEvent.VK_F1*/, SWT.F1}, 257 {0x71 /*KeyEvent.VK_F2*/, SWT.F2}, 258 {0x72 /*KeyEvent.VK_F3*/, SWT.F3}, 259 {0x73 /*KeyEvent.VK_F4*/, SWT.F4}, 260 {0x74 /*KeyEvent.VK_F5*/, SWT.F5}, 261 {0x75 /*KeyEvent.VK_F6*/, SWT.F6}, 262 {0x76 /*KeyEvent.VK_F7*/, SWT.F7}, 263 {0x77 /*KeyEvent.VK_F8*/, SWT.F8}, 264 {0x78 /*KeyEvent.VK_F9*/, SWT.F9}, 265 {0x79 /*KeyEvent.VK_F10*/, SWT.F10}, 266 {0x7A /*KeyEvent.VK_F11*/, SWT.F11}, 267 {0x7B /*KeyEvent.VK_F12*/, SWT.F12}, 268 }; 269 270 // RT-27940: map these to Fx keys 271 // /* Numeric Keypad Keys */ 272 // {KeyEvent.VK_MULTIPLY, SWT.KEYPAD_MULTIPLY}, 273 // {KeyEvent.VK_ADD, SWT.KEYPAD_ADD}, 274 // {KeyEvent.VK_RETURN, SWT.KEYPAD_CR}, 275 // {KeyEvent.VK_SUBTRACT, SWT.KEYPAD_SUBTRACT}, 276 // {KeyEvent.VK_DECIMAL, SWT.KEYPAD_DECIMAL}, 277 // {KeyEvent.VK_DIVIDE, SWT.KEYPAD_DIVIDE}, 278 //// {KeyEvent.VK_????, SWT.KEYPAD_EQUAL}, 279 280 static int keyCodeToEmbedKeyCode(int keyCode) { 281 for (int i=0; i<KeyTable.length; i++) { 282 if (KeyTable [i] [1] == keyCode) return KeyTable [i] [0]; 283 } 284 return 0; 285 } 286 287 static int keyModifiersToEmbedKeyModifiers(int extModifiers) { 288 int embedModifiers = 0; 289 if ((extModifiers & SWT.SHIFT) != 0) { 290 embedModifiers |= AbstractEvents.MODIFIER_SHIFT; 291 } 292 if ((extModifiers & SWT.CTRL) != 0) { 293 embedModifiers |= AbstractEvents.MODIFIER_CONTROL; 294 } 295 if ((extModifiers & SWT.ALT) != 0) { 296 embedModifiers |= AbstractEvents.MODIFIER_ALT; 297 } 298 // NOTE: can't get Windows key from SWT 299 if ((extModifiers & SWT.COMMAND) != 0) { 300 embedModifiers |= AbstractEvents.MODIFIER_META; 301 } 302 return embedModifiers; 303 } 304 }