Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Bindkey + PageUp/PageDown keys



On Dec 12,  1:38am, Geoff Wing wrote:
} Subject: Re: Bindkey + PageUp/PageDown keys
}
} Oliver Grimm <grimm@xxxxxxxxxxxxxxxxxxxxx> typed:
} :In xterm, they are called 'Prior', 'Next', etc. but how do I use
} :this with bindkey ?
} 
} Your terminal emulator (xterm) will translate the Prior (PageUp)
} and Next (PageDown) keys into a character sequence - usually an
} escape sequence (i.e. the first character in the sequence is an
} escape character).

Here's a little something I cobbled together.  I've called it "zkbd".
It works better as a shell script than as a function, so I suppose if
it goes into the distribution it should go under the top-level Misc/
directory rather than in Functions/Misc/.

Run "zkbd" and it'll ask you to press a bunch of function and movement
keys.  It records those sequences in ~/.zkbd/$TERM in the associative
array $key (so this requires 3.1.9).

I realize that naming the file after $TERM is not quite accurate because
e.g. one can use an xterm with with just about any kind of keyboard.  On
the other hand, the same keyboard can generate different sequences in
different terminal emulators.  If someone has a better idea for the file
naming convention, I'd be happy to hear it.

Further, if somebody has one of those Sun keyboards with the 24 extra
function keys in left and right banks, and wants to fix this up to work
with that as well -- e.g., check that $DISPLAY or $TTY refers to the
console, that the hardware is Sun, et cetera, and prompt for the extra
keys when so -- that'd be cool too.

---- 8< ---- cut here ---- 8< ----
#! /bin/zsh -f

emulate -RL zsh
local zkbd term key seq

zkbd=${ZDOTDIR:-$HOME}/.zkbd
[[ -d $zkbd ]] || mkdir $zkbd || return 1

print 'typeset -A key\n' > $zkbd/$TERM.tmp || return 1
trap "command rm -f $zkbd/$TERM.tmp" 0 1 2

read term"?Enter current terminal type: [$TERM] "
[[ -n $term ]] && TERM=$term

cat <<EOF

You will now be asked to press in turn each of the 12 function keys, plus
the 6 common keypad keys found on typical PC keyboards, plus the 4 arrow
keys.  If you do not press a key within 10 seconds, key reading will
abort.  If your keyboard does not have the requested key, press Space to
skip to the next key.

Do not type ahead!  Wait at least one second after pressing each key for
zsh to read the entire sequence and prompt for the next key.  If a key
sequence does not echo within 2 seconds after you press it, that key may
not be sending any sequence at all.  In this case zsh is not able to make
use of that key.  Press Space to skip to the next key.

EOF

getseq () {
    trap 'stty -raw' 0 1 2
    stty raw
    local k='' seq='' i
    for ((i=5; i>0; --i))
    do
	read -t -k 1 k && break
	sleep 1
    done
    [[ -n $k ]] || return 1
    [[ $k = $'\012' || $k = $'\015' || $k = ' ' ]] && return 0
    seq=$k
    while read -t -k 1 k
    do
       seq=$seq$k 
    done
    print -R ${(V)seq}
}

exec 3>/dev/tty

for key in F{1..12} Insert Delete Home End PageUp PageDown Up Left Down Right
do
    print -u3 -Rn "Press $key: "
    seq="$(getseq)" || return 1
    print "key{$key}=${(q)seq}"
    print -u3
done >> $zkbd/$TERM.tmp

command mv $zkbd/$TERM.tmp $zkbd/$TERM

cat <<EOF

Parameter assignments for the keys you typed have been written to the file:
$zkbd/$TERM

You may read this file into ${ZDOTDIR:-$HOME}/.zshrc or another startup
file with the "source" or "." commands, then reference the \$key parameter
in bindkey commands like this:

    bindkey "\$key{Right}" forward-char
    bindkey "\$key{Left}" backward-char

EOF
---- 8< ---- cut here ---- 8< ----

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   



Messages sorted by: Reverse Date, Date, Thread, Author