'286 curses keypad() mode

Ronald Florence ron at mlfarm.com
Fri May 24 23:29:04 AEST 1991


In comp.unix.xenix.sco, wardlaw at kaos.UUCP (Johnie Wardlaw) writes:

 > I am having a problem using the function key capabilities in curses on
 > SCO Xenix V/286 v2.3.2.

There are all sorts of solutions, and some are complex.  To read
multi-character keys reliably, you sometimes need a timer so that the
the key handler can distinguish between `ESC' followed by `[' followed
by `A', from the special key sequence `ESC[A'.  The following code is
a quick and dirty solution.  I wouldn't use it for an editor, but it
works fine for games.  The keycodes are from the terminfo version of
curses, but I use this code with termcap.

Call the function lookupkeys() after initscr().  Then read keys with
int_variable = getkey().


#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	keys.c
#	keys.h
# This archive created: Fri May 24 09:17:31 1991
# By:	Ronald Florence (Maple Lawn Farm, Stonington, CT)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'keys.c'" '(1587 characters)'
if test -f 'keys.c'
then
	echo shar: "will not over-write existing file 'keys.c'"
else
sed 's/^X//' << \SHAR_EOF > 'keys.c'
X/*
X *  keys.c -	gets arrow and function keys from termcap,
X *		returns terminfo codes
X *		changes quit key for use as arrow
X *
X * 		define NO_SYSV for versions of curses that do not look up
X *     		arrow & function key strings from termcap
X *
X *  copyright 1988 Ronald Florence
X *  changed VMIN & VTIME for wy99 @ 9600, ron at mlfarm (7/11/88)
X */
X
X#include <curses.h>
X#ifndef KEY_DOWN
X#include "keys.h"
X#endif
X
X#define NKEYS	16
X
Xchar	
X#ifdef NO_SYSV
X  *tcap_ids[] = {
X    "kd", "ku", "kl", "kr", "kh", "kb",
X    "k0", "k1", "k2", "k3", "k4", "k5", "k6", "k7", "k8", "k9", 0
X    },
X#endif
X  *fkeys[NKEYS];
X	
Xlookupkeys()
X{
X#ifdef NO_SYSV
X  static char	sbuf[256];
X  char	**key, tbuf[1024], *fill = sbuf, *tgetstr(); 
X  int	i = 0;
X
X  tgetent(tbuf, ttytype);
X  for (key = tcap_ids; *key; ++key) 
X    fkeys[i++] = tgetstr(*key, &fill);
X#else
X  fkeys[0] = KD;
X  fkeys[1] = KU;
X  fkeys[2] = KL;
X  fkeys[3] = KR;
X  fkeys[4] = KH;
X  fkeys[5] = KB;
X  fkeys[6] = K0;
X  fkeys[7] = K1;
X  fkeys[8] = K2;
X  fkeys[9] = K3;
X  fkeys[10] = K4;
X  fkeys[11] = K5;
X  fkeys[12] = K6;
X  fkeys[13] = K7;
X  fkeys[14] = K8;
X  fkeys[15] = K9;
X#endif
X  fixquit();
X}	
X
X
Xgetkey()
X{
X  char	cmd[6];
X  register	k;
X  
X  k = read(0, cmd, 6);
X  cmd[k] = '\0';
X  for (k = 0; k < NKEYS; k++)   
X    if (strcmp(cmd, fkeys[k]) == 0)
X      return (k + KEY_DOWN);
X  return ((int) *cmd);
X}
X
X
Xfixquit()
X{
X  struct termio	new;
X
X  ioctl(0, TCGETA, &new);
X  new.c_cc[VQUIT] = 0xff;	/* in case QUIT is an arrow */
X  new.c_cc[VTIME] = 1;		/* minimum timeout */
X  new.c_cc[VMIN] = 3;		/* or three characters */
X  ioctl(0, TCSETA, &new);
X}
SHAR_EOF
if test 1587 -ne "`wc -c < 'keys.c'`"
then
	echo shar: "error transmitting 'keys.c'" '(should have been 1587 characters)'
fi
fi
echo shar: "extracting 'keys.h'" '(345 characters)'
if test -f 'keys.h'
then
	echo shar: "will not over-write existing file 'keys.h'"
else
sed 's/^X//' << \SHAR_EOF > 'keys.h'
X/*
X *	keys.h
X *	copyright 1988 Ronald Florence
X *
X *	use with curses programs that need extended keyboard
X *	(if tcap.h does not include the defines)
X */
X
X#define KEY_DOWN	0402
X#define KEY_UP		0403
X#define KEY_LEFT	0404
X#define	KEY_RIGHT	0405
X#define KEY_HOME	0406
X#define KEY_BACKSPACE	0407
X#define KEY_F0		0410
X#define KEY_F(n)	(KEY_F0 + (n))
SHAR_EOF
if test 345 -ne "`wc -c < 'keys.h'`"
then
	echo shar: "error transmitting 'keys.h'" '(should have been 345 characters)'
fi
fi
exit 0
#	End of shell archive

--

				Ronald Florence
				ron at mlfarm.com



More information about the Comp.unix.xenix.sco mailing list