Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reading characters without having them show up on the screen?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
alexsb92




PostPosted: Sat Mar 27, 2010 7:24 pm   Post subject: Reading characters without having them show up on the screen?

Hey guys,

I am trying to read a character in Java without having it show up on the screen. It is possible with the hsa class, but i absolutely hate ready to program, and I do not want to use it. Is there any way to do that with Standard Java libraries? Basically I am looking for the Java equivalent of readkey that is found in Pascal.

Thanks
Sponsor
Sponsor
Sponsor
sponsor
TerranceN




PostPosted: Sat Mar 27, 2010 8:55 pm   Post subject: Re: Reading characters without having them show up on the screen?

I don't know what readkey is in Pascal so I hope this is what you mean. Look into the KeyListener and KeyEvent classes. If you implement KeyListener in your class then you can use the KeyTyped method to get the KeyEvent. Also, in order for it to get key events you need to add it to a Component using addKeyListener. Here is an example:

Java:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

// This class is both a Component (because it inherits frame) and a KeyListener (because it implements KeyListener)
public class KeyTypedExample extends Frame implements KeyListener
{
    // Entry point for program
    public static void main(String[] args)
    {
        // Creates a new instance
        KeyTypedExample keyTypedExample = new KeyTypedExample();
    }
   
    // Constructor for class
    public KeyTypedExample()
    {
        // sets size and makes window visible
        setSize(500, 500);
        setVisible(true);
       
        // assigns its own key listener to itself
        addKeyListener(this);
    }
   
    // This is triggered whenever a key is pressed then released
    public synchronized void keyTyped(KeyEvent e)
    {
        // Prints the char representation of what was pressed
        System.out.println(e.getKeyChar());
    }
   
   
    // These methods are not used, but have to be defined because of KeyListener
    public synchronized void keyPressed(KeyEvent e){}
    public synchronized void keyReleased(KeyEvent e){}
}


Hope that helps.
TheGuardian001




PostPosted: Sat Mar 27, 2010 9:29 pm   Post subject: Re: Reading characters without having them show up on the screen?

If you don't want a GUI one, I believe java.io.Console has a readPassword method which doesn't echo. It returns an array of characters though, so you'd have to put it in a string yourself. to set it as the default console input, use

code:

myConsole = System.console();
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: