Results 1 to 4 of 4
  1. #1
    arimakidd is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Question KeyBoard Problem

    I am learning Java and following along using a text book. There is some simply code to detect a keystroke from the keyboard and then just output to a label. For some reason its not recognizing any keystroke all. Can someone tell me why?? Here is the code:
    Java Code:
    //KeyTester
    //Demonstrates how Key Strokes are detected
    
    
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class KeyTester extends Applet implements KeyListener
    {
    	Label lblOutput = new Label("A");
    
    
    	public void init()
    	{
    		setLayout(new BorderLayout());
            add(new Label("Please press a key"), BorderLayout.NORTH);
            add(lblOutput, BorderLayout.CENTER);
    
            lblOutput.setFont(new Font("SansSerif", Font.BOLD, 100));
            lblOutput.setAlignment(Label.CENTER);
    
            this.requestFocus();
            addKeyListener(this);
    
    
    	}//end of init method
    
    	public void keyPressed(KeyEvent e)
    	{
    
    
    	}//end of keyPressed Mehthod
    
    	public void keyReleased(KeyEvent e)
    		{
    
    
    	}//end of keyReleased Mehthod
    
    	public void keyTyped(KeyEvent e)
    		{
    			char theChar = e.getKeyChar();
    			String theCharacter = String.valueOf(theChar);
    			lblOutput.setText(theCharacter);
    
    	}//end of Method
    }//end of applet
    Help appreciated. Oh bye the way, the name of the text book is "JAVA 2 fast&easy web development".

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Read the API doc for KeyEvent. I don't think you have any components in your class that receive them.
    Also look at the setFocusable() method.
    Add println()s to all the listener methods to see which are called.
    Also add a listener to the label.
    Last edited by Norm; 09-19-2008 at 03:10 AM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    //  <applet code="KeyTesterRx" width="400" height="400"></applet>
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class KeyTesterRx extends Applet implements KeyListener
    {
        Label lblOutput = new Label("A");
    
        public void init()
        {
            setLayout(new BorderLayout());
            add(new Label("Please press a key"), BorderLayout.NORTH);
            add(lblOutput, BorderLayout.CENTER);
    
            lblOutput.setFont(new Font("SansSerif", Font.BOLD, 100));
            lblOutput.setAlignment(Label.CENTER);
    
    //        this.requestFocus();
            lblOutput.addKeyListener(this);
            lblOutput.setFocusable(true);
            lblOutput.requestFocusInWindow();
        }
    
        public void keyPressed(KeyEvent e)
        {
            System.out.println("keyPressed");
        }
    
        public void keyReleased(KeyEvent e)
        {
            System.out.println("keyReleased");
        }
    
        public void keyTyped(KeyEvent e)
        {
            char theChar = e.getKeyChar();
            String theCharacter = String.valueOf(theChar);
            lblOutput.setText(theCharacter);
            System.out.println("keyTyped");
        }
    }

  4. #4
    arimakidd is offline Member
    Join Date
    Sep 2008
    Posts
    5
    Rep Power
    0

    Thumbs up You are correct!

    Yes, you are correct. On checking the API, and I paste, "The listener object created from that class is then registered with a component using the component's addKeyListener method." I did not register my label and now the code works just fine. Only had to change two lines. I'll just highlight the lines in question.
    Before Correction:
    Java Code:
    this.requestFocus();
    addKeyListener(this);
    After Correction:
    Java Code:
    lblOutput.requestFocus();
    lblOutput.addKeyListener(this);
    Will have a look at those two other methods mentioned(setFocusable and requestFocusInWindow), but the changes mentioned here work just fine.

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 01:08 PM
  2. getting each character from keyboard
    By Sreejesh25 in forum Advanced Java
    Replies: 6
    Last Post: 03-05-2010, 07:12 PM
  3. Reading Keyboard
    By ushigley in forum New To Java
    Replies: 4
    Last Post: 08-20-2008, 01:50 PM
  4. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 02:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •