Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Simple program that detects my keystroke

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Simple program that detects my keystroke

    how can i code a java program that detects my keystroke?
    for example i press enter,

    the program will display a string message "You Press Enter" + "<Enter key>"

    is it possible to make this algorithm in java?


  2. #2
    Member
    Join Date
    Aug 2009
    Posts
    53
    Thanks
    2
    Thanked 3 Times in 2 Posts

    Default Re: Simple program that detects my keystroke

    Check out the robot class, that should do the trick

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Simple program that detects my keystroke

    You can use a KeyListener. You need to have some object that listens for the event, ordinarily a Component (JFrame in the example below) in which you can call addKeyListener(KeyListener) to add the KeyListener implementation.

    public class Test extends JFrame implements KeyListener{
            /**Constructor*/
    	public Test(){
    		super();
    		addKeyListener(this);
    		setVisible(true);
    	}
    	/**Key Listener implementation*/
    	public void keyPressed(KeyEvent e){
    		System.out.println("Key Pressed");
    	}
    	public void keyReleased(KeyEvent e){}
    	public void keyTyped(KeyEvent e){}
     
     
        public static void main(String[] args) {
    		new Test();
        }
     
    }

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Simple program that detects my keystroke

        public void keyPressed(KeyEvent e){
     
            int id = e.getID();
            String keyString;
     
            if (id == KeyEvent.KEY_TYPED) {
                char c = e.getKeyChar();
                keyString = "key character = '" + c + "'"; // this part
            }
            else {
                int keyCode = e.getKeyCode();
                keyString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")";
            }
     
            System.out.println(keyString);
        }

    sir the method only displays an uppercased character ,, if i pressed a lower case character it always
    display it as an uppercased.

Similar Threads

  1. Simple Program for Cash
    By javastudent in forum Paid Java Projects
    Replies: 9
    Last Post: December 1st, 2009, 07:25 AM
  2. Error of data types and type casting in java program
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: September 2nd, 2009, 10:22 AM
  3. PLEASE HELP!!!! simple java program...
    By parvez07 in forum Object Oriented Programming
    Replies: 5
    Last Post: August 26th, 2009, 06:38 AM
  4. help with simple java program
    By parvez07 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 25th, 2009, 07:19 AM
  5. Someone please help me write a simple program!!!
    By ocean123 in forum Loops & Control Statements
    Replies: 3
    Last Post: June 14th, 2009, 09:46 PM