0

I need to delete last character entered by user while entering password and I need help in handling Backspace pressed by user.

Below is the code I wrote for Login, how to modify it so that I can handle Backspace ?

void login(){

    //As mentioned on internet
    Console con=null;
    con=System.console();

    System.out.println("==============================================================");
    System.out.println("\t\t\t LOGIN MENU\n");

    System.out.print("\tEnter User Name: ");
        String userName=scan.next();
    System.out.print("\tEnter Your Password: ");
        char[] password = con.readPassword("");

    System.out.println(password);
    System.out.println("==============================================================");


    String pass=new String(password);

    //Arrays.fill is used to fill 'spaces' to replace password (Security Concerns)
    java.util.Arrays.fill(password, ' ');

    //System.out.println(password);

    con.flush();    

}

Thank You! :)

  • If the built-in `readPassword` method doesn't do what you want, you will probably have to do your own keyboard input to read single keystrokes. Unfortunately, I don't know how to do this, and it is probably OS-dependent. – ajb Jan 05 '16 at 08:03
  • This code will never even see a backspace. It's handled by the teletype driver. Unclear what you're asking. – user207421 Jan 05 '16 at 08:34
  • @EJP Actually I need to make a Login Screen I have used console.readPassword() to take input without displaying the password, I was not aware that this will not handle backspace. But I need the functionality to handle backspace which will erase prev character. Can you suggest another input class using which I can hide input or echo '*' and which can handle Backspace? Thank You – rishabh_prasad Jan 05 '16 at 12:39
  • You don't have to handle backspace. The functionality of handling a backspace is already provided by the TTY driver. I've already said all this. – user207421 Jan 05 '16 at 19:05

0 Answers0