Ignore F1-F12 keys when user enters text input on console



I am writing a Ruby script to accept a user's input which will be used
as a password later on. The user input it taken using STDIN.getc. Since
it is a password input, when the user enters the characters on the
keyboard, the entered characters are not displayed and each character is
obscured by displaying an asterix (*).

The code snippet is shown below.

---------START---------
def TestFunc
myPassword = ""

print "Password: "
myPassword = getPassword

print "My password: #{myPassword}\n"
end

def read_char
system "stty raw -echo"
ch = STDIN.getc
ensure
system "stty -raw echo"
return ch
end

def getPassword
password = ""
input = ""
isValidLastChar = 1
invalidCharacterDetected = 0

while (password == "") || (password != "" && input != "\r")
char = read_char
input = char.chr
if input == "\r"
next
end

if char == 127 # backspace character pressed
if password != ""
password = password.chop
print "\b \b"
end
elsif input != "" && input[0] > 32 && input[0] < 127
print "*"
password = password + input
end
end

print "\n"
return password
end
----------END----------

For this code, if I entered "1234" (without the quotes), followed by the
F1 key, the PageUp key, the Right arrow key and finally pressed Enter, I
see the following output on the screen:

-----SCREEN OUTPUT-----

[root@morpheus ~]# irb
irb(main):001:0> load "TestRuby.rb"
=> true
irb(main):002:0> testStub
Password: ***********
My password: 1234[11~[5~[C
=> nil
irb(main):004:0>

-----------------------

Observe the printed output.
1 -> displayed as 1
2 -> displayed as 2
3 -> displayed as 3
4 -> displayed as 4
F1 -> displayed as [11~
PgUp -> displayed as [5~
Right arrow -> displayed as [C

I am finding it difficult to figure out a way to ignore/process special
keys (F1-F12, Up/Down/Left/Right, PgUp, PgDn, Home, End, etc.).

Any suggestions or help?
--
Posted via http://www.ruby-forum.com/.

.



Relevant Pages

  • Re: Ignore F1-F12 keys when user enters text input on console
    ... The user input it taken using STDIN.getc. ... the entered characters are not displayed and each character is ... obscured by displaying an asterix. ... def TestFunc ...
    (comp.lang.ruby)
  • Re: curses bkgd problem
    ... On 2004-03-09, Gandalf wrote: ... it is displaying $ characters in black instead of displaying ... > def clrscr(): ... the characters used to draw the box. ...
    (comp.lang.python)
  • Re: Sharing objects between processes
    ... one updating thread takes user input, ... I can use a single lock over the entire object; ... a write is something I also want to avoid. ... def write_random: ...
    (comp.lang.python)
  • Re: [Full-Disclosure] How T-Mobils network was compromised
    ... User input is passed to a function which sanitizes the input by ... converting "dangerous" characters to HTML representations. ... Function is flawless from a programming perspective and performs as ... should have occurred is a check for "../" in a loop. ...
    (Full-Disclosure)
  • avoid which characters in text fields with vb- and jscript?
    ... native win2k3 srv/xp clients environment on our IIS 6.0 intranet ... Have made various tables storing user input, ... sure to clean user inputs for unwanted characters before storing in ... Using the vbscript Replace funktion Im able to remove quotes ...
    (microsoft.public.scripting.vbscript)