How to trap the backspace character ?

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to trap the backspace character ?

  1. #1
    Join Date
    Feb 2005

    Unhappy How to trap the backspace character ?

    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 But the original code is shown below:

    The Password Reader class:

    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.
    *@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 = {
    case -1:
    case '\n':
    break loop;

    case '\r':
    int c2 =;
    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(, "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:


    * 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,

  2. #2
    Join Date
    Sep 2008


    Hey sarin did you get the solution??.. if yes please share with us....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center