Solved

Determine if F1 key is pressed in Bash script.

Posted on 2004-09-15
8
2,655 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
yum update fails 11 372
tallylog file 2 2,598
How to redirect a serial com to a KVM machine on Proxmox 3 258
Linux 10 106
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now