Wednesday, 21 August 2013

Change common keys using KeyStroke in Java

Change common keys using KeyStroke in Java

I'm trying to create an editor that have several hotkeys. This editor will
be have some function each ke we press. (btw, sorry if my english bad :D )
I've done with Enter key (backspace, delete, & arrow key too). (note: ta
is text area, javaswing)
String keyStrokeAndKey_enter = "ENTER";
KeyStroke keyStroke_enter =
KeyStroke.getKeyStroke(keyStrokeAndKey_enter);
ta.getInputMap().put(keyStroke_enter, keyStrokeAndKey_enter);
ta.getActionMap().put(keyStrokeAndKey_enter, enter);
I've done if we use alt+[key]
String keyStrokeAndKey_1 = "1";
KeyStroke keyStroke_1 = KeyStroke.getKeyStroke(KeyEvent.VK_1,
Event.CTRL_MASK);
ta.getInputMap().put(keyStroke_1, keyStrokeAndKey_1);
ta.getActionMap().put(keyStrokeAndKey_1, _1);
But, i stuck with only alphabet key (a,b,c,etc). I've try like this:
String keyStrokeAndKey_a = "a";
KeyStroke keyStroke_a = KeyStroke.getKeyStroke(keyStrokeAndKey_a);
ta.getInputMap().put(keyStroke_a, keyStrokeAndKey_a);
ta.getActionMap().put(keyStrokeAndKey_a, _a);
So, what must i do to solve my problem? Thanks before :)

No comments:

Post a Comment