private boolean proceedKeyEvent(KeyEvent event, KeyStroke stroke) { if (myInputMap.get(stroke) != null) { final Action action = myActionMap.get(myInputMap.get(stroke)); if (action != null && action.isEnabled()) { action.actionPerformed( new ActionEvent( getContent(), event.getID(), "", event.getWhen(), event.getModifiers())); return true; } } return false; }
/* * Post an ActionEvent to the target (on * keydown) and the item is enabled. * Returns true if there is an associated shortcut. */ boolean handleShortcut(KeyEvent e) { MenuShortcut s = new MenuShortcut(e.getKeyCode(), (e.getModifiers() & InputEvent.SHIFT_MASK) > 0); // Fix For 6185151: Menu shortcuts of all menuitems within a menu // should be disabled when the menu itself is disabled if (s.equals(shortcut) && isItemEnabled()) { // MenuShortcut match -- issue an event on keydown. if (e.getID() == KeyEvent.KEY_PRESSED) { doMenuEvent(e.getWhen(), e.getModifiers()); } else { // silently eat key release. } return true; } return false; }
/** * Processes a key event forwarded from the <code>MenuSelectionManager</code> and changes the menu * selection, if necessary, by using <code>MenuSelectionManager</code>'s API. * * <p>Note: you do not have to forward the event to sub-components. This is done automatically by * the <code>MenuSelectionManager</code>. * * @param e a <code>KeyEvent</code> * @param path the <code>MenuElement</code> path array * @param manager the <code>MenuSelectionManager</code> */ public void processKeyEvent(KeyEvent e, MenuElement path[], MenuSelectionManager manager) { MenuKeyEvent mke = new MenuKeyEvent( e.getComponent(), e.getID(), e.getWhen(), e.getModifiers(), e.getKeyCode(), e.getKeyChar(), path, manager); processMenuKeyEvent(mke); if (mke.isConsumed()) { e.consume(); } }
/** * Searches the contact list when any key, different from "space", "+" or "-" is typed. Selects * the Contact name closest to the typed string. The time between two button presses is checked to * determine whether the user makes a new search or a continuous search. When user types the same * letter consecutively the search mechanism selects the next Contact name starting with the same * letter. */ public void keyTyped(KeyEvent e) { // Nothing to do if the contact list is empty if (contactList.getModel().getSize() <= 0) return; long eventTimestamp = e.getWhen(); char keyChar = e.getKeyChar(); if (keyChar == ' ') { openOrCloseGroup(); } else if (keyChar == '+') { openGroup(); } else if (keyChar == '-') { closeGroup(); } else { if ((lastTypedTimestamp - eventTimestamp) > 1000) { keyBuffer.delete(0, keyBuffer.length() - 1); } this.lastTypedTimestamp = eventTimestamp; this.keyBuffer.append(keyChar); boolean selectedSameLetterContact = false; int selectedIndex = this.contactList.getSelectedIndex(); // Check if there's any selected contact node and get its name. if (selectedIndex != -1) { Object selectedObject = this.contactList.getSelectedValue(); if (selectedObject instanceof MetaContact) { String selectedContactName = ((MetaContact) selectedObject).getDisplayName(); if (selectedContactName != null) { selectedSameLetterContact = selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString()); } } else if (selectedObject instanceof ConferenceChatContact) { String selectedContactName = ((ConferenceChatContact) selectedObject).getName(); if (selectedContactName != null) { selectedSameLetterContact = selectedContactName.substring(0, 1).equalsIgnoreCase(keyBuffer.toString()); } } } // The search starts from the beginning if: // 1) the newly entered character is different from the last one // or // 2) the currently selected contact starts with a different letter int contactIndex = contactList.getNextMatch( keyBuffer.toString(), (lastTypedKey != keyChar || !selectedSameLetterContact) ? 0 : selectedIndex + 1, Position.Bias.Forward); int currentlySelectedIndex = this.contactList.getSelectedIndex(); if (currentlySelectedIndex != contactIndex && contactIndex != -1) { this.contactList.setSelectedIndex(contactIndex); currentlySelectedIndex = contactList.getSelectedIndex(); } this.contactList.ensureIndexIsVisible(currentlySelectedIndex); this.lastTypedKey = keyChar; } }
public void keyTyped(KeyEvent e) { if (debug) { System.out.println( "--- RecordingModule: key typed = " + e + "\n > Key char->int = " + (int) e.getKeyChar()); System.out.println(" -- isActionKey() = " + e.isActionKey()); System.out.println(" -- isISOControl() = " + Character.isISOControl(e.getKeyChar())); System.out.println(" -- isWhitespace() = " + Character.isWhitespace(e.getKeyChar())); } if (isKeyReserved(e)) { return; } if (enabled && !readOnly && lastKeyPressEvent != null) { if (enableKeyboard) { boolean replace = false; String text = ""; if (isControl(e)) { if (lastKeyPressEvent.getKeyCode() == KeyEvent.VK_ENTER) { // Change the Type cmd prior to Typeline if the delay from the last type key is less // than 1 sec if (useTypeline && e.getModifiers() == 0 && lastElement != null) { String s = DocumentUtils.getElementText(lastElement); if (s.startsWith("Type ") && (System.currentTimeMillis() - lastInsertTime) < typelineDelay) { replace = true; text = s.replaceFirst("Type", "Typeline"); } } } if ("".equals(text)) { int count = 1; KeyEvent e2; long lastEventTime = e.getWhen(); // We go through the vector of events and check whether there are events corresponding // to a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (e.getID() == e2.getID() && e.getKeyChar() == e2.getKeyChar() && e.getKeyCode() == e2.getKeyCode() && e.getModifiers() == e2.getModifiers() && (lastEventTime - e2.getWhen() < keyMutiDelay)) { count++; replace = true; lastEventTime = e2.getWhen(); } else { break; } } text = "Press "; // String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers()); String modifiers = parser.modifiersToString(e.getModifiers()); if (!"".equals(modifiers)) { text += modifiers + "+"; } String charText = KeyEvent.getKeyText(lastKeyPressEvent.getKeyCode()); if (charText == null) { charText = "<unknown>"; } text += charText; if (count > 1) { text += " " + PressCommand.PARAM_COUNT + "=" + count; } if (debug) { System.out.println("--- RecordingModule: Inserting '" + text + "'"); } } } else { text = "" + e.getKeyChar(); KeyEvent e2; // We go through the vector of events and check whether there are events corresponding to // a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (!isControl(e2) && !e2.isActionKey()) { text = e2.getKeyChar() + text; replace = true; } else { break; } } text = "Type \"" + Utils.escapeUnescapedDoubleQuotes(text) + "\""; } // Insert the command to the current editor insertLine(text, replace, true, false); } insertEvent(e); } }
public void keyPressed(KeyEvent e) { if (debug) { System.out.println( "--- RecordingModule: key pressed = " + e + "\n > Key char->int = " + (int) e.getKeyChar()); } // Here we process just action keys because they do not generate KEY_TYPED events. // Other key events are handled by the keyTyped method. lastInteractionTime = System.currentTimeMillis(); if (isKeyReserved(e)) { return; } if (enabled && !readOnly) { // System.out.println("keyPressed (e.isActionKey()=)"+e.isActionKey()+": // "+e.toString()); // TODO: implement text corrections in type like Delete, Backspace if (e.isActionKey()) { if (enableKeyboard) { int count = 1; KeyEvent e2; long lastEventTime = e.getWhen(); // We go through the vector of events and check whether there are events corresponding to // a typed text. for (int i = 0; i < events.size() && events.get(i) instanceof KeyEvent; i++) { e2 = (KeyEvent) events.get(i); if (e.getID() == e2.getID() && e.getKeyChar() == e2.getKeyChar() && e.getKeyCode() == e2.getKeyCode() && e.getModifiers() == e2.getModifiers() && (lastEventTime - e2.getWhen() < keyMutiDelay)) { count++; lastEventTime = e2.getWhen(); } else { break; } } String text = "Press "; // String modifiers = KeyEvent.getKeyModifiersText(e.getModifiers()); String modifiers = parser.modifiersToString(e.getModifiers()); if (!"".equals(modifiers)) { text += modifiers + "+"; } String charText = (String) keyCodes.get(new Integer(e.getKeyCode())); if (charText == null) { charText = "<unknown>"; } text += charText; if (count > 1) { text += " " + PressCommand.PARAM_COUNT + "=" + count; } // text += '\n'; if (debug) { System.out.println("--- RecordingModule: Inserting '" + text + "'"); } // Insert the command to the current editor insertLine(text, count > 1, true, false); } insertEvent(e); } lastKeyPressEvent = e; } }
/** * Converts a new event to an old one (used for compatibility). If the new event cannot be * converted (because no old equivalent exists) then this returns null. * * <p>Note: this method is here instead of in each individual new event class in java.awt.event * because we don't want to make it public and it needs to be called from java.awt. */ Event convertToOld() { Object src = getSource(); int newid = id; switch (id) { case KeyEvent.KEY_PRESSED: case KeyEvent.KEY_RELEASED: KeyEvent ke = (KeyEvent) this; if (ke.isActionKey()) { newid = (id == KeyEvent.KEY_PRESSED ? Event.KEY_ACTION : Event.KEY_ACTION_RELEASE); } int keyCode = ke.getKeyCode(); if (keyCode == KeyEvent.VK_SHIFT || keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_ALT) { return null; // suppress modifier keys in old event model. } // no mask for button1 existed in old Event - strip it out return new Event( src, ke.getWhen(), newid, 0, 0, Event.getOldEventKey(ke), (ke.getModifiers() & ~InputEvent.BUTTON1_MASK)); case MouseEvent.MOUSE_PRESSED: case MouseEvent.MOUSE_RELEASED: case MouseEvent.MOUSE_MOVED: case MouseEvent.MOUSE_DRAGGED: case MouseEvent.MOUSE_ENTERED: case MouseEvent.MOUSE_EXITED: MouseEvent me = (MouseEvent) this; // no mask for button1 existed in old Event - strip it out Event olde = new Event( src, me.getWhen(), newid, me.getX(), me.getY(), 0, (me.getModifiers() & ~InputEvent.BUTTON1_MASK)); olde.clickCount = me.getClickCount(); return olde; case FocusEvent.FOCUS_GAINED: return new Event(src, Event.GOT_FOCUS, null); case FocusEvent.FOCUS_LOST: return new Event(src, Event.LOST_FOCUS, null); case WindowEvent.WINDOW_CLOSING: case WindowEvent.WINDOW_ICONIFIED: case WindowEvent.WINDOW_DEICONIFIED: return new Event(src, newid, null); case ComponentEvent.COMPONENT_MOVED: if (src instanceof Frame || src instanceof Dialog) { Point p = ((Component) src).getLocation(); return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0); } break; case ActionEvent.ACTION_PERFORMED: ActionEvent ae = (ActionEvent) this; String cmd; if (src instanceof Button) { cmd = ((Button) src).getLabel(); } else if (src instanceof MenuItem) { cmd = ((MenuItem) src).getLabel(); } else { cmd = ae.getActionCommand(); } return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd); case ItemEvent.ITEM_STATE_CHANGED: ItemEvent ie = (ItemEvent) this; Object arg; if (src instanceof List) { newid = (ie.getStateChange() == ItemEvent.SELECTED ? Event.LIST_SELECT : Event.LIST_DESELECT); arg = ie.getItem(); } else { newid = Event.ACTION_EVENT; if (src instanceof Choice) { arg = ie.getItem(); } else { // Checkbox arg = new Boolean(ie.getStateChange() == ItemEvent.SELECTED); } } return new Event(src, newid, arg); case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED: AdjustmentEvent aje = (AdjustmentEvent) this; switch (aje.getAdjustmentType()) { case AdjustmentEvent.UNIT_INCREMENT: newid = Event.SCROLL_LINE_DOWN; break; case AdjustmentEvent.UNIT_DECREMENT: newid = Event.SCROLL_LINE_UP; break; case AdjustmentEvent.BLOCK_INCREMENT: newid = Event.SCROLL_PAGE_DOWN; break; case AdjustmentEvent.BLOCK_DECREMENT: newid = Event.SCROLL_PAGE_UP; break; case AdjustmentEvent.TRACK: if (aje.getValueIsAdjusting()) { newid = Event.SCROLL_ABSOLUTE; } else { newid = Event.SCROLL_END; } break; default: return null; } return new Event(src, newid, new Integer(aje.getValue())); default: } return null; }