Java Code Examples for java.awt.event.KeyEvent#CTRL_MASK

The following examples show how to use java.awt.event.KeyEvent#CTRL_MASK . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TreeTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void processKeyEvent(KeyEvent e) {
    //Manually hook in the bindings for tab - does not seem to get called
    //automatically
    if (isEditing() && ((e.getKeyCode() == KeyEvent.VK_DOWN) || (e.getKeyCode() == KeyEvent.VK_UP))) {
        return; //XXX
    }

    //Bypass standard tab and escape handling, and use our registered
    //actions instead
    if (
        !isEditing() ||
            (((e.getKeyCode() != KeyEvent.VK_TAB) && (e.getKeyCode() != KeyEvent.VK_ESCAPE)) ||
            ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0))
    ) {
        super.processKeyEvent(e);
    } else {
        processKeyBinding(
            KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiersEx(), e.getID() == KeyEvent.KEY_RELEASED), e,
            JComponent.WHEN_FOCUSED, e.getID() == KeyEvent.KEY_PRESSED
        );
    }
}
 
Example 2
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static boolean usableKeyOnMac(int key, int mask) {
    //All permutations fail for Q except ctrl
    if (key == KeyEvent.VK_Q) {
        return false;
    }

    boolean isMeta = ((mask & KeyEvent.META_MASK) != 0) || ((mask & KeyEvent.CTRL_DOWN_MASK) != 0);

    boolean isAlt = ((mask & KeyEvent.ALT_MASK) != 0) || ((mask & KeyEvent.ALT_DOWN_MASK) != 0);

    boolean isOnlyMeta = isMeta && ((mask & ~(KeyEvent.META_DOWN_MASK | KeyEvent.META_MASK)) == 0);

    //Mac OS consumes keys Command+ these keys - the app will never see
    //them, so CTRL should not be remapped for these
    if (isOnlyMeta) {
        return (key != KeyEvent.VK_H) && (key != KeyEvent.VK_SPACE) && (key != KeyEvent.VK_TAB);
    }
    if ((key == KeyEvent.VK_D) && isMeta && isAlt) {
        return false;
    }
    if (key == KeyEvent.VK_SPACE && isMeta && ((mask & KeyEvent.CTRL_MASK) != 0)) {
        // http://lists.apple.com/archives/java-dev/2010/Aug/msg00002.html
        return false;
    }
    return true;
}
 
Example 3
Source File: JavaTypeCompletionItem.java    From nb-springboot with Apache License 2.0 6 votes vote down vote up
@Override
public void processKeyEvent(KeyEvent evt) {
    if (evt.getID() == KeyEvent.KEY_TYPED && evt.getKeyChar() == '.' && elementKind == ElementKind.PACKAGE) {
        logger.log(Level.FINER, "Accepting package ''{0}'' and continuing completion", name);
        Completion.get().hideDocumentation();
        Completion.get().hideCompletion();
        JTextComponent tc = (JTextComponent) evt.getSource();
        BaseDocument doc = (BaseDocument) tc.getDocument();
        doc.runAtomic(new Runnable() {
            @Override
            public void run() {
                try {
                    doc.remove(dotOffset, caretOffset - dotOffset);
                    doc.insertString(dotOffset, name.concat("."), null);
                } catch (BadLocationException ble) {
                    //ignore
                }
            }
        });
        Completion.get().showCompletion();
        evt.consume();
    }
    // detect if Ctrl + Enter is pressed
    overwrite = evt.getKeyCode() == KeyEvent.VK_ENTER && (evt.getModifiers() & KeyEvent.CTRL_MASK) != 0;
}
 
Example 4
Source File: ContactList.java    From Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void contactItemClicked(ContactItem item) {
       activeItem = item;

       if (activeKeyEvent == null || ((activeKeyEvent.getModifiers() & KeyEvent.CTRL_MASK) == 0)) {
           clearSelectionList(item);
       }


       fireContactItemClicked(item);
       activeKeyEvent = null;
   }
 
Example 5
Source File: MasterCli.java    From brModelo with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void keyTyped(KeyEvent e) {
    key = e.getKeyChar();
    if (key == (char) 127 || key == '\b' || key == '\n') {
        e.consume();
        return;
    }

    if (e.getModifiers() == KeyEvent.CTRL_MASK) {
        return;
    }

    palavra = insertChar(palavra, key);
    posCursor++;
}
 
