Solved

capturing the PageDown keypress ...

Posted on 1999-01-10
3
379 Views
Last Modified: 2008-02-26
hi,
  can anyone post me a snippet of the code that captures the PageDn keypress in a C program under unix environment...
I can capture other usual keys but I am not to sure on how to capture 'PageDn'.

Thanks
0
Comment
Question by:Haho
3 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255748
Use the same code you use to capture other keys, and if the scan code for the keypress is 34, then the Page Down key has been pressed.
0
 
LVL 3

Accepted Solution

by:
mliberi earned 50 total points
ID: 1255749
I'm posting two C test program allowing to capture key pressed.
The first one uses 'curses' standard library.
The second is 'curses' independent, but the codes are terminal
dependent.

#include <stdlib.h>
#include <curses.h>

void gotoxy(int X, int Y)
{
  move(Y,X);
}

int keypressed()
/* return true if there is a keystroke ready to be read */
{
  int Z;
  nodelay(stdscr, TRUE);
  Z = getch();
  nodelay(stdscr, FALSE);
  if (Z==ERR)
    return FALSE;
  ungetch(Z);
  return TRUE;
}

int rotella[4]={'-','/','|','\\'};

main()
{
  int Z,i;
  char alfa[20];

  initscr();
  box(stdscr,0,0);
  if (has_colors())
    start_color();
  curs_set(0); /* 0=invisible, 1=visible, 2=very visible */
  scrollok(stdscr, TRUE);
  keypad(stdscr,TRUE);
  notimeout(stdscr, FALSE);
  noecho();
  raw();
  init_pair(1,7,1);
  wattrset(stdscr,COLOR_PAIR(1));
  erase();
  gotoxy(20,1);
  addstr("Keyboard codes, (c) M.Liberi, 1995");
  gotoxy(1,3);
  addstr("Press ^C to exit\n");
  refresh();
  while (Z != 3) /* F10 */
  {
    int i=0;
    while (! keypressed())
    {
      gotoxy(1,5);
      addch(rotella[i]);
      i= (++i)&3;
      refresh();
    }
    Z = getch();
    printw("  %d   %s\n", Z, keyname(Z));
    refresh();
  }
  clear();
  endwin();
}


----------------------------------------------------------------

#include <stdio.h>
#include <errno.h>
#include <termio.h>

#define fd_stdin 0
#define then
#define true  1
#define false 0

int waitkey(unsigned char TimeOut)
/* read a single char, without echo, from stdin with a timeout         */
/* if nothing has arrived within Timeout tenths of a seconds return -1 */
{
  int rc;
  struct termios old_termdef, new_termdef;

  tcgetattr(fd_stdin,&old_termdef);
  new_termdef = old_termdef;
  new_termdef.c_iflag &=  IGNCR;    /* ignore CR OFF            */
  new_termdef.c_lflag &= ~ISIG;     /* signal generation OFF    */
  new_termdef.c_lflag &= ~ICANON;   /* Non canonical input      */
  new_termdef.c_lflag &= ~ECHO;     /* No echo                  */
  new_termdef.c_cc[VMIN] = 0;       /* read zero or one char    */
  new_termdef.c_cc[VTIME]= TimeOut; /* TIMEOUT in 0.1 sec units */
  tcsetattr(fd_stdin,TCSANOW,&new_termdef);
  rc = getchar();
  tcsetattr(fd_stdin,TCSANOW,&old_termdef);
  return rc;                        /* -1 if timeout reached */
}

int KeyPressed()
/* true if a character is present in input buffer */
{
  int rc=waitkey(0);
  if (rc>=0)
  {
    ungetc(rc,stdin);
    return true;
  }
  return false;
}

int inkey()
/* read a single character, without echo, from stdin */
{
  int rc;
  struct termios old_termdef, new_termdef;

  tcgetattr(fd_stdin,&old_termdef);
  new_termdef = old_termdef;
  new_termdef.c_iflag &=  IGNCR;    /* ignore CR OFF            */
  new_termdef.c_lflag &= ~ISIG;     /* signal generation OFF    */
  new_termdef.c_lflag &= ~ICANON;   /* Non canonical input      */
  new_termdef.c_lflag &= ~ECHO;     /* No echo                  */
  new_termdef.c_cc[VMIN] = 1;       /* read only one char       */
  new_termdef.c_cc[VTIME]= 0;       /* forever                  */
  tcsetattr(fd_stdin,TCSANOW,&new_termdef);
  rc = getchar();
  tcsetattr(fd_stdin,TCSANOW,&old_termdef);
  return rc;
}

int main()
{
  int c,i;

  if (!isatty(fd_stdin))
  {
    fprintf(stderr,"stdin is not a tty\n");
    return 1;
  }
  fprintf(stderr,"keybd, M.Liberi, 1996\n");
  fprintf(stderr,"Press ^C to exit\n");
  while ((c=inkey()) != 3)
  {
    if (isprint(c))
      then
        printf("%c %d", c, c);
      else
        printf("  %d", c);
    while (KeyPressed())
    {
      c=inkey();
      printf(" %d", c);
    }
    putchar('\n');
  }
  return 0;
}

0
 
LVL 1

Author Comment

by:Haho
ID: 1255750
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question