[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

capturing the PageDown keypress ...

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
Haho
Asked:
Haho
1 Solution
 
scrapdogCommented:
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
 
mliberiCommented:
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
 
HahoAuthor Commented:
thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now