1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.g2d.event.adapter;
18 import java.awt.event.KeyEvent;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.KeyListener;
22 import org.simantics.scenegraph.g2d.events.Event;
23 import org.simantics.scenegraph.g2d.events.EventDebugPolicy;
24 import org.simantics.scenegraph.g2d.events.IEventHandler;
25 import org.simantics.scenegraph.g2d.events.IEventQueue;
26 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
27 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;
28 import org.simantics.scenegraph.g2d.events.adapter.AbstractEventAdapter;
31 * Adapts SWT key events to G2D Event
34 * @author Toni Kalajainen
36 public class SWTKeyEventAdapter extends AbstractEventAdapter implements KeyListener {
38 private final boolean DEBUG = EventDebugPolicy.SWT_KEY_EVENT_ADAPTION;
40 public SWTKeyEventAdapter(Object sender, IEventHandler delegator) {
41 super(sender, delegator);
44 public SWTKeyEventAdapter(Object sender, IEventQueue queue) {
49 public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
51 System.out.println("SWT key pressed: " + e);
53 new KeyPressedEvent(sender, e.time, e.character, adaptSWTKeyCodeToAWTKeyCode(e.keyCode), adaptSWTModifierToAWTModifier(e.stateMask)));
57 public void keyReleased(org.eclipse.swt.events.KeyEvent e) {
59 System.out.println("SWT key released: " + e);
61 new KeyReleasedEvent(sender, e.time, e.character, adaptSWTKeyCodeToAWTKeyCode(e.keyCode), adaptSWTModifierToAWTModifier(e.stateMask)));
64 public static int adaptSWTKeyCodeToAWTKeyCode(int swtKeyCode)
69 if (swtKeyCode>=KeyEvent.VK_A+0x20 && swtKeyCode<=KeyEvent.VK_Z+0x20)
70 return swtKeyCode - 0x20;
73 case SWT.ARROW_UP: return KeyEvent.VK_UP;
74 case SWT.ARROW_DOWN: return KeyEvent.VK_DOWN;
75 case SWT.ARROW_LEFT: return KeyEvent.VK_LEFT;
76 case SWT.ARROW_RIGHT: return KeyEvent.VK_RIGHT;
77 case SWT.PAGE_UP: return KeyEvent.VK_PAGE_UP;
78 case SWT.PAGE_DOWN: return KeyEvent.VK_PAGE_DOWN;
79 case SWT.HOME: return KeyEvent.VK_HOME;
80 case SWT.END: return KeyEvent.VK_END;
81 case SWT.INSERT: return KeyEvent.VK_INSERT;
82 case SWT.F1: return KeyEvent.VK_F1;
83 case SWT.F2: return KeyEvent.VK_F2;
84 case SWT.F3: return KeyEvent.VK_F3;
85 case SWT.F4: return KeyEvent.VK_F4;
86 case SWT.F5: return KeyEvent.VK_F5;
87 case SWT.F6: return KeyEvent.VK_F6;
88 case SWT.F7: return KeyEvent.VK_F7;
89 case SWT.F8: return KeyEvent.VK_F8;
90 case SWT.F9: return KeyEvent.VK_F9;
91 case SWT.F10: return KeyEvent.VK_F10;
92 case SWT.F11: return KeyEvent.VK_F11;
93 case SWT.F12: return KeyEvent.VK_F12;
94 case SWT.F13: return KeyEvent.VK_F13;
95 case SWT.F14: return KeyEvent.VK_F14;
96 case SWT.F15: return KeyEvent.VK_F15;
97 case SWT.KEYPAD_MULTIPLY: return KeyEvent.VK_MULTIPLY;
98 case SWT.KEYPAD_ADD: return KeyEvent.VK_ADD;
99 case SWT.KEYPAD_SUBTRACT: return KeyEvent.VK_SUBTRACT;
100 case SWT.KEYPAD_DECIMAL: return KeyEvent.VK_DECIMAL;
101 case SWT.KEYPAD_DIVIDE: return KeyEvent.VK_DIVIDE;
102 case SWT.KEYPAD_0: return KeyEvent.VK_NUMPAD0;
103 case SWT.KEYPAD_1: return KeyEvent.VK_NUMPAD1;
104 case SWT.KEYPAD_2: return KeyEvent.VK_NUMPAD2;
105 case SWT.KEYPAD_3: return KeyEvent.VK_NUMPAD3;
106 case SWT.KEYPAD_4: return KeyEvent.VK_NUMPAD4;
107 case SWT.KEYPAD_5: return KeyEvent.VK_NUMPAD5;
108 case SWT.KEYPAD_6: return KeyEvent.VK_NUMPAD6;
109 case SWT.KEYPAD_7: return KeyEvent.VK_NUMPAD7;
110 case SWT.KEYPAD_8: return KeyEvent.VK_NUMPAD8;
111 case SWT.KEYPAD_9: return KeyEvent.VK_NUMPAD9;
112 case SWT.KEYPAD_EQUAL: return KeyEvent.VK_EQUALS;
113 case SWT.KEYPAD_CR: return KeyEvent.VK_ENTER; // wrong?
114 case SWT.HELP: return KeyEvent.VK_HELP;
115 case SWT.CAPS_LOCK: return KeyEvent.VK_CAPS_LOCK;
116 case SWT.NUM_LOCK: return KeyEvent.VK_NUM_LOCK;
117 case SWT.SCROLL_LOCK: return KeyEvent.VK_NUM_LOCK;
118 case SWT.PAUSE: return KeyEvent.VK_PAUSE;
119 //case SWT.BREAK: return KeyEvent.vk_
120 case SWT.PRINT_SCREEN: return KeyEvent.VK_PRINTSCREEN;
121 case '\'': return KeyEvent.VK_QUOTE;
123 case SWT.DEL: return KeyEvent.VK_DELETE;
124 case SWT.CR: return KeyEvent.VK_ENTER;
125 case SWT.ESC: return KeyEvent.VK_ESCAPE;
126 case SWT.TAB: return KeyEvent.VK_TAB;
127 case SWT.ALT: return KeyEvent.VK_ALT;
128 case SWT.SHIFT: return KeyEvent.VK_SHIFT;
129 case SWT.CTRL: return KeyEvent.VK_CONTROL;
130 case SWT.COMMAND: return KeyEvent.VK_META;
131 case SWT.BS: return KeyEvent.VK_BACK_SPACE;
132 //case SWT.: return KeyEvent.VK_;
135 if (c>=0 && c<255) return c;
141 public static int adaptSWTModifierToAWTModifier(int swtModifier)
146 boolean alt = (c & SWT.ALT) != 0;
147 boolean ctrl = (c & SWT.CTRL) != 0;
148 boolean shift = (c & SWT.SHIFT) != 0;
149 boolean command = (c & SWT.COMMAND) != 0;
151 if (alt) result |= KeyEvent.ALT_DOWN_MASK | KeyEvent.ALT_MASK;
152 if (ctrl) result |= KeyEvent.CTRL_DOWN_MASK | KeyEvent.CTRL_MASK;
153 if (alt && ctrl) result |= KeyEvent.ALT_GRAPH_DOWN_MASK | KeyEvent.ALT_GRAPH_MASK;
154 if (shift) result |= KeyEvent.SHIFT_DOWN_MASK | KeyEvent.SHIFT_MASK;
155 if (command) result |= KeyEvent.META_DOWN_MASK | KeyEvent.META_MASK;