简体   繁体   中英

How can I append to Java KeyEvent modifiers?

I have a KeyEvent whose modifiers I want to change to include the shift key. My current approach is to create a new KeyEvent using information from the old, attempting to add the shift key to the modifiers. However, it doesn't seem to be working.

Is there a better way to do this?

event = new KeyEvent((Component) event.getSource(), KeyEvent.KEY_RELEASED, event.getWhen(), event.getModifiersEx() | KeyEvent.SHIFT_DOWN_MASK, event.getKeyCode(), KeyEvent.CHAR_UNDEFINED);

Instead of this: event = new KeyEvent((Component) event.getSource(), KeyEvent.KEY_RELEASED, event.getWhen(), event.getModifiersEx() | KeyEvent.SHIFT_DOWN_MASK, event.getKeyCode(), KeyEvent.CHAR_UNDEFINED);

I should have done this: event = new KeyEvent((Component) event.getSource(), event.getID(), event.getWhen(), event.getModifiersEx() | KeyEvent.SHIFT_DOWN_MASK, event.getKeyCode(), KeyEvent.CHAR_UNDEFINED);

Note that instead of KeyEvent.KEY_RELEASED I have event.getID().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM