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

The following examples show how to use java.awt.event.KeyEvent#getExtendedKeyCodeForChar() . 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: EqualKeyCode.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String []s) {
    for (int i = 0; i < LETTERS.length(); i++){
        char cSmall = LETTERS.charAt(i);
        char cLarge = Character.toUpperCase(cSmall);

        int iSmall = KeyEvent.getExtendedKeyCodeForChar(cSmall);
        int iLarge = KeyEvent.getExtendedKeyCodeForChar(cLarge);

        System.out.print(" " + cSmall + ":" + iSmall + " ---- ");
        System.out.println(" " + cLarge + " : " + iLarge);
        if (KeyEvent.getExtendedKeyCodeForChar(cSmall) !=
            KeyEvent.getExtendedKeyCodeForChar(cLarge))
        {
            throw new RuntimeException("ExtendedKeyCode doesn't exist or doesn't match between capital and small letters.");
        }
    }
}
 
Example 2
Source File: GuiTester.java    From Digital with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Types the given key
 *
 * @param stroke the key to type
 */
public void typeNow(KeyStroke stroke) {
    int mod = stroke.getModifiers();
    if ((mod & SHIFT_DOWN_MASK) != 0) keyPress(KeyEvent.VK_SHIFT);
    if ((mod & CTRL_DOWN_MASK) != 0) keyPress(KeyEvent.VK_CONTROL);
    if ((mod & ALT_DOWN_MASK) != 0) keyPress(KeyEvent.VK_ALT);
    try {
        int keyCode = stroke.getKeyCode();
        if (keyCode == 0) keyCode = KeyEvent.getExtendedKeyCodeForChar(stroke.getKeyChar());
        keyType(keyCode);
    } finally {
        if ((mod & SHIFT_DOWN_MASK) != 0) keyRelease(KeyEvent.VK_SHIFT);
        if ((mod & CTRL_DOWN_MASK) != 0) keyRelease(KeyEvent.VK_CONTROL);
        if ((mod & ALT_DOWN_MASK) != 0) keyRelease(KeyEvent.VK_ALT);
    }
}
 
Example 3
Source File: ActionUtil.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
public static ShortcutSet getMnemonicAsShortcut(@Nonnull AnAction action) {
  int mnemonic = KeyEvent.getExtendedKeyCodeForChar(action.getTemplatePresentation().getMnemonic());
  if (mnemonic != KeyEvent.VK_UNDEFINED) {
    KeyboardShortcut ctrlAltShortcut = new KeyboardShortcut(KeyStroke.getKeyStroke(mnemonic, InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK), null);
    KeyboardShortcut altShortcut = new KeyboardShortcut(KeyStroke.getKeyStroke(mnemonic, InputEvent.ALT_DOWN_MASK), null);
    CustomShortcutSet shortcutSet;
    if (SystemInfo.isMac) {
      if (Registry.is("ide.mac.alt.mnemonic.without.ctrl")) {
        shortcutSet = new CustomShortcutSet(ctrlAltShortcut, altShortcut);
      }
      else {
        shortcutSet = new CustomShortcutSet(ctrlAltShortcut);
      }
    }
    else {
      shortcutSet = new CustomShortcutSet(altShortcut);
    }
    return shortcutSet;
  }
  return null;
}
 
Example 4
Source File: RobotInput.java    From knife with MIT License 5 votes vote down vote up
@Deprecated
private void inputStringOld1(String str){
	delay(100);
	for (char c : str.toCharArray()) {
		int keyCode = KeyEvent.getExtendedKeyCodeForChar(c);
		input(keyCode);
	}
	delay(100);
}
 
Example 5
Source File: KeyPressAndHoldTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void typeSampleBody() {
    robot.delay(PAUSE);
    for (int utfCode : SAMPLE.substring(1).toCharArray()) {
        int keyCode = KeyEvent.getExtendedKeyCodeForChar(utfCode);
        robot.keyPress(keyCode);
        robot.keyRelease(keyCode);
    }
    robot.delay(PAUSE);
    robot.waitForIdle();
}
 
Example 6
Source File: MnemonicWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean updateText() {
  String text = getText();
  if (text != null) {
    int code = KeyEvent.VK_UNDEFINED;
    int index = -1;
    int length = text.length();
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
      char ch = text.charAt(i);
      if (ch != UIUtil.MNEMONIC) {
        sb.append(ch);
      }
      else if (i + 1 < length) {
        code = KeyEvent.getExtendedKeyCodeForChar(text.charAt(i + 1));
        index = sb.length();
      }
    }
    if (code != KeyEvent.VK_UNDEFINED) {
      try {
        myEvent = true;
        setText(sb.toString());
      }
      finally {
        myEvent = false;
      }
      myCode = code;
      myIndex = index;
      return true;
    }
  }
  return false;
}
 
Example 7
Source File: LocalKeyboardCore.java    From Repeat with Apache License 2.0 4 votes vote down vote up
private void typeUnknown(char c) {
	int converted = KeyEvent.getExtendedKeyCodeForChar(c);
	controller.keyPress(converted);
	controller.keyRelease(converted);
}
 
