DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Jul 2004
    Posts
    83

    Keyboard Listener?

    Hey I was wondering if anyone could help me with keyboard listener. I have no idea how to use it but I want to know if the left arrow key is pushed and **** etc...can anyone help me or know of any links that explains it well?
    They say if you play a Microsoft Windows CD backwards it will play satanic messages. But thats nothing, if you play it forwards it installs Windows.

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Look up the KeyEvent class for the other constants.

    Code:
            class MyKeyListener implements KeyListener
            {
                public void keyTyped(KeyEvent e)
                {
                }
                public void keyReleased(KeyEvent e)
                {
                }
                public void keyPressed(KeyEvent e)
                {
                    if(e.getKeyCode() == VK_LEFT)
                    {
                       //you pressed the left arrow key
                    {
                }
            }
            MyComponent.addKeyListener(new MyKeyListener());

  3. #3
    Join Date
    May 2005
    Posts
    1
    Does that enable you to press more than one key at the same time? I'm just trying a basic 2 player pong game, and I don't know how to allow more than one key to work at once.

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Yes, there are different combinations like shif+key, ctrl+key, ctrl+alt-key
    etc. that you can detect using the SwingUtilities class methods on the key event.
    However, keeping track of two or three char-keys that are pressed (almost)
    simultaneously is messy, you must at least disable the char-repeat function; when
    you get a keyPressed event then you shold process it but not do it again before you
    have received a keyReleased event for the same key.
    eschew obfuscation

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