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 4 of 4

Thread: Listening to 2 Key Presses

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Listening to 2 Key Presses

    This should be a fairly easy answer, but I cannot seem to find anything helpful on google.

    I have a JTextArea that I want to provide the option to print. In order to activate that option, the user will press the standard: CTRL+P. How do I write a KeyListener to respond to the CTRL and P keys causing events?

    Any help is appreciated.
    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/


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Listening to 2 Key Presses

    Probably a better fit for a KeyBinding (see How to use KeyBindings) where you specify the KeyStroke as something like
    KeyStroke.getKeyStroke("P",InputEvent.CTRL_MASK )

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Listening to 2 Key Presses

    Ok, I don't understand how to get the Input Map. It just says:
    component.getInputMap().put(KeyStroke.getKeyStroke ("SPACE"),
    "pressed");
    for getting the component, and doesn't mention what component is for their examples. I tried to get the map to the JFrame, but it says it cannot find the symbol getInputMap(). I then tried to get the map for my ContentPane (which is a JScrollPane for this window) and I got the same error.
    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/

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Listening to 2 Key Presses

    The method getInputMap() is defined in JComponent, so you can call the method on anything that is or extends this class (JFrame does not, JScrollPane does). If you continue to get the error you will have to post a more specific example that demonstrates the compiler error

Similar Threads

  1. Listening to JMenuItem keyboard focus
    By nik_meback in forum AWT / Java Swing
    Replies: 0
    Last Post: December 31st, 2010, 04:02 AM
  2. Replies: 1
    Last Post: December 6th, 2010, 10:09 AM
  3. video game problem - delay in response to arrow key presses
    By gib65 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2010, 07:39 PM