Example 6
Source File: KeyShortCutEditPanel.java    From netbeans-mmd-plugin with Apache License 2.0 5 votes vote down vote up
private void updateCurrentSelectedForKey (final KeyEvent evt) {
  final int index = this.tableKeyShortcuts.getSelectedRow();
  if (index>=0){
    final KeyShortcut oldShortcut = this.listOfKeys.get(index);
    final int keyCode = evt.getKeyCode();
    final int modifiers = evt.getModifiers() & (KeyEvent.META_MASK | KeyEvent.SHIFT_MASK | KeyEvent.CTRL_MASK | KeyEvent.ALT_MASK);
    final KeyShortcut newShortCut = new KeyShortcut(oldShortcut.getID(),keyCode,modifiers);
    this.listOfKeys.set(index, newShortCut);
    for(final TableModelListener l:this.listeners){
      l.tableChanged(new TableModelEvent(this,index));
    }
  }
  
  updateForSelected();
}
 
Example 7
Source File: TopComponentTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testToStringOfDelegateContainsNameOfOriginalAction() throws Exception {
    SaveAction sa = SaveAction.get(SaveAction.class);
    Action a = sa.createContextAwareInstance(Lookup.EMPTY);
    if (a.toString().indexOf("SaveAction") == -1) {
        fail("We need name of the original action:\n" + a.toString());
    }

    SaveAction.cnt = 0;
    CharSequence log = Log.enable("org.netbeans.ui", Level.FINER);
    
    final TopComponent tc = new TopComponent();
    final KeyEvent ke = new KeyEvent(tc, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.CTRL_MASK, 0, 'S');
    final KeyStroke ks = KeyStroke.getKeyStrokeForEvent(ke);
    MockServices.setServices(MyKM.class);
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            tc.setActivatedNodes( new Node[0] );
            tc.processKeyBinding(ks, ke, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, true);
        }
        
    });
    
    if (log.toString().indexOf("SaveAction") == -1 
            && SaveAction.cnt == 1 ) { //make sure the action was actually invoked
        fail(log.toString());
    }
}
 
Example 8
Source File: ResourceBundleSupport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the keystroke stored at the given resourcebundle key.
 * <p/>
 * The keystroke will be composed of a simple key press and the plattform's MenuKeyMask.
 * <p/>
 * The keystrokes character key should be either the symbolic name of one of the KeyEvent.VK_* constants or the
 * character for that key.
 * <p/>
 * For the 'A' key, the resource bundle would therefore either contain "VK_A" or "a".
 * <pre>
 * a.resourcebundle.key=VK_A
 * an.other.resourcebundle.key=a
 * </pre>
 *
 * @param key the resourcebundle key
 * @return the keystroke
 * @see java.awt.Toolkit#getMenuShortcutKeyMask()
 */
public KeyStroke getKeyStroke( final String key ) {
  String name = strictString( key );
  if ( StringUtils.isEmpty( name ) ) {
    return null;
  }

  boolean explicitNone = false;
  int mask = 0;
  final StringTokenizer strtok = new StringTokenizer( name );
  while ( strtok.hasMoreTokens() ) {
    final String token = strtok.nextToken();
    if ( "shift".equals( token ) ) {
      mask |= KeyEvent.SHIFT_MASK;
    } else if ( "alt".equals( token ) ) {
      mask |= KeyEvent.ALT_MASK;
    } else if ( "ctrl".equals( token ) ) {
      mask |= KeyEvent.CTRL_MASK;
    } else if ( "meta".equals( token ) ) {
      mask |= KeyEvent.META_MASK;
    } else if ( "menu".equals( token ) ) {
      mask |= getMenuKeyMask();
    } else if ( "none".equals( token ) ) {
      explicitNone = true;
    } else {
      name = token;
    }
  }
  if ( explicitNone == true ) {
    mask = 0;
  } else if ( mask == 0 ) {
    mask = getMenuKeyMask();
  }
  //noinspection MagicConstant
  return KeyStroke.getKeyStroke( createMnemonic( name ).intValue(), mask );
}
 
Example 9
Source File: EditableDisplayerTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void ctrlPressKey(final Component target, final int key) throws Exception {
    if (SwingUtilities.isEventDispatchThread()) {
        KeyEvent k = new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.CTRL_MASK, key, (char) key);
        target.dispatchEvent(k);
    } else {
        
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                KeyEvent ke = new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.CTRL_MASK, key, (char) key);
                target.dispatchEvent(ke);
            }
        });
        sleep();
    }
}
 
Example 10
Source File: Utilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean addModifiersPortable(StringBuilder buf, int modifiers) {
    boolean b = false;

    if ((modifiers & KeyEvent.SHIFT_MASK) != 0) {
        buf.append('S');
        b = true;
    }

    if (Utilities.isMac() && ((modifiers & KeyEvent.META_MASK) != 0) || !Utilities.isMac() && ((modifiers & KeyEvent.CTRL_MASK) != 0)) {
        buf.append('D');
        b = true;
    }

    if (Utilities.isMac() && ((modifiers & KeyEvent.CTRL_MASK) != 0) || !Utilities.isMac() && ((modifiers & KeyEvent.ALT_MASK) != 0)) {
        buf.append('O');
        b = true;
    }
    // mac alt fallback
    if (Utilities.isMac() && ((modifiers & KeyEvent.ALT_MASK) != 0)) {
        buf.append('A');
        b = true;
    }
    // META fallback, see issue #224362
    if (!Utilities.isMac() && ((modifiers & KeyEvent.META_MASK) != 0)) {
        buf.append('M');
        b = true;
    }

    return b;
}
 
Example 11
Source File: KeyboardSupport.java    From CrossMobile with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void reactToReleaseEvent(CEventCallback callback, int vk_event, int modifier) {
    switch (vk_event) {
        case KeyEvent.VK_CONTROL:
            if ((mask & MULTITOUCH.mask) != 0)
                callback.endMultiTouch();
            break;
        case KeyEvent.VK_B:
        case KeyEvent.VK_BACK_SPACE:
            if ((mask & BACK.mask) != 0)
                callback.back();
            break;
        case KeyEvent.VK_P:
        case KeyEvent.VK_SPACE:
            if ((mask & PAUSE.mask) != 0)
                ((DesktopLifecycleBridge) Native.lifecycle()).toggleActivation();
            break;
        case KeyEvent.VK_ESCAPE:
        case KeyEvent.VK_Q:
            if ((mask & QUIT.mask) != 0)
                callback.powerOff();
            break;
        case KeyEvent.VK_M:
            if (modifier == KeyEvent.CTRL_MASK)
                memoryWarning();
            break;
        case KeyEvent.VK_H:
        case KeyEvent.VK_HOME:
            if ((mask & HOME.mask) != 0)
                callback.home();
            break;
        case KeyEvent.VK_LEFT:
            if ((mask & ROTATE.mask) != 0)
                callback.rotateLeft();
            break;
        case KeyEvent.VK_RIGHT:
            if ((mask & ROTATE.mask) != 0)
                callback.rotateRight();
            break;
    }
}
 
Example 12
Source File: GUIMain.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
private void addShortcutForWindows() {
	if (PacketProxyUtility.getInstance().isMac()) {
		return;
	}
	JPanel p = (JPanel) getContentPane();
	InputMap im = p.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
	ActionMap am = p.getActionMap();
	int hotkey = (KeyEvent.CTRL_MASK);
	registerTabShortcut(KeyEvent.VK_H, hotkey, im, am, Panes.HISTORY.ordinal());
	registerTabShortcut(KeyEvent.VK_I, hotkey, im, am, Panes.INTERCEPT.ordinal());
	registerTabShortcut(KeyEvent.VK_R, hotkey, im, am, Panes.REPEATER.ordinal());
	registerTabShortcut(KeyEvent.VK_B, hotkey, im, am, Panes.BULKSENDER.ordinal());
	registerTabShortcut(KeyEvent.VK_O, hotkey, im, am, Panes.OPTIONS.ordinal());
	registerTabShortcut(KeyEvent.VK_L, hotkey, im, am, Panes.LOG.ordinal());
}
 
Example 13
Source File: ResourceBundleSupport.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns the keystroke stored at the given resourcebundle key.
 * <p/>
 * The keystroke will be composed of a simple key press and a keystroke mask pattern. The pattern should be specified
 * via the words "shift", "alt", "ctrl", "meta" or "menu". Menu should be used to reference the platform specific menu
 * shortcut. For the sake of safety, menu should only be combined with "shift" and/or "alt" for menu keystrokes.
 * <p/>
 * The keystrokes character key should be either the symbolic name of one of the KeyEvent.VK_* constants or the
 * character for that key.
 * <p/>
 * For the 'A' key, the resource bundle would therefore either contain "VK_A" or "a".
 * <pre>
 * a.resourcebundle.key=VK_A
 * an.other.resourcebundle.key=a
 * </pre>
 *
 * @param key the resourcebundle key
 * @return the keystroke
 * @see java.awt.Toolkit#getMenuShortcutKeyMask()
 */
