LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Bash & Xdialog - how to code to recognize ENTER key on keyboard.? (https://www.linuxquestions.org/questions/linux-software-2/bash-and-xdialog-how-to-code-to-recognize-enter-key-on-keyboard-631806/)

craftybytes 03-31-2008 12:07 AM

Bash & Xdialog - how to code to recognize ENTER key on keyboard.?
 
Hi all..

Bash scripting query again..

1)
Say I have the following snippet in a Bash script:

Code:

Xdialog --screen-center --title "CONFIRM..!" --yesno "Are you sure you want to overwrite existing file?" 6 60
 case $? in
  0)
    echo "1" > $FIL8;;
  1)
    rm -f $FIL3;
    rm -f $FIL7;
    rerun1;
    ;;
  255)
    rm -f $FIL3;
    rm -f $FIL7;
    rerun;
    ;;
 esac

what do I need to add to the 'case' statements to check for the "ENTER" key on the keyboard being pressed..?

or do I need to use something like 'grep' or 'sed' to do this..?

If so how..?

2)
There is also another snippet of bash script that I would like to add this "ENTER" key code to as well:

Code:

Xdialog --screen-center --title "$Key Input" --inputbox "The input should be an alphanumeric string
between a minimum 8 to a maximum 48 characters..
Can contain uppercase and lowercase letters and numbers..

Please enter your input: " 20 58 2> /root/.tmp1/.inputbox.tmp.$$
retval=$?
input=`cat /root/.tmp1/.inputbox.tmp.$$`
touch $input
rm -f /root/.tmp1/.inputbox.tmp.$$
 if [ "$retval" -eq 1 ];then
  rerun
 else
  echo "entry - OK"
 fi

What code do I need to add to the 'if' statement to check for the "ENTER" key being pressed on the keyboard..?

or do I need to use 'grep' or 'sed' for this..?

If so how..?

The reason for this is - I have coded for an "OK" button in the Xdialog boxes - but I would like to allow for the user to use the "ENTER" key on the keyboard to do the exact same thing as clicking on the "OK" button as well..!!

TIA.

crafty.
.
.

unSpawn 03-31-2008 05:59 AM

In your case you don't need to AFAIK.
0) Dialog apps rely on the user selecting the right button and then pressing enter to signify their choice and
1) since you're using a graphical interface your script isn't exposed to receive enters and
2) at least with Zenity the selection can only be one of two buttons, it can't be anything else so you wouldn't be able to send an enter unless you run something that intercepts the keyboard (and you don't want to do that) and
3) your users will be baffled by the inconsistency with other apps that don't exhibit that behaviour.
You only need to work with the exit value, makes things easier for you and your users.

archtoad6 03-31-2008 12:06 PM

In other words, pressing "Enter" is already being recognized -- it's the same as clicking on the currently selected button.

Why would you want to change a default behavior that is consistent w/ the vast majority of GUI apps., across desktop environments & even OS's?

By chance, did you inherit the maintenance of this code?

craftybytes 04-01-2008 01:02 AM

Thanks for the replies..

As shown - for the 'case' statements - 0) = covers the OK button; 1) = covers the CANCEL button; 255) = covers the 'close' button for the box window..

And in a fashion - the same for the 'if/then' grouping..

Just thought that there may have been ways to add in the extra for possible ENTER key usage as well..

@archtoad6 -
Quote:

In other words, pressing "Enter" is already being recognized
Well in the case of my code - it actually isn't recognized - no matter how many times the ENTER key is pressed, there is no execution of the input - ONLY clicking on the OK button does the job..

Yes and no - I actually wrote the script - so I'll have to maintain it as well..

Anyway - the script works well and does do what it was designed for - just thought that there might have been room for some slight improvement - especially towards those who prefer to use the keyboard more than a mouse..

Much appreciated for the replies though..

crafty.
.
.

unSpawn 04-01-2008 07:25 AM

Quote:

Originally Posted by craftybytes (Post 3106747)
Well in the case of my code - it actually isn't recognized - no matter how many times the ENTER key is pressed, there is no execution of the input - ONLY clicking on the OK button does the job..

X11/Xorg, DEs and WMs adhere to certain standards so most of them behave predictably when it comes to selecting windows in the foreground, recognising "OK" and "Cancel" buttons and using keyboard events to work those. Being more thorough analysing and describing the env and code you're working in might help us help you.

archtoad6 04-01-2008 10:15 AM

craftybytes,

What was your distro/DE environment?

(I was/am using MEPIS 6.0, KDE 3.5.3)

craftybytes 04-03-2008 01:00 AM

.
Distro & DE environment =

Puppy Linux v2.15CE (kernel 2.6.18.1); & iceWM & ROX..!!

But my scripts are mainly in Bash & using Xdialog + a couple of 'yaf-splash' boxes..

So should work in ANY linux distro if got Bash & Xdialog..

Anyway - as I said previously - my scripts are working well and doing what I expected..

I'm of the belief though that - a little "positive" tweaking here & there does not go astray..

Thanks for the interest..

crafty.
.


All times are GMT -5. The time now is 11:11 AM.