key binding: VK_ALT and VK_SHIFT not working
I'm trying to bind the left alt key and the right shift key in a JPanel. Here's my code:
Code Java:
int RIGHT_SHIFT = KeyEvent.VK_SHIFT;
int LEFT_ALT = KeyEvent.VK_ALT;
// right shift (move current item right)
inputMap.put(KeyStroke.getKeyStroke(RIGHT_SHIFT, 0, false), "right_shift");
actionMap.put("right_shift", new InputProcessor(PRESSED, RIGHT_SHIFT));
// left alt (move current item left)
inputMap.put(KeyStroke.getKeyStroke(LEFT_ALT, 0, false), "left_alt");
actionMap.put("left_alt", new InputProcessor(PRESSED, LEFT_ALT));
Neither alt keys nor the shift keys have the effect I want.
But if I change my code to this:
Code Java:
int RIGHT_SHIFT = KeyEvent.VK_S;
int LEFT_ALT = KeyEvent.VK_A;
// right shift (move current item right)
inputMap.put(KeyStroke.getKeyStroke(RIGHT_SHIFT, 0, false), "right_shift");
actionMap.put("right_shift", new InputProcessor(PRESSED, RIGHT_SHIFT));
// left alt (move current item left)
inputMap.put(KeyStroke.getKeyStroke(LEFT_ALT, 0, false), "left_alt");
actionMap.put("left_alt", new InputProcessor(PRESSED, LEFT_ALT));
It works.
So what does VK_ALT and VK_SHIFT refer to? Why does using VK_S and VK_A make the s and a keys effective but using the VK_ALT and VK_SHIFT doesn't make the alt and shift keys effective? What would one use if he wanted to use the alt and shift keys - and specifically the left alt key and the right shift key?
Re: key binding: VK_ALT and VK_SHIFT not working
Quote:
So what does VK_ALT and VK_SHIFT refer to?
Very useful advice for you: If you are unsure of how the code is going to run, especially with Listeners, put some System.out.println() statements in your Listeners that print to the console all the relevant information on the event occurring. I used this tactic the other day when I was trying to figure out how Mouse Wheel Listeners work. You simply put the System.out.println() statements in the Listener and then just play around with different input until you begin to understand what it is doing and how it reads the input.
I'm unsure how to help you with your problem. Mainly because I prefer to use Listeners instead of simply mapping keys. Maybe someone else can provide more help on key mapping.
Re: key binding: VK_ALT and VK_SHIFT not working
Thanks for the suggestions aussiemsgr.
I just cross-posted this on another forum: key binding: VK_ALT and VK_SHIFT not working - Java Forums
Re: key binding: VK_ALT and VK_SHIFT not working
Can you use a KeyListener rather than bindings? I know the keypressed of KeyListener is fired for alt's and shift's. Just tried the keybinding approach and it doesn't work for alts and shifts (never tried so its news to me)....there's probably a logical explanation buried in the api somewhere as to why but I don't have time to go deep into it to find that reason.
PS appreciate the link indicating the cross post.
Re: key binding: VK_ALT and VK_SHIFT not working
I think you need to use keyPress and keyRelease.
For example:
If you wanted to press ALT+F+S to save a file, you would use
Re: key binding: VK_ALT and VK_SHIFT not working
I received a reply in that thread I cross-posted on. It gives the solution to my problem and explains everything (including why ALT and SHIFT didn't work in key binding).
Thanks for everyone's help anyway.