Java Code Examples for java.awt.event.KeyEvent#isMetaDown()

The following examples show how to use java.awt.event.KeyEvent#isMetaDown() . 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: ClipboardKeyAdapter.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
@Override
public void keyReleased(KeyEvent event) {
    if (event.isControlDown() || event.isMetaDown()) {
        switch (event.getKeyCode()) {
            case KeyEvent.VK_C:
                // Copy
                cancelEditing();
                XTableUtils.copyToClipboard(table, false);
                break;
            case KeyEvent.VK_X:
                // Cut
                cancelEditing();
                XTableUtils.copyToClipboard(table, true);
                break;
            case KeyEvent.VK_V:
                // Paste
                cancelEditing();
                XTableUtils.pasteFromClipboard(table);
                break;
            default:
                break;
        }
    }

}
 
Example 2
Source File: StaffApplication.java    From RemoteSupportTool with Apache License 2.0 6 votes vote down vote up
@Override
protected synchronized void doHandleKeyTyped(KeyEvent e) {
    if (handler == null) return;
    //LOGGER.debug("key typed: " + e.paramString());
    final char keyChar = e.getKeyChar();

    // Don't type non printable characters.
    if (keyChar == KeyEvent.CHAR_UNDEFINED || Character.isWhitespace(keyChar) || Character.isISOControl(keyChar) || Character.isIdentifierIgnorable(keyChar)) {
        //LOGGER.debug("non printable {} / {}", Character.isWhitespace(keyChar), Character.isISOControl(keyChar));
        return;
    }

    // Don't type a character, if a modifier key is pressed at the same time.
    if (e.isControlDown() || e.isMetaDown() || windowsKeyDown || (!SystemUtils.IS_OS_MAC && e.isAltDown())) {
        //LOGGER.debug("modifier {} / {} / {} / {}", e.isControlDown(), e.isAltDown(), e.isMetaDown(), windowsKeyDown);
        return;
    }

    //LOGGER.debug("type character \"{}\" ({})", keyChar, e.getKeyCode());
    handler.sendKeyType(keyChar);
    e.consume();
}
 
Example 3
Source File: ActionMacroManager.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void postProcessKeyEvent(KeyEvent e) {
  if (e.getID() != KeyEvent.KEY_PRESSED) return;
  if (myLastActionInputEvent.contains(e)) {
    myLastActionInputEvent.remove(e);
    return;
  }
  final boolean modifierKeyIsPressed = e.getKeyCode() == KeyEvent.VK_CONTROL || e.getKeyCode() == KeyEvent.VK_ALT || e.getKeyCode() == KeyEvent.VK_META || e.getKeyCode() == KeyEvent.VK_SHIFT;
  if (modifierKeyIsPressed) return;

  final boolean ready = IdeEventQueue.getInstance().getKeyEventDispatcher().isReady();
  final boolean isChar = e.getKeyChar() != KeyEvent.CHAR_UNDEFINED && UIUtil.isReallyTypedEvent(e);
  final boolean hasActionModifiers = e.isAltDown() | e.isControlDown() | e.isMetaDown();
  final boolean plainType = isChar && !hasActionModifiers;
  final boolean isEnter = e.getKeyCode() == KeyEvent.VK_ENTER;

  if (plainType && ready && !isEnter) {
    myRecordingMacro.appendKeytyped(e.getKeyChar(), e.getKeyCode(), e.getModifiers());
    notifyUser(Character.valueOf(e.getKeyChar()).toString(), true);
  }
  else if ((!plainType && ready) || isEnter) {
    final String stroke = KeyStroke.getKeyStrokeForEvent(e).toString();

    final int pressed = stroke.indexOf("pressed");
    String key = stroke.substring(pressed + "pressed".length());
    String modifiers = stroke.substring(0, pressed);

    String shortcut = (modifiers.replaceAll("ctrl", "control").trim() + " " + key.trim()).trim();

    myRecordingMacro.appendShortcut(shortcut);
    notifyUser(KeymapUtil.getKeystrokeText(KeyStroke.getKeyStrokeForEvent(e)), false);
  }
}
 
Example 4
Source File: GPTreeTableBase.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
  if (isStartEditingEvent(e, false)) {
    // It is an action which is supposed to start cell editing, but it is not
    // typing any character. Such actions are F2, Insert and Ctrl+T in the Gantt chart.
    // We want to select all existing text but do not clear it.
    putClientProperty("GPTreeTableBase.selectAll", true);
    putClientProperty("GPTreeTableBase.clearText", false);
  } else {
    putClientProperty("GPTreeTableBase.selectAll", false);
    putClientProperty("GPTreeTableBase.clearText", true);
    // Otherwise let's check if it is a character and ctrl/meta is not pressed.
    if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED && !e.isMetaDown() && !e.isControlDown()) {
      // In this case we want to clear existing text.
      putClientProperty("GPTreeTableBase.clearText", true);
    } else {
      putClientProperty("GPTreeTableBase.clearText", false);
    }
  }
  if (e.getKeyChar() == '>' && ((ks.getModifiers() | KeyEvent.CTRL_DOWN_MASK) > 0)) {
    return super.processKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_GREATER, ks.getModifiers()), e, condition, pressed);
  }
  if (e.getKeyChar() == '<' && ((ks.getModifiers() | KeyEvent.CTRL_DOWN_MASK) > 0)) {
    return super.processKeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_LESS, ks.getModifiers()), e, condition, pressed);
  }
  // See also overridden method editCellAt.
  return super.processKeyBinding(ks, e, condition, pressed);
}
 
Example 5
Source File: WSRecorder.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
@Override
public void recordRawKeyEvent(RComponent r, KeyEvent e) {
    JSONObject event = new JSONObject();
    event.put("type", "key_raw");
    int keyCode = e.getKeyCode();
    if (keyCode == KeyEvent.VK_META || keyCode == KeyEvent.VK_SHIFT || keyCode == KeyEvent.VK_ALT
            || keyCode == KeyEvent.VK_CONTROL) {
        return;
    }
    if ((e.isActionKey() || e.isControlDown() || e.isMetaDown() || e.isAltDown()) && e.getID() == KeyEvent.KEY_PRESSED) {
        String mtext = buildModifiersText(e);
        event.put("modifiersEx", mtext);
        KeysMap keysMap = KeysMap.findMap(e.getKeyCode());
        if (keysMap == KeysMap.NULL) {
            return;
        }
        String keyText;
        if (keysMap == null) {
            keyText = KeyEvent.getKeyText(e.getKeyCode());
        } else {
            keyText = keysMap.toString();
        }
        event.put("keyCode", keyText);
    } else if (e.getID() == KeyEvent.KEY_TYPED && !e.isControlDown()) {
        if (Character.isISOControl(e.getKeyChar()) && hasMapping(e.getKeyChar())) {
            event.put("keyChar", getMapping(e.getKeyChar()));
        } else {
            event.put("keyChar", "" + e.getKeyChar());
        }
    } else {
        return;
    }
    recordEvent(r, event);
}
 
Example 6
Source File: WatchAnnotationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void keyPressed(KeyEvent e) {
    if (e.isAltDown() || e.isAltGraphDown() ||
        e.isControlDown() || e.isMetaDown() ||
        e.isShiftDown()) {

        setSelectCursor();
    }
}
 
Example 7
Source File: WatchAnnotationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void keyReleased(KeyEvent e) {
    if (!(e.isAltDown() || e.isAltGraphDown() ||
          e.isControlDown() || e.isMetaDown() ||
          e.isShiftDown())) {

        unsetSelectCursor();
    }
}
 
Example 8
Source File: GPTreeTableBase.java    From ganttproject with GNU General Public License v3.0 5 votes vote down vote up
private boolean isStartEditingEvent(KeyEvent e, boolean includeCharTyping) {
  boolean result = e.getKeyCode() == KeyEvent.VK_F2
      || e.getKeyCode() == KeyEvent.VK_INSERT
      || (myNewRowAction != null && KeyStroke.getKeyStrokeForEvent(e).equals(myNewRowAction.getKeyStroke()));
  if (!result && includeCharTyping) {
    result = e.getKeyChar() != KeyEvent.CHAR_UNDEFINED && !e.isMetaDown() && !e.isControlDown();
  }
  return result;
}
 
Example 9
Source File: View2D.java    From energy2d with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void processKeyReleased(KeyEvent e) {
	if (runHeatingThread) {
		if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
			cooling = false;
		}
	}
	switch (e.getKeyCode()) {
	case KeyEvent.VK_DELETE:
	case KeyEvent.VK_BACK_SPACE:
		// this is different than cut() in that it doesn't create a copy for pasting
		if (selectedManipulable != null) {
			if (JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the selected object?", "Delete Object", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
				undoManager.addEdit(new UndoRemoveManipulable(this));
				notifyManipulationListeners(selectedManipulable, ManipulationEvent.DELETE);
				setSelectedManipulable(null);
			}
		} else {
			if (showGraph) {
				if (JOptionPane.showConfirmDialog(this, "Are you sure you want to erase the graph?", "Erase Graph", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION) {
					eraseGraph();
				}
			}
		}
		break;
	case KeyEvent.VK_R:
		notifyManipulationListeners(null, runToggle ? ManipulationEvent.STOP : ManipulationEvent.RUN);
		break;
	case KeyEvent.VK_T:
		notifyManipulationListeners(null, ManipulationEvent.RESET);
		break;
	case KeyEvent.VK_L:
		notifyManipulationListeners(null, ManipulationEvent.RELOAD);
		break;
	case KeyEvent.VK_S: // avoid conflict with the save keystroke
		if (!e.isControlDown() && !e.isMetaDown() && !e.isAltDown())
			notifyManipulationListeners(null, ManipulationEvent.SUN_SHINE);
		break;
	case KeyEvent.VK_Q:
		notifyManipulationListeners(null, ManipulationEvent.SUN_ANGLE_INCREASE);
		break;
	case KeyEvent.VK_W:
		notifyManipulationListeners(null, ManipulationEvent.SUN_ANGLE_DECREASE);
		break;
	case KeyEvent.VK_G:
		showGraph = !showGraph;
		notifyGraphListeners(showGraph ? GraphEvent.GRAPH_OPENED : GraphEvent.GRAPH_CLOSED);
		break;
	case KeyEvent.VK_ESCAPE: // allow the app to shut down when in full screen mode
		Object r = getClientProperty("close_full_screen");
		if (r instanceof Runnable)
			((Runnable) r).run();
		break;
	}
	repaint();
	tipText = null;
	// e.consume();//don't call, or this stops key binding
}
 
Example 10
Source File: DefaultKeyboardHandler.java    From tn5250j with GNU General Public License v2.0 4 votes vote down vote up
protected void displayInfo(KeyEvent e, String s) {
  String charString, keyCodeString, modString, tmpString, isString;

  char c = e.getKeyChar();
  int keyCode = e.getKeyCode();
  int modifiers = e.getModifiers();

  if (Character.isISOControl(c)) {
    charString = "key character = "
        + "(an unprintable control character)";
  } else {
    charString = "key character = '"
        + c + "'";
  }

  keyCodeString = "key code = " + keyCode
      + " ("
      + KeyEvent.getKeyText(keyCode)
      + ")";
  if (keyCode == KeyEvent.VK_PREVIOUS_CANDIDATE) {

    keyCodeString += " previous candidate ";

  }

  if (keyCode == KeyEvent.VK_DEAD_ABOVEDOT ||
      keyCode == KeyEvent.VK_DEAD_ABOVERING ||
      keyCode == KeyEvent.VK_DEAD_ACUTE ||
      keyCode == KeyEvent.VK_DEAD_BREVE ||
      keyCode == KeyEvent.VK_DEAD_CIRCUMFLEX

      ) {

    keyCodeString += " dead key ";

  }

  modString = "modifiers = " + modifiers;
  tmpString = KeyEvent.getKeyModifiersText(modifiers);
  if (tmpString.length() > 0) {
    modString += " (" + tmpString + ")";
  } else {
    modString += " (no modifiers)";
  }

  isString = "isKeys = isActionKey (" + e.isActionKey() + ")" +
      " isAltDown (" + e.isAltDown() + ")" +
      " isAltGraphDown (" + e.isAltGraphDown() + ")" +
      " isAltGraphDownLinux (" + isAltGr + ")" +
      " isControlDown (" + e.isControlDown() + ")" +
      " isMetaDown (" + e.isMetaDown() + ")" +
      " isShiftDown (" + e.isShiftDown() + ")";


  String newline = "\n";
  System.out.println(s + newline
      + "    " + charString + newline
      + "    " + keyCodeString + newline
      + "    " + modString + newline
      + "    " + isString + newline);

}
 
