[comp.unix.questions] An ioctl question

bann@virgin.UUCP (Roger Bannister) (10/26/89)

	I modified this progam that I found in a book so that prints out
   the hex code of each key.  I noticed that the keypad was producing the
   same scan codes as the regular keys.  Is there a way to get the keypad
   to give different scan code like curses.

             Roger

---------------------Program Starts here ----------------------------------

#include <sys/ioctl.h>
#include <sys/termio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include <stdio.h>

#define CLRSCR printf("\033[2J")

struct termio OLD, NEW;
int Keyfile;

InitKeyboard()
{
        
        Keyfile = open("/dev/tty",O_RDONLY);
        if (Keyfile < 0)
    		{
		printf("Unable to open /dev/tty\n");
		exit(1);
		}
	ioctl(Keyfile, TCGETA, &OLD);
}

SetRead()
{
        NEW.c_lflag &= ~ICANON; 
        NEW.c_lflag &= ~ECHO; 
        NEW.c_cc[VMIN] = 1;
        NEW.c_cc[VTIME] = 0;
	ioctl(Keyfile, TCSETA, &NEW);
}

/******************* Start of Main Program ********************/
main()
{
	int Bytes;
	int Buffer[2];

	/*run_expire(); */

	InitKeyboard();
	do
		{
	        SetRead();
		Bytes = read( Keyfile, Buffer, 1 );
	        ioctl(Keyfile, TCSETA, &OLD);
		printf( "Read char %x\n", (short) Buffer[0] );
		}
	while ( Buffer[0] != 'Z' );

}

barmar@kulla (Barry Margolin) (10/28/89)

In article <2582@virgin.UUCP> bann@virgin.UUCP (Roger Bannister) writes:
>	I modified this progam that I found in a book so that prints out
>   the hex code of each key.  I noticed that the keypad was producing the
>   same scan codes as the regular keys.  Is there a way to get the keypad
>   to give different scan code like curses.

It depends on the type of terminal you're using.  Some terminals support an
"alternate keypad" mode, which causes keys on the keypad to send escape
sequences instead of the normal characters.  There's generally control
sequences that can be sent to the terminal to turn this mode on and off; it
may also be settable with the terminal's SET-UP mechanism, if it has one.

In the termcap file, the sequences that turn keypad mode on and off are
notated with "ks=" and "ke=", respectively.
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar