Results 1 to 5 of 5
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default key binding: VK_ALT and VK_SHIFT not working

    This is a cross-post from here

    I'm trying to bind the left alt key and the right shift key in a JPanel. Here's my code:

    Java Code:
    	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:

    Java Code:
    	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?

    On the forum this is a cross-post of, I received a reply suggesting I experiment with a KeyListener. I've done that and I've found that the key code for ALT is 18 and SHIFT is 16 - just as the values of VK_ALT and VK_SHIFT are. I've also found that both ALT keys are 18 and both SHIFT keys are 16. This is unfortunate because I'd really like a way to distinguish between them. Another strange thing I've learnt about the ALT key is that it only responds to every odd key stroke. The first time I press it, it prints out 18. The second time, nothing happens. The third time, 18 prints out. The fourth time, nothing... and so on. If I press it an odd number of times, it seems that any key after that will not respond. It would be nice if I could get around this as well.

    Any help would be much appreciated.

  2. #2
    aephyr is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    For modifier keys, you must provide the appropriate modifier for KeyStrokes on key press. However, if you want the KeyStroke on key release, then you do not include the modifier.

    So here would be the KeyStrokes for ALT press and ALT release:

    KeyStroke.getKeyStroke(KeyEvent.VK_ALT, InputEvent.ALT_DOWN_MASK, false)
    KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true)

    The issue you are experiencing with every other ALT press being sent to your KeyListener is because JFrame toggles the focus to or from the title bar on ALT press. Try pressing and releasing ALT and then the spacebar and you will see the system menu popup at the titlebar. If you consume the ALT press, then that will disable the JFrame from moving the focus. Note that key bindings are automatically consumed.

    Supposedly you can distinguish right and left keys via the getKeyLocation() method from the KeyEvent.
    Last edited by aephyr; 11-25-2010 at 07:16 PM.

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Thanks aephyr,

    Your suggestions were exactly what I needed.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Welcome aephyr! Good to see you sharing your knowledge in one more place!

    db

  5. #5
    aephyr is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Help: Key Binding
    By mine0926 in forum New To Java
    Replies: 0
    Last Post: 08-06-2010, 04:52 AM
  2. Key Binding problem
    By dilpreet28 in forum New To Java
    Replies: 4
    Last Post: 06-22-2010, 03:59 AM
  3. dynamic binding
    By apparao in forum New To Java
    Replies: 1
    Last Post: 02-16-2010, 08:41 AM
  4. [SOLVED] Key binding Q
    By playwin2 in forum New To Java
    Replies: 3
    Last Post: 11-04-2008, 06:04 AM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •