use function keys in bash

Chris F.A. Johnson c.f.a.johnson-bJEeYj9oJeDQT0dZR+AlfA at public.gmane.org
Fri Feb 20 11:43:23 UTC 2004


On Thu, 19 Feb 2004, Jeremy Wakeman wrote:

> Hi.
>
> I have used tcsh for several years, but I am tired of having to relearn
> commands/syntax/settings/rc files everytime I use a linux box that does
> not have tcsh installed.  So, I am in the process of switching to bash.
> The one thing that I cannot seem to get bash to do that tcsh does is to
> execute a command when I press a function key.
>
> In my .tcshrc, I have several entries as follows:
>
> bindkey -c [18~ "screen -d -r mutt || screen -S mutt mutt"
> bindkey -c [19~ "screen -d -r mang || screen -S mang mang"
> bindkey -c [21~ "screen -d -r mp3blaster || screen -S mp3blaster mp3blaster -a ~/.playlist_01.lst -t=500"
> bindkey -c [23~ "screen -d -r topcheck || screen -S topcheck topcheck"
> bindkey -c [24~ "screen -list"
>
> With this setup, I can just press F7 to call forward the mutt screen (or,
> start one if there isn't one running), F10 for mp3blaster, etc.
>
> I read through the readline stuff in bash(1), but I don't see anything
> about executing non-builtin commands, and none of the things I tried in
> .inputrc worked.
>
> Is there a way to do this, or am I stuck?


    This function reads a single key; as written, it doesn't check for
    function keys, just cursor keys and mouse button presses (there's
    another function that actually decodes the mouse clicks), but it's
    quite easy to add function keys. There are variables that need to
    be defined, as well.

    There are couple of scripts on my website that use this function,
    in one form or another. See the URL below. The scripts are
    mouse-demo and pop3ck.

    There are more portable ways of doing it; this is purely bash:

get_key() { #== store keypress from list of permissible characters
    local OKchars=${1:-"$allkeys"}
    local k
    local error=0
    local gk_tmo=${getkey_time:-${DFLT_TIME_OUT:-600}}
    local ESC_END=[a-zA-NP-Z~^$]
    type read_mouse >/dev/null 2>&1 || read_mouse() { :; }
    mouse_x=0 mouse_y=0 mouse_b=0 mouse_line=0
    printf "$mouse_on"
    stty -echo
    while :; do
      IFS= read -r -d '' -sn1 -t$gk_tmo _GET_KEY </dev/tty 2>&1 || break
      index "$OKchars" "$_GET_KEY"
      if [ "$_INDEX" -gt 0 ]
      then
	case $_GET_KEY in
	    ${ESC})
		while :; do
		  IFS= read -rst1 -d '' -n1 k </dev/tty || break 2
		  _GET_KEY=$_GET_KEY$k
 		  case $k in
		      $ESC_END)
			  [ "$_GET_KEY" = "$MSI" ] && read_mouse
			  break 2
			  ;;
		  esac
		done
		;;
	     *) break;;
	esac
      fi
    done
    printf "$mouse_off"
    return $error
}


-- 
	Chris F.A. Johnson
	=================================================================
	cfaj-uVmiyxGBW52XDw4h08c5KA at public.gmane.org        http://cfaj.freeshell.org/shell/scripts
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list