DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Dylan Guest

    How to detect function key (like F1,F2,...etc)


    Dear all

    I used following Java code to get key from keyboard, however this function
    KeyEvent.getKeyCode() can not detect any function (like F1,F2,...etc). Could
    you give me a hand on this case. Thanks for your help. ^^


    import java.awt.*;
    import java.awt.event.*;

    public class keyEventTest extends Frame implements KeyListener {
    //«Øºc¤l
    keyEventTest() { super(); }
    keyEventTest(String title) { super(title); }

    public static void main(String[] arg) {
    keyEventTest keyScreen = new keyEventTest("This is a test Frame");
    TextArea ta = new TextArea();
    Button bt = new Button("Button");

    keyScreen.setLayout(new BorderLayout());
    keyScreen.add(ta);
    keyScreen.add(bt,BorderLayout.SOUTH);
    ta.addKeyListener(new keyEventTest());
    keyScreen.setLocation(100,100);
    keyScreen.setSize(500,500);
    keyScreen.setVisible(true);
    }

    //Impletement keyListener's method
    public void keyPressed(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {
    e.getKeyCode();
    }
    }




  2. #2
    Volker Held Guest

    Re: How to detect function key (like F1,F2,...etc)

    Hi Dylan,

    just put the e.getKeyCode() method in:
    public void keyPressed( KeyEvent e ){;}

    this way you will get your int keycodes.

    Volker

    Dylan wrote:
    > Dear all
    >
    > I used following Java code to get key from keyboard, however this function
    > KeyEvent.getKeyCode() can not detect any function (like F1,F2,...etc). Could
    > you give me a hand on this case. Thanks for your help. ^^
    >
    >
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > public class keyEventTest extends Frame implements KeyListener {
    > //«Øºc¤l
    > keyEventTest() { super(); }
    > keyEventTest(String title) { super(title); }
    >
    > public static void main(String[] arg) {
    > keyEventTest keyScreen = new keyEventTest("This is a test Frame");
    > TextArea ta = new TextArea();
    > Button bt = new Button("Button");
    >
    > keyScreen.setLayout(new BorderLayout());
    > keyScreen.add(ta);
    > keyScreen.add(bt,BorderLayout.SOUTH);
    > ta.addKeyListener(new keyEventTest());
    > keyScreen.setLocation(100,100);
    > keyScreen.setSize(500,500);
    > keyScreen.setVisible(true);
    > }
    >
    > //Impletement keyListener's method
    > public void keyPressed(KeyEvent e) {}
    > public void keyReleased(KeyEvent e) {}
    > public void keyTyped(KeyEvent e) {
    > e.getKeyCode();
    > }
    > }
    >
    >
    >
    >




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