button and "keyboard ENTER key"


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: button and "keyboard ENTER key"

Hybrid View

  1. #1
    Join Date
    Jul 2005
    Posts
    23

    Question button and "keyboard ENTER key"

    Hi,

    I have a dialog box with few buttons. How do I make a button "clicked" whenever it get focussed and I press "Enter" on the keyboard? Currently I have to use mouse and click on the button.

    Thank you.

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299

    Class to make Enter pressed as an ActionEvent

    I assume that you want the actionPerformed() method to be called when you press Enter when the button has the focus.

    Here's a class that does that. Add it as a KeyListener for the button:

    button.addKeyListener(new MakeEnterDoAction());



    public class MakeEnterDoAction extends KeyAdapter {

    public void keyPressed(KeyEvent ke) {
    if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
    Object src = ke.getSource();
    Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(
    new ActionEvent(src,
    ActionEvent.ACTION_PERFORMED,
    "Enter"));
    }
    } // end keyPressed()
    } // end class


    Norm

  3. #3
    Join Date
    Jul 2005
    Posts
    23
    Thank you very much norm..

    Ill put that in my program + with some notes for your contribution

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center