📄 defaultkeyboardfocusmanager.java
字号:
} } return oneEnqueued; } public boolean dispatchKeyEvent (KeyEvent e) { Component focusOwner = getGlobalPermanentFocusOwner (); if (focusOwner != null) redispatchEvent(focusOwner, e); // Loop through all registered KeyEventPostProcessors, giving // each a chance to process this event. Iterator i = getKeyEventPostProcessors().iterator(); while (i.hasNext ()) { KeyEventPostProcessor processor = (KeyEventPostProcessor) i.next (); if (processor.postProcessKeyEvent ((KeyEvent) e)) return true; } // The event hasn't been consumed yet. Check if it is an // MenuShortcut. if (postProcessKeyEvent (e)) return true; // Always return true. return true; } public boolean postProcessKeyEvent (KeyEvent e) { // Check if this event represents a menu shortcut. // MenuShortcuts are activated by Ctrl- KeyEvents, only on KEY_PRESSED. int modifiers = e.getModifiersEx (); if (e.getID() == KeyEvent.KEY_PRESSED && (modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) { Window focusedWindow = getGlobalFocusedWindow (); if (focusedWindow instanceof Frame) { MenuBar menubar = ((Frame) focusedWindow).getMenuBar (); if (menubar != null) { // If there's a menubar, loop through all menu items, // checking whether each one has a shortcut, and if // so, whether this key event should activate it. int numMenus = menubar.getMenuCount (); for (int i = 0; i < numMenus; i++) { Menu menu = menubar.getMenu (i); int numItems = menu.getItemCount (); for (int j = 0; j < numItems; j++) { MenuItem item = menu.getItem (j); MenuShortcut shortcut = item.getShortcut (); if (item.isEnabled() && shortcut != null) { // Dispatch a new ActionEvent if: // // a) this is a Shift- KeyEvent, and the // shortcut requires the Shift modifier // // or, b) this is not a Shift- KeyEvent, and the // shortcut does not require the Shift // modifier. if (shortcut.getKey () == e.getKeyCode () && ((shortcut.usesShiftModifier () && (modifiers & KeyEvent.SHIFT_DOWN_MASK) != 0) || (! shortcut.usesShiftModifier () && (modifiers & KeyEvent.SHIFT_DOWN_MASK) == 0))) { item.dispatchEvent (new ActionEvent (item, ActionEvent.ACTION_PERFORMED, item.getActionCommand (), modifiers)); // The event was dispatched. return true; } } } } } } } return false; } public void processKeyEvent (Component comp, KeyEvent e) { AWTKeyStroke eventKeystroke = AWTKeyStroke.getAWTKeyStrokeForEvent (e); // For every focus traversal keystroke, we need to also consume // the other two key event types for the same key (e.g. if // KEY_PRESSED TAB is a focus traversal keystroke, we also need to // consume KEY_RELEASED and KEY_TYPED TAB key events). // consuming KEY_RELEASED is easy, because their keyCodes matches // the KEY_PRESSED event. Consuming the intermediate KEY_TYPED is // very difficult because their is no clean way that we can know // which KEY_TYPED belongs to a focusTraversalKey and which not. // To address this problem we swallow every KEY_TYPE between the // KEY_PRESSED event that matches a focusTraversalKey and the // corresponding KEY_RELEASED. AWTKeyStroke oppositeKeystroke = AWTKeyStroke.getAWTKeyStroke (e.getKeyCode (), e.getModifiersEx (), !(e.id == KeyEvent.KEY_RELEASED)); // Here we check if we are currently waiting for a KEY_RELEASED and // swallow all KeyEvents that are to be delivered in between. This // should only be the KEY_TYPED events that correspond to the // focusTraversalKey's KEY_PRESSED event if (waitForKeyStroke != null) { if (eventKeystroke.equals(waitForKeyStroke)) // release this lock waitForKeyStroke = null; // as long as we are waiting for the KEY_RELEASED, we swallow every // KeyEvent, including the KEY_RELEASED e.consume(); return; } Set forwardKeystrokes = comp.getFocusTraversalKeys (KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); Set backwardKeystrokes = comp.getFocusTraversalKeys (KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS); Set upKeystrokes = comp.getFocusTraversalKeys (KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS); Set downKeystrokes = null; if (comp instanceof Container) downKeystrokes = comp.getFocusTraversalKeys (KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS); if (forwardKeystrokes.contains (eventKeystroke)) { waitForKeyStroke = oppositeKeystroke; focusNextComponent (comp); e.consume (); } else if (backwardKeystrokes.contains (eventKeystroke)) { waitForKeyStroke = oppositeKeystroke; focusPreviousComponent (comp); e.consume (); } else if (upKeystrokes.contains (eventKeystroke)) { waitForKeyStroke = oppositeKeystroke; upFocusCycle (comp); e.consume (); } else if (comp instanceof Container && downKeystrokes.contains (eventKeystroke)) { waitForKeyStroke = oppositeKeystroke; downFocusCycle ((Container) comp); e.consume (); } } protected void enqueueKeyEvents (long after, Component untilFocused) { delayRequests.add (new EventDelayRequest (after, untilFocused)); } protected void dequeueKeyEvents (long after, Component untilFocused) { // FIXME: need synchronization on delayRequests and enqueuedKeyEvents. // Remove the KeyEvent with the oldest timestamp, which should be // the first element in the SortedSet. if (after < 0) { int size = delayRequests.size (); if (size > 0) delayRequests.remove (delayRequests.first ()); } else { EventDelayRequest template = new EventDelayRequest (after, untilFocused); if (delayRequests.contains (template)) { EventDelayRequest actual = (EventDelayRequest) delayRequests.tailSet (template).first (); delayRequests.remove (actual); actual.dispatchEvents (); } } } protected void discardKeyEvents (Component comp) { // FIXME: need synchronization on delayRequests and enqueuedKeyEvents. Iterator i = delayRequests.iterator (); while (i.hasNext ()) { EventDelayRequest request = (EventDelayRequest) i.next (); if (request.focusedComp == comp || (comp instanceof Container && ((Container) comp).isAncestorOf (request.focusedComp))) request.discardEvents (); } } public void focusPreviousComponent (Component comp) { Component focusComp = (comp == null) ? getGlobalFocusOwner () : comp; Container focusCycleRoot = focusComp.getFocusCycleRootAncestor (); FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy (); Component previous = policy.getComponentBefore (focusCycleRoot, focusComp); if (previous != null) previous.requestFocusInWindow (); } public void focusNextComponent (Component comp) { Component focusComp = (comp == null) ? getGlobalFocusOwner () : comp; Container focusCycleRoot = focusComp.getFocusCycleRootAncestor (); FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy (); Component next = policy.getComponentAfter (focusCycleRoot, focusComp); if (next != null) next.requestFocusInWindow (); } public void upFocusCycle (Component comp) { Component focusComp = (comp == null) ? getGlobalFocusOwner () : comp; Container focusCycleRoot = focusComp.getFocusCycleRootAncestor (); if (focusCycleRoot instanceof Window) { FocusTraversalPolicy policy = focusCycleRoot.getFocusTraversalPolicy (); Component defaultComponent = policy.getDefaultComponent (focusCycleRoot); if (defaultComponent != null) defaultComponent.requestFocusInWindow (); } else { Container parentFocusCycleRoot = focusCycleRoot.getFocusCycleRootAncestor (); focusCycleRoot.requestFocusInWindow (); setGlobalCurrentFocusCycleRoot (parentFocusCycleRoot); } } public void downFocusCycle (Container cont) { if (cont == null) return; if (cont.isFocusCycleRoot (cont)) { FocusTraversalPolicy policy = cont.getFocusTraversalPolicy (); Component defaultComponent = policy.getDefaultComponent (cont); if (defaultComponent != null) defaultComponent.requestFocusInWindow (); setGlobalCurrentFocusCycleRoot (cont); } }} // class DefaultKeyboardFocusManager
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -