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

**URL:** <https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863>\
**Category:** Ruby\
**Created:** [July 29, 2009, 5:12am UTC](https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863 "2009-07-29T05:12:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_dunno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@dr\_dunno](https://www.ruby-forum.com/u/dr_dunno)\
**Post date:** [July 29, 2009, 5:12am UTC](https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863/1 "2009-07-29T05:12:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dr\_dunno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@dr\_dunno](https://www.ruby-forum.com/u/dr_dunno)\
**Post date:** [July 29, 2009, 5:16am UTC](https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863/2 "2009-07-29T05:16:35Z")

</div>

Sorry a small correction. The test function definition line should be  
changed.

Replace  
def TestFunc  
with  
def testFunc

-DR

---

<div class="post-metadata">

**Author:** ![dr\_dunno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@dr\_dunno](https://www.ruby-forum.com/u/dr_dunno)\
**Post date:** [July 29, 2009, 6:43am UTC](https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863/3 "2009-07-29T06:43:28Z")

</div>

Hi,

2009/7/29 Deiva R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Â myPassword = “”  
> ensure  
> Â while (password == “”) || (password != “” && input != “\r”)  
> Â Â Â end
> 
> Password: \*\*\*\*\*\*\*\*\*\*\*  
> 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?  
> Try Kernel#select like this:

def read\_char  
system "stty raw -echo "  
ch = STDIN.getc  
if ch.chr==27.chr  
STDIN.getc while(select([STDIN],nil,nil,0))  
end  
ensure  
system "stty -raw echo "  
return ch  
end

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![dr\_dunno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@dr\_dunno](https://www.ruby-forum.com/u/dr_dunno)\
**Post date:** [July 29, 2009, 8:21am UTC](https://www.ruby-forum.com/t/ignore-f1-f12-keys-when-user-enters-text-input-on-console/172863/4 "2009-07-29T08:21:36Z")

</div>

Heesob P. wrote:

> Try Kernel#select like this:
> 
> def read\_char  
> system "stty raw -echo "  
> ch = STDIN.getc  
> if ch.chr==27.chr  
> STDIN.getc while(select([STDIN],nil,nil,0))  
> end  
> ensure  
> system "stty -raw echo "  
> return ch  
> end

Excellent! It works like a charm. Thanks a bunch. 🙂
