Solved

Determine if F1 key is pressed in Bash script.

Posted on 2004-09-15
8
2,770 Views
Last Modified: 2012-05-05
Hi, I'm trying to determine if a Function key (F1)  has been pressed.

Using - dumpkeys - I know the key code for F1 is "\033[[A"

Now, I write a bash script like this

#!/bin/sh
echo please enter the F1 function key
read -sn3 key
if [ $key == "\033[[A" ]; then
        echo you pressed the F1 key
        else
        echo you pressed some other key
fi

But this dosen't work. Can someone tell me how to do this proper.
Thanks


0
Comment
Question by:Airgazm
  • 4
  • 3
8 Comments
 
LVL 23

Expert Comment

by:Mysidia
ID: 12069840
well ^[[[A is 4 characters.

But if the user presses something else it will only be one character.
Perhaps

read -sn1 key
if [ $key == "\033" ] ; then
     read -sn3 key
     if [ $key == "[[A" ] ; then
          echo "You pressed the F1 key"
     else
          echo "You pressed some other key"
     fi
else
  echo "You pressed some other key"
fi
0
 

Author Comment

by:Airgazm
ID: 12069923
Did you try that on your system? It dosen't work correctly here.

I'm pretty sure a function key is actually 3 keys but I could be wrong.  

If I do a simple command on prompt

read key
when I press the F1 key it returns ^[[A  - 3 chars plus the esc

I have looked on the net for an answer and have found nothing covering how Linux returns these special codes with a read function.



0
 
LVL 11

Accepted Solution

by:
dimitry earned 250 total points
ID: 12070242
This code is working:
--------------------------------------------------------------------------------
#!/bin/sh
f1='\[\[A'
f2='\[\[B'
SUCCESS=0
echo please enter the F1 function key
read -n4 key                      # Read 4 characters.
echo -n "$key" | grep "$f1"  #Check if character code detected.
if [ "$?" -eq $SUCCESS ]
then
  echo "F1 key pressed."
  exit $SUCCESS
fi
echo -n "$key" | grep "$f2"  #Check if character code detected.
if [ "$?" -eq $SUCCESS ]
then
  echo "F2 key pressed."
  exit $SUCCESS
fi
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Airgazm
ID: 12070415
Hmm, that code dosen't work correctly either.  

It didn't print anything other than the char input "^[[A" and then a ~







0
 

Author Comment

by:Airgazm
ID: 12070422
Opps I meant char input "^[[11~"  

0
 
LVL 11

Expert Comment

by:dimitry
ID: 12070430
It is working on my RH 9.0 machine...
Are you sure you took the code as is ?
0
 

Author Comment

by:Airgazm
ID: 12070597
Ok, you were right, I was using Putty and it wasn't set to Linux mode, I changed it and now it works.  
Seems like the code could be more simple but it does what it is suppose to do.

Thanks
0
 
LVL 11

Expert Comment

by:dimitry
ID: 12070817
You are welcome
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Is it possible to combine several text files into one big one? (redhat v5 enterprise) 4 110
Linux daemon 11 356
inotifywait in linux 2 287
Linux CentOs 6.7 : i can't start service 8 96
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question