• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Trapping Backspace !!! :(

Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a program to read a password from a command line input. I also have another program, a Thread to mask the characters while the user enters the password. The problem is that the program works fine, but after entering a few characters if I press the "backspace" key on the keyboard, it displays all the characters entered until then on the next line on the screen except the last character entered. The main goal of the program is to MASK the characters entered by the user. When I use the backspace or "Del" key, I should be able to delete the most recent character entered by the user while keeping all the previously entered characters masked.

Can anyone tell me why this is happening and how to do it? I am using a modification of the code provided by java.sun.com. But the original code is shown below:

The Password Reader class:

import java.io.*;
import java.util.*;

* This class prompts the user for a password and attempts to mask input with "*"

public class PasswordField {

*@param input stream to be used (e.g. System.in)
*@param prompt The prompt to display to the user.
*@return The password as entered by the user.

public static final char[] getPassword(InputStream in, String prompt) throws IOException {
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);

char[] lineBuffer;
char[] buf;
int i;

buf = lineBuffer = new char[128];

int room = buf.length;
int offset = 0;
int c;

loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;

case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) {
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in);
} else {
break loop;

if (--room < 0) {
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy(lineBuffer, 0, buf, 0, offset);
Arrays.fill(lineBuffer, ' ');
lineBuffer = buf;
buf[offset++] = (char) c;
if (offset == 0) {
return null;
char[] ret = new char[offset];
System.arraycopy(buf, 0, ret, 0, offset);
Arrays.fill(buf, ' ');
return ret;

public static void main(String argv[]) {
char password[] = null;
try {
password = PasswordField.getPassword(System.in, "Enter your password: ");
} catch(IOException ioe) {
if(password == null ) {
System.out.println("No password entered");
} else {
System.out.println("The password entered is: "+String.valueOf(password));



The Password Masking Thread:

import java.io.*;

* This class attempts to erase characters echoed to the console.

class MaskingThread extends Thread {
private volatile boolean stop;
private char echochar = '*';

*@param prompt The prompt displayed to the user
public MaskingThread(String prompt) {

* Begin masking until asked to stop.
public void run() {

int priority = Thread.currentThread().getPriority();

try {
stop = true;
while(stop) {
System.out.print("\010" + echochar);
try {
// attempt masking at this rate
}catch (InterruptedException iex) {
} finally { // restore the original priority

* Instruct the thread to stop masking.
public void stopMasking() {
this.stop = false;

The other question is, I want to know the basic difference between a carriage-return and a newline character. Is it actually possible to insert a carriage-return by using any of the keys on the keyboard? And what is the character representing the backspace character? I am not able to capture the backspace if I use '\b'.

Thanks a lot,
Awaiting your response,
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my system - linux 2.6, java 1.5 - I don't get characters in the next line.
Backspace works as expected, Del doesn't work.
Arrow left doesn't work too.

When reaching the end of line, the cursor stops, and shows the last character in clear.

Carriage-return and Newline are reminiscenzes to old typewriters.
The enter-key is the only key, normally used for both actions together.
But every key is programly controlled and may behave complete different, and I don't know the behaviour on JavaME-devices.
I don't know how to issue a CR without LF or LF without CR from Java.

Dos/ Win normally use \n\r for EOL in files, unix-like systems use \n and MacOS \r.

In former times, you could use ALT+[keycode] with keycode entered on the Numpad, to produce control-characters, which aren't reachable on the keyboard.

Where do you like to use '\b'?
As a case-statement?
Perhaps it's consumed from System.in before you get it?

(I'm afraid, there isn't much help in my posting.)
[ March 14, 2005: Message edited by: Stefan Wagner ]
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic