Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: key binding: VK_ALT and VK_SHIFT not working

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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:

    	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:

    	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?


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: key binding: VK_ALT and VK_SHIFT not working

    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.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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

  4. The Following User Says Thank You to gib65 For This Useful Post:

    JavaPF (November 25th, 2010)

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,237
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default 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.

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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

    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_F);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_ALT);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #6
    Member
    Join Date
    Oct 2010
    Posts
    31
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default 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.

Similar Threads

  1. SQL query not working
    By mjpam in forum What's Wrong With My Code?
    Replies: 11
    Last Post: September 28th, 2010, 05:15 PM
  2. [SOLVED] Buttons not working..
    By khms in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 2nd, 2010, 06:01 PM
  3. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  4. Extracting the BINDING element from WSDL file
    By Sai in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2010, 02:56 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM