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
    Posts
    11

    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 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);
    thread.start();

    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);
    }
    ((PushbackInputStream)in).unread(c2);
    } else {
    break loop;
    }

    default:
    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;
    break;
    }
    }
    maskingthread.stopMasking();
    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) {
    ioe.printStackTrace();
    }
    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) {
    System.out.print(prompt);
    }

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

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

    try {
    stop = true;
    while(stop) {
    System.out.print("\010" + echochar);
    try {
    // attempt masking at this rate
    Thread.currentThread().sleep(1);
    }catch (InterruptedException iex) {
    Thread.currentThread().interrupt();
    return;
    }
    }
    } finally { // restore the original priority
    Thread.currentThread().setPriority(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,
    Sarin

  2. #2
    Join Date
    Sep 2008
    Posts
    1

    hi

    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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