Example 8
Source File: Robot.java    From RemoteSupportTool with Apache License 2.0 4 votes vote down vote up
public synchronized void printText(String text) {
    //LOGGER.debug("printText \"{}\"", text);
    waitForIdle();

    // release keys, that may have been set previously
    //final List<Integer> oldPressedKeys = new ArrayList<>(pressedKeys);
    releasePressedKeys();

    //noinspection EmptyFinallyBlock
    try {
        // Try to print the text through the native API of the operating system.
        boolean textWasSent = false;
        if (SystemUtils.IS_OS_WINDOWS)
            textWasSent = WindowsUtils.sendText(text, this);
        else if (SystemUtils.IS_OS_MAC)
            textWasSent = MacUtils.sendText(text);
        else if (SystemUtils.IS_OS_LINUX)
            textWasSent = LinuxUtils.sendText(text);

        // Otherwise try to print the text through the Robot class.
        if (!textWasSent) {
            for (int i = 0; i < text.length(); i++) {
                final char character = text.charAt(i);
                final int code = KeyEvent.getExtendedKeyCodeForChar(character);

                if (KeyEvent.VK_UNDEFINED != code) {
                    keyPress(code);
                    keyRelease(code);
                } else {
                    LOGGER.warn("Can't detect key code for character \"{}\".", character);
                }
            }
        }

    } finally {
        // reset previously pressed keys
        //releasePressedKeys();
        //for (Integer code : oldPressedKeys) {
        //    keyPress(code);
        //}
    }
}
 
Example 9
Source File: ToolMenuItem.java    From Spade with GNU General Public License v3.0 4 votes vote down vote up
public ToolMenuItem(String name, Tool t, Character key)
{
	super(name);
	
	// This is here, so some Tools don't have to have a key assigned. We can't have key-code's for ALL the Tools! It's impossible!
	if(key != null)
	{
		HotkeyData shortcut = new HotkeyData(KeyEvent.getExtendedKeyCodeForChar(Character.toLowerCase(key)));
		super.setAccelerator(shortcut);
		Spade.addTool(key, t);
	}
	
	this.tool = t;
	
	// TRY to load the icon!
	try
	{
		URL url = tool.getClass().getResource("/res/icons/tools/" + name + ".png");
		
		if(url != null)
		{
			this.setIcon(new ImageIcon(ImageIO.read(url)));
		}
		else
		{
			this.setIcon(new ImageIcon(ImageIO.read(Spade.questionMarkURL)));
		}
		
	}
	catch(IOException e1)
	{
		System.err.println("Error: Tool '" + name + "' is missing an icon!");
	}
	
	this.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent e)
		{
			Spade.setTool(tool);
		}
	});
}
 
Example 10
Source File: KeyCodes.java    From haxademic with MIT License 4 votes vote down vote up
public static int keyCodeFromChar(char inputChar) {
	return KeyEvent.getExtendedKeyCodeForChar(inputChar);
}
 
Example 11
Source File: SystemShortcuts.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void readSystem() {
  myKeyStroke2SysShortcut.clear();

  if (!SystemInfo.isMac || !SystemInfo.isJetBrainsJvm) return;

  try {
    if (!Registry.is("read.system.shortcuts")) return;

    if (ourShkClass == null) ourShkClass = ReflectionUtil.forName("java.awt.desktop.SystemHotkey");
    if (ourShkClass == null) return;

    if (ourMethodReadSystemHotkeys == null) ourMethodReadSystemHotkeys = ReflectionUtil.getMethod(ourShkClass, "readSystemHotkeys");
    if (ourMethodReadSystemHotkeys == null) return;

    List<AWTKeyStroke> all = (List<AWTKeyStroke>)ourMethodReadSystemHotkeys.invoke(ourShkClass);
    if (all == null || all.isEmpty()) return;

    String debugInfo = "";
    for (AWTKeyStroke shk : all) {
      if (shk.getModifiers() == 0) {
        //System.out.println("Skip system shortcut [without modifiers]: " + shk);
        continue;
      }
      if (shk.getKeyChar() == KeyEvent.CHAR_UNDEFINED && shk.getKeyCode() == KeyEvent.VK_UNDEFINED) {
        //System.out.println("Skip system shortcut [undefined key]: " + shk);
        continue;
      }
      if ("Move focus to the next window in application".equals(getDescription(shk))) {
        // Skip this shortcut because it handled in IDE-side
        // see: JBR-1515 Regression test jb/sun/awt/macos/MoveFocusShortcutTest.java fails on macOS  (Now we prevent Mac OS from handling the shortcut. We can enumerate windows on IDE level.)
        continue;
      }

      KeyStroke sysKS;
      if (shk.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
        final int keyCode = KeyEvent.getExtendedKeyCodeForChar(shk.getKeyChar());
        if (keyCode == KeyEvent.VK_UNDEFINED) {
          //System.out.println("Skip system shortcut [undefined key]: " + shk);
          continue;
        }
        sysKS = KeyStroke.getKeyStroke(keyCode, shk.getModifiers());
      }
      else sysKS = KeyStroke.getKeyStroke(shk.getKeyCode(), shk.getModifiers());

      myKeyStroke2SysShortcut.put(sysKS, shk);

      if (DEBUG_SYSTEM_SHORTCUTS) {
        debugInfo += shk.toString() + ";\n";
      }
    }
    if (DEBUG_SYSTEM_SHORTCUTS) {
      Logger.getInstance(SystemShortcuts.class).info("system shortcuts:\n" + debugInfo);
    }
  }
  catch (Throwable e) {
    Logger.getInstance(SystemShortcuts.class).debug(e);
  }
}