Search code examples
javatextawtkeyevent

KeyEvent.getKeyText() is returning special characters in OSX (e.g. for 'VK_ENTER') but not on Windows XP, any solution?


I am using KeyEvents and

KeyEvent.getKeyText(KeyEvent.VK_...) 

in order to get the text corresponding to the KeyEvent.

In OSX (Lion) when I request the text for a special key, like TAB or ENTER (KeyEvent.VK_ENTER) I get the corresponding symbol (e.g. for Enter, I get the curved arrow). The same in a Windows XP system gives me the key name (e.g. "Enter") instead of the symbol.

Is there a solution to get the special characters in XP, too?


Solution

  • MAC have different implementation:

    These were changed intentionally to provide the proper symbols for menu shortcuts in Swing for in-window menu bars. Instead of using the word "Meta" or "Command" we actually now return the unicode symbol that the system menu bar shows.

    In that case, you can use the constants for that keys, VK_ENTER, e.g.

    if (evt.getKeyCode() == KeyEvent.VK_DOWN || evt.getKeyCode() == KeyEvent.VK_KP_DOWN) {}
    

    Or you can map the keys to its string names with Map

    map.put(KeyEvent.VK_ENTER, '\u21b5'); \\ ↵