/** * This is hack. AWT doesn't allow to create KeyStroke with specified key code and key char * simultaneously. Therefore we are using reflection. */ private static KeyStroke getKeyStrokeWithoutMouseModifiers(KeyStroke originalKeyStroke) { int modifier = originalKeyStroke.getModifiers() & ~InputEvent.BUTTON1_DOWN_MASK & ~InputEvent.BUTTON1_MASK & ~InputEvent.BUTTON2_DOWN_MASK & ~InputEvent.BUTTON2_MASK & ~InputEvent.BUTTON3_DOWN_MASK & ~InputEvent.BUTTON3_MASK; try { Method[] methods = AWTKeyStroke.class.getDeclaredMethods(); Method getCachedStrokeMethod = null; for (Method method : methods) { if (GET_CACHED_STROKE_METHOD_NAME.equals(method.getName())) { getCachedStrokeMethod = method; getCachedStrokeMethod.setAccessible(true); break; } } if (getCachedStrokeMethod == null) { throw new IllegalStateException("not found method with name getCachedStrokeMethod"); } Object[] getCachedStrokeMethodArgs = new Object[] { originalKeyStroke.getKeyChar(), originalKeyStroke.getKeyCode(), modifier, originalKeyStroke.isOnKeyRelease() }; return (KeyStroke) getCachedStrokeMethod.invoke(originalKeyStroke, getCachedStrokeMethodArgs); } catch (Exception exc) { throw new IllegalStateException(exc.getMessage()); } }