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


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

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

  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 {
    //غcl
    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 {
    > //غcl
    > 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();
    > }
    > }
    >
    >
    >
    >




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