Solved

capturing the PageDown keypress ...

Posted on 1999-01-10
3
373 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now