1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 25 package org.openoffice.accessibility; 26 27 import com.sun.star.uno.UnoRuntime; 28 import com.sun.star.awt.XKeyHandler; 29 import org.openoffice.java.accessibility.AccessibleKeyBinding; 30 import org.openoffice.java.accessibility.Build; 31 32 import java.awt.*; 33 import java.awt.event.KeyEvent; 34 import javax.accessibility.*; 35 36 public class KeyHandler extends Component implements XKeyHandler, java.awt.KeyEventDispatcher { 37 EventQueue eventQueue; 38 39 public class VCLKeyEvent extends KeyEvent implements Runnable { 40 boolean consumed = true; 41 VCLKeyEvent(Component c, int id, int modifiers, int keyCode, char keyChar)42 public VCLKeyEvent(Component c, int id, int modifiers, int keyCode, char keyChar) { 43 super(c, id, System.currentTimeMillis(), modifiers, keyCode, keyChar); 44 } 45 run()46 public void run() { 47 // This is a no-op .. 48 } 49 setConsumed(boolean b)50 public void setConsumed(boolean b) { 51 consumed = b; 52 } 53 isConsumed()54 public boolean isConsumed() { 55 return consumed; 56 } 57 } 58 KeyHandler()59 public KeyHandler() { 60 eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue(); 61 java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this); 62 } 63 64 /** This method is called by the current KeyboardFocusManager requesting that this KeyEventDispatcher 65 * dispatch the specified event on its behalf 66 */ dispatchKeyEvent(java.awt.event.KeyEvent e)67 public boolean dispatchKeyEvent(java.awt.event.KeyEvent e) { 68 if (e instanceof VCLKeyEvent) { 69 VCLKeyEvent event = (VCLKeyEvent) e; 70 event.setConsumed(false); 71 return true; 72 } 73 return false; 74 } 75 76 /** Handler for KeyPressed events */ keyPressed(com.sun.star.awt.KeyEvent event)77 public boolean keyPressed(com.sun.star.awt.KeyEvent event) { 78 // try { 79 VCLKeyEvent vke = new VCLKeyEvent(this, KeyEvent.KEY_PRESSED, 80 AccessibleKeyBinding.convertModifiers(event.Modifiers), 81 AccessibleKeyBinding.convertKeyCode(event.KeyCode), 82 event.KeyChar != 0 ? event.KeyChar : KeyEvent.CHAR_UNDEFINED); 83 84 eventQueue.postEvent(vke); 85 86 // VCL events for TABs have empty KeyChar 87 if (event.KeyCode == com.sun.star.awt.Key.TAB ) { 88 event.KeyChar = '\t'; 89 } 90 91 // Synthesize KEY_TYPED event to emulate Java behavior 92 if (event.KeyChar != 0) { 93 eventQueue.postEvent(new VCLKeyEvent(this, 94 KeyEvent.KEY_TYPED, 95 AccessibleKeyBinding.convertModifiers(event.Modifiers), 96 KeyEvent.VK_UNDEFINED, 97 event.KeyChar)); 98 } 99 100 // Wait until the key event is processed 101 return false; 102 // eventQueue.invokeAndWait(vke); 103 // return vke.isConsumed(); 104 // } catch(java.lang.InterruptedException e) { 105 // return false; 106 // } catch(java.lang.reflect.InvocationTargetException e) { 107 // return false; 108 // } 109 } 110 111 /** Handler for KeyReleased events */ keyReleased(com.sun.star.awt.KeyEvent event)112 public boolean keyReleased(com.sun.star.awt.KeyEvent event) { 113 // try { 114 VCLKeyEvent vke = new VCLKeyEvent(this, KeyEvent.KEY_RELEASED, 115 AccessibleKeyBinding.convertModifiers(event.Modifiers), 116 AccessibleKeyBinding.convertKeyCode(event.KeyCode), 117 event.KeyChar != 0 ? event.KeyChar : KeyEvent.CHAR_UNDEFINED); 118 eventQueue.postEvent(vke); 119 120 // Wait until the key event is processed 121 return false; 122 // eventQueue.invokeAndWait(vke); 123 // return vke.isConsumed(); 124 // } catch(java.lang.InterruptedException e) { 125 // return false; 126 // } catch(java.lang.reflect.InvocationTargetException e) { 127 // return false; 128 // } 129 } 130 disposing(com.sun.star.lang.EventObject event)131 public void disposing(com.sun.star.lang.EventObject event) { 132 java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(this); 133 } 134 } 135