
-----------------------------------
alexsb92
Sat Mar 27, 2010 7:24 pm

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

-----------------------------------
TerranceN
Sat Mar 27, 2010 8:55 pm

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:

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

Hope that helps.

-----------------------------------
TheGuardian001
Sat Mar 27, 2010 9:29 pm

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();
[/code]