Example 11
Source File: StaffApplication.java    From RemoteSupportTool with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("Duplicates")
protected synchronized void doHandleKeyRelease(KeyEvent e) {
    if (handler == null) return;
    //LOGGER.debug("key released: " + e.paramString());

    // Get code of the released key.
    // Keypad arrows are translated to regular arrow keys.
    final int keyCode;
    switch (e.getKeyCode()) {
        case KeyEvent.VK_KP_DOWN:
            keyCode = KeyEvent.VK_DOWN;
            break;
        case KeyEvent.VK_KP_LEFT:
            keyCode = KeyEvent.VK_LEFT;
            break;
        case KeyEvent.VK_KP_RIGHT:
            keyCode = KeyEvent.VK_RIGHT;
            break;
        case KeyEvent.VK_KP_UP:
            keyCode = KeyEvent.VK_UP;
            break;
        default:
            keyCode = e.getKeyCode();
            break;
    }

    // Never press undefined key codes.
    if (keyCode == KeyEvent.VK_UNDEFINED) {
        return;
    }

    // Never send caps lock, num lock and scroll lock key.
    if (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK || keyCode == KeyEvent.VK_SCROLL_LOCK) {
        return;
    }

    // Detect, if a control key was pressed.
    final boolean isControlKey = e.isActionKey() ||
            keyCode == KeyEvent.VK_BACK_SPACE ||
            keyCode == KeyEvent.VK_DELETE ||
            keyCode == KeyEvent.VK_ENTER ||
            keyCode == KeyEvent.VK_SPACE ||
            keyCode == KeyEvent.VK_TAB ||
            keyCode == KeyEvent.VK_ESCAPE ||
            keyCode == KeyEvent.VK_ALT ||
            keyCode == KeyEvent.VK_ALT_GRAPH ||
            keyCode == KeyEvent.VK_CONTROL ||
            keyCode == KeyEvent.VK_SHIFT ||
            keyCode == KeyEvent.VK_META;

    // Release control keys.
    if (isControlKey) {
        //LOGGER.debug("release key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
        handler.sendKeyRelease(keyCode);
        e.consume();
    }

    // Release other keys, if they are pressed together with a modifier key.
    else if (e.isControlDown() || e.isMetaDown() || windowsKeyDown || (!SystemUtils.IS_OS_MAC && e.isAltDown()) || pressedKeys.contains(keyCode)) {
        //LOGGER.debug("release key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
        handler.sendKeyRelease(keyCode);
        pressedKeys.remove((Integer) keyCode);
        e.consume();
    }

    // Forget, that the Windows key is pressed.
    if (keyCode == KeyEvent.VK_WINDOWS) {
        synchronized (Frame.this) {
            windowsKeyDown = false;
        }
    }
}
 
Example 12
Source File: StaffApplication.java    From RemoteSupportTool with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("Duplicates")
protected synchronized void doHandleKeyPress(KeyEvent e) {
    if (handler == null) return;
    //LOGGER.debug("key pressed: " + e.paramString());

    // Get code of the pressed key.
    // Keypad arrows are translated to regular arrow keys.
    final int keyCode;
    switch (e.getKeyCode()) {
        case KeyEvent.VK_KP_DOWN:
            keyCode = KeyEvent.VK_DOWN;
            break;
        case KeyEvent.VK_KP_LEFT:
            keyCode = KeyEvent.VK_LEFT;
            break;
        case KeyEvent.VK_KP_RIGHT:
            keyCode = KeyEvent.VK_RIGHT;
            break;
        case KeyEvent.VK_KP_UP:
            keyCode = KeyEvent.VK_UP;
            break;
        default:
            keyCode = e.getKeyCode();
            break;
    }

    // Never press undefined key codes.
    if (keyCode == KeyEvent.VK_UNDEFINED) {
        return;
    }

    // Never send caps lock, num lock and scroll lock key.
    if (keyCode == KeyEvent.VK_CAPS_LOCK || keyCode == KeyEvent.VK_NUM_LOCK || keyCode == KeyEvent.VK_SCROLL_LOCK) {
        return;
    }

    // Detect, if a control key was pressed.
    final boolean isControlKey = e.isActionKey() ||
            keyCode == KeyEvent.VK_BACK_SPACE ||
            keyCode == KeyEvent.VK_DELETE ||
            keyCode == KeyEvent.VK_ENTER ||
            keyCode == KeyEvent.VK_SPACE ||
            keyCode == KeyEvent.VK_TAB ||
            keyCode == KeyEvent.VK_ESCAPE ||
            keyCode == KeyEvent.VK_ALT ||
            keyCode == KeyEvent.VK_ALT_GRAPH ||
            keyCode == KeyEvent.VK_CONTROL ||
            keyCode == KeyEvent.VK_SHIFT ||
            keyCode == KeyEvent.VK_META;

    // Press control keys.
    if (isControlKey) {
        //LOGGER.debug("press key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
        handler.sendKeyPress(keyCode);
        e.consume();
    }

    // Press other keys, if they are pressed together with a modifier key.
    else if (e.isControlDown() || e.isMetaDown() || windowsKeyDown || (!SystemUtils.IS_OS_MAC && e.isAltDown())) {
        //LOGGER.debug("press key \"{}\" ({})", keyCode, KeyEvent.getKeyText(keyCode));
        handler.sendKeyPress(keyCode);
        if (!pressedKeys.contains(keyCode))
            pressedKeys.add(keyCode);
        e.consume();
    }

    // Remember, that the Windows key was pressed.
    if (keyCode == KeyEvent.VK_WINDOWS) {
        synchronized (Frame.this) {
            windowsKeyDown = true;
        }
    }
}
 
Example 13
Source File: LoginScreenPlugin.java    From plugins with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void keyPressed(KeyEvent e)
{
	if (!config.pasteEnabled() || (
		client.getGameState() != GameState.LOGIN_SCREEN &&
			client.getGameState() != GameState.LOGIN_SCREEN_AUTHENTICATOR))
	{
		return;
	}

	// enable pasting on macOS with the Command (meta) key
	boolean isModifierDown = OSType.getOSType() == OSType.MacOS ? e.isMetaDown() : e.isControlDown();

	if (e.getKeyCode() == KeyEvent.VK_V && isModifierDown)
	{
		try
		{
			final String data = Toolkit
				.getDefaultToolkit()
				.getSystemClipboard()
				.getData(DataFlavor.stringFlavor)
				.toString()
				.trim();

			switch (client.getLoginIndex())
			{
				// Username/password form
				case 2:
					if (client.getCurrentLoginField() == 0)
					{
						// Truncate data to maximum username length if necessary
						client.setUsername(data.substring(0, Math.min(data.length(), MAX_USERNAME_LENGTH)));
					}

					break;
				// Authenticator form
				case 4:
					// Truncate data to maximum OTP code length if necessary
					client.setOtp(data.substring(0, Math.min(data.length(), MAX_PIN_LENGTH)));
					break;
			}
		}
		catch (UnsupportedFlavorException | IOException ex)
		{
			log.warn("failed to fetch clipboard data", ex);
		}
	}
}
 
Example 14
Source File: LoginScreenPlugin.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void keyPressed(KeyEvent e)
{
	if (!config.pasteEnabled() || (
		client.getGameState() != GameState.LOGIN_SCREEN &&
		client.getGameState() != GameState.LOGIN_SCREEN_AUTHENTICATOR))
	{
		return;
	}

	// enable pasting on macOS with the Command (meta) key
	boolean isModifierDown = OSType.getOSType() == OSType.MacOS ? e.isMetaDown() : e.isControlDown();

	if (e.getKeyCode() == KeyEvent.VK_V && isModifierDown)
	{
		try
		{
			final String data = Toolkit
				.getDefaultToolkit()
				.getSystemClipboard()
				.getData(DataFlavor.stringFlavor)
				.toString()
				.trim();

			switch (client.getLoginIndex())
			{
				// Username/password form
				case 2:
					if (client.getCurrentLoginField() == 0)
					{
						// Truncate data to maximum username length if necessary
						client.setUsername(data.substring(0, Math.min(data.length(), MAX_USERNAME_LENGTH)));
					}

					break;
				// Authenticator form
				case 4:
					// Truncate data to maximum OTP code length if necessary
					client.setOtp(data.substring(0, Math.min(data.length(), MAX_PIN_LENGTH)));
					break;
			}
		}
		catch (UnsupportedFlavorException | IOException ex)
		{
			log.warn("failed to fetch clipboard data", ex);
		}
	}
}
 
Example 15
Source File: StyledEditor.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 4 votes vote down vote up
private static boolean isSave(KeyEvent ke) {
    return (ke.isMetaDown() || ke.isControlDown()) && ke.getKeyCode() == KeyEvent.VK_S;
}
 
Example 16
Source File: Input.java    From lizzie with GNU General Public License v3.0 4 votes vote down vote up
private boolean controlIsPressed(KeyEvent e) {
  boolean mac = System.getProperty("os.name", "").toUpperCase().startsWith("MAC");
  return e.isControlDown() || (mac && e.isMetaDown());
}
 
Example 17
Source File: SwingTools.java    From rapidminer-studio with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 * Returns whether or not the control modifier is down during the event. This takes the OS into
 * account, so on Mac it will check if the meta modifier is down during the event.
 *
 * @param e
 *            the event
 * @return {@code true} if the control modifier is down on Windows/Linux or the meta modifier is
 *         down on Mac; {@code false} otherwise
 */
public static boolean isControlOrMetaDown(KeyEvent e) {
	if (!IS_MAC) {
		return e.isControlDown();
	} else {
		return e.isMetaDown();
	}
}
 
Example 18
Source File: KeyBindingOverrideKeyEventDispatcher.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * A test to see if the given event is modified in such a way as a text component would not
 * handle the event
 * @param e the event
 * @return true if modified
 */
private boolean isModified(KeyEvent e) {
	return e.isAltDown() || e.isAltGraphDown() || e.isMetaDown() || e.isControlDown();
}