public KeyStroke getOptionalKeyStroke( final String key ) {
  try {
    String name = getOptionalString( key );
    if ( StringUtils.isEmpty( name ) ) {
      return null;
    }

    boolean noneSelected = false;
    int mask = 0;
    final StringTokenizer strtok = new StringTokenizer( name );
    while ( strtok.hasMoreTokens() ) {
      final String token = strtok.nextToken();
      if ( "shift".equals( token ) ) {
        mask |= KeyEvent.SHIFT_MASK;
      } else if ( "alt".equals( token ) ) {
        mask |= KeyEvent.ALT_MASK;
      } else if ( "ctrl".equals( token ) ) {
        mask |= KeyEvent.CTRL_MASK;
      } else if ( "meta".equals( token ) ) {
        mask |= KeyEvent.META_MASK;
      } else if ( "menu".equals( token ) ) {
        mask |= getMenuKeyMask();
      } else if ( "none".equals( token ) ) {
        noneSelected = true;
      } else {
        name = token;
      }
    }
    if ( noneSelected ) {
      mask = 0;
    } else if ( mask == 0 ) {
      mask = getMenuKeyMask();
    }
    //noinspection MagicConstant
    return KeyStroke.getKeyStroke( createMnemonic( name ).intValue(), mask );
  } catch ( MissingResourceException mre ) {
    return null;
  }
}
 
Example 14
Source File: MMDCfgPanel.java    From netbeans-mmd-plugin with Apache License 2.0 4 votes vote down vote up
private int getScalingModifiers() {
  return (this.checkBoxScalingALT.isSelected() ? KeyEvent.ALT_MASK : 0)
          | (this.checkBoxScalingCTRL.isSelected() ? KeyEvent.CTRL_MASK : 0)
          | (this.checkBoxScalingMETA.isSelected() ? KeyEvent.ALT_MASK : 0)
          | (this.checkBoxScalingSHIFT.isSelected() ? KeyEvent.SHIFT_MASK : 0);
}
 
Example 15
Source File: CreateTestsAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** creates new CreateTestsAction instance */
public CreateTestsAction() {
    super(MENU, MENU, new Action.Shortcut(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK, KeyEvent.VK_J));
}
 
Example 16
Source File: JDA.java    From java-disassembler with GNU General Public License v3.0 4 votes vote down vote up
private static boolean isCtrlDown(KeyEvent e) {
    return ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0);
}
 
Example 17
Source File: EditableDisplayerTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doCtrlPressKey(final Component target, final int key) throws Exception {
    
    KeyEvent k = new KeyEvent(target, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), KeyEvent.CTRL_MASK, key, (char) key);
    target.dispatchEvent(k);
}
 
Example 18
Source File: ExecuteTestAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** creates new ExecuteTestAction instance */
public ExecuteTestAction() {
    super(MENU, MENU, new Action.Shortcut(KeyEvent.CTRL_MASK|KeyEvent.ALT_MASK, KeyEvent.VK_L));
}
 
Example 19
Source File: MenuBar.java    From netbeans with Apache License 2.0 4 votes vote down vote up
protected @Override boolean processKeyBinding(KeyStroke ks,
                                KeyEvent e,
                                int condition,
                                boolean pressed) {
    if (Utilities.isMac()) {
        int mods = e.getModifiers();
        boolean isCtrl = (mods & KeyEvent.CTRL_MASK) != 0;
        boolean isAlt = (mods & KeyEvent.ALT_MASK) != 0;
        if (isAlt && (e instanceof MarkedKeyEvent)) {
            mods = mods & ~ KeyEvent.CTRL_MASK;
            mods = mods & ~ KeyEvent.CTRL_DOWN_MASK;
            mods |= KeyEvent.ALT_MASK;
            mods |= KeyEvent.ALT_DOWN_MASK;
            
            KeyEvent newEvent = new MarkedKeyEvent (
                (Component) e.getSource(), e.getID(), 
                e.getWhen(), mods, e.getKeyCode(), e.getKeyChar(), 
                e.getKeyLocation());
            
            KeyStroke newStroke = null;
            if( null != ks ) {
                newStroke = e.getID() == KeyEvent.KEY_TYPED ?
                    KeyStroke.getKeyStroke (ks.getKeyChar(), mods) :
                    KeyStroke.getKeyStroke (ks.getKeyCode(), mods,
                    !ks.isOnKeyRelease());
            }
            
            boolean result = super.processKeyBinding (newStroke, 
                newEvent, condition, pressed);
            
            if (newEvent.isConsumed()) {
                e.consume();
            }
            return result;
        } else if (!isAlt) {
            return super.processKeyBinding (ks, e, condition, pressed);
        } else {
            return false;
        }
    } else {
        return super.processKeyBinding (ks, e, condition, pressed);
    }                     
}
 
Example 20
Source File: ValueCompletionItem.java    From nb-springboot with Apache License 2.0 4 votes vote down vote up
@Override
public void processKeyEvent(KeyEvent evt) {
    // detect if Ctrl + Enter is pressed
    overwrite = evt.getKeyCode() == KeyEvent.VK_ENTER && (evt.getModifiers() & KeyEvent.CTRL_MASK) != 0;
}