DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2

Thread: KeyEvent

  1. #1
    Join Date
    Nov 2005
    Posts
    24

    KeyEvent

    if i require keys to activate buttons and methods etc without using the mouse, do i need a keyboard focus? isnt is as easy as just saying...


    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_DOWN) {

    adjustLayout();
    }

    Obviously not cos its not working lol! any help guys?

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    Make sure that you are adding the KeyListener to the component that will have the focus.
    Code:
    JPanel pnl = new JPanel();
    pnl.addKeyListener(new KeyListener() {
      public void keyPressed(KeyEvent e) {
        // ...
      }
      public void keyReleased(KeyEvent e) {
        // ...
      }
      public void keyTyped(KeyEvent e) {
        // ...
      }
    });
    Hope this helps.
    ~evlich

Similar Threads

  1. Replies: 0
    Last Post: 07-07-2000, 08:28 AM

Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links