I see what you are trying to do. None of the other answers are correct.
You want to get the βcorrectβ KeyEvent constant for the given character, and you want to do this without creating some kind of lookup table whose length should be two million long.
Indeed, thinking will help you here. It will be pretty slow. But he will do the job. Is the work really needed - another question. :-)
The function you want about is probably something like this:
public int getKeyEventConstant(final char c) throws Exception { final Field field = KeyEvent.class.getField(String.format("VK_%S", Character.valueOf(c))); assert field != null; return field.getInt(null); }
Then you can feed it as shown below, although you will have all kinds of problems if the supplied String contains characters, the function described above is not edited to handle exceptions correctly:
public toKeyEventCodes(final String s) { int[] returnValue = null; if (s != null && !s.isEmpty()) { final Collection<Integer> codes = new ArrayList<Integer>(s.length()); final char[] chars = s.toCharArray(); assert chars != null; assert chars.length > 0; for (final char c : chars) { if (!Character.isWhitespace(c)) {
All this code has not been verified. Good luck. I assume that you still have something too complicated, but hopefully this will force you to point in the right direction.
source share