LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2005, 09:01 AM   #1
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Rep: Reputation: 30
detecting KEY_BACKSPACE with ncurses


Anyone had any success detecting the back space or delete characters in their ncurses applications.

I can detect the arrows and page down kets etc ok...
advice on any obvoius blunders appreciated,been coding for hours now.. getting jaded.:0(

i get ^? on the screen when i press backspace.


Code:
       .........

       switch(c){
        case  KEY_DL:
      	printw("delete a characer "); 
	refresh();
        return 0;
      
         
         case  BACK_SPACE:
      	printw("delete a characer "); 
	refresh();
        return 0;
 
         .............
p.s i even tried ...

case '\b':
bl*&dy do something;
 
Old 07-31-2005, 10:58 AM   #2
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
its KEY_DC..

wot a dougnut.

still baffled with backspace though.
 
Old 07-31-2005, 12:10 PM   #3
dtcs
LQ Newbie
 
Registered: Jul 2005
Distribution: Slackware, Debian
Posts: 28

Rep: Reputation: 15
hi slzckboy,

To get the character value you first need to find the key code of that char. That depands on what platform you are using, key codes will vary from platform to platform.
here is some code to detect the different key codes:

getkey.c
Code:
#include <stdio.h>
#include "dtio.h"
int main(void){
  int key;
  char keystr[10];
  dt_init();
  dt_cursor(1,5);
  dt_puts("Hit a key to get the key code.  Q to exit");
  key=dt_getchar();
  while(key!='Q' && key!='q'){
    dt_cursor(dt_rows()/2,dt_cols()/2-2);  
    sprintf(keystr,"%4d",key);
    dt_puts(keystr);
    key=dt_getchar();
  }
  dt_end();
}
dtio.c
Code:
#include "dtio.h"
#include <string.h>
#include <stdlib.h>

#if PLATFORM == AIX  /*THIS WORKS ON LINUX TOO*/
  #include <ncurses.h>
#else
  #include <conio.h> /*FOR BORLAND*/ 
#endif

void dt_init(void){
#if PLATFORM == AIX
  initscr();
  noecho();
  cbreak();
  keypad(stdscr, 1);
#endif
}
void dt_end(void){
#if PLATFORM == AIX
  refresh();
  endwin();
#else
  clrscr();
#endif
}
int dt_rows(void){
#if PLATFORM == AIX
  return LINES;
#else
  struct text_info x;
  gettextinfo(&x);
  return x.screenheight;
#endif
}
int dt_cols(void){
#if PLATFORM == AIX
  return COLS;
#else
  struct text_info x;
  gettextinfo(&x);
  return x.screenwidth;
#endif
}
void dt_clear(void){
#if PLATFORM == AIX
  erase();
#else
  clrscr();
#endif
}
void dt_flush(void){
#if PLATFORM == AIX
  refresh();
#endif
}
void dt_cursor(int row,int col){
#if PLATFORM == AIX
  move(row,col);
#else
  gotoxy(col + 1, row + 1);
#endif

}
int dt_getchar(void){
#if PLATFORM == AIX
  refresh();
  return getch();
#else
  int key;
  key =getch();
  return key==0? 1000+getch() :key;
#endif

}
void dt_putchar(int c){
#if PLATFORM == AIX
  addch(c);
#else
  putch(c);
#endif
}
void dt_puts(const char* s){
#if PLATFORM == AIX
  addstr(s);
#else
  cputs(s);
#endif
}
void dio_draw(char *str, int row, int col, int length){
}
int dio_edit(char* str, int slen, int row, int col, 
        int flen, int* start, int* offset, int* insert ){


}
dtio.h - specify here what platform you want and populate the key macro with the key codes you found
Now you can use this header file in your real application and appropriate keys should be triggered.
Code:
#define AIX 1
#define BORLAND 2
#define PLATFORM AIX

#if PLATFORM == AIX
  #define LEFT   260 /*add in key code value*/
  #define RIGHT  261
  #define HOME 262
  #define END 360
  #define INS 331
  #define DEL 330
  #define BACK 263
  #define PGUP 339
  #define UP 259
  #define DOWN 258
  #define PGDOWN 338
  #define ENTER   10
  #define TAB 9
  #define ESC 27
#else 
  #define LEFT 
  #define RIGHT
  #define HOME
  #define END
  #define INS
  #define DEL
  #define BACK
  #define PGUP
  #define UP
  #define DOWN
  #define PGDOWN
  #define ENTER
  #define TAB
  #define ESC
#endif
void dt_init(void);
void dt_end(void);
int dt_rows(void);
int dt_cols(void);
void dt_clear(void);
void dt_flush(void);
void dt_cursor(int row,int col);
int dt_getchar(void);
void dt_putchar(int c);
void dt_puts(const char* s);
void dio_draw(char *str, int row, int col, int length);
int dio_edit(char* str, int slen, int row, int col, int flen, 
                             int* start, int* offset, int* insert );
Your real app what you have
Code:
#include "dtio.h"
int keycode=dt_getchar();
switch(keycode){
case LEFT:
case RIGHT:
case HOME:
...
}
I compiled this on linux and therefore these keycodes you see are for that platform you can double check them
cc dtio.c getkey.c -lncurses
 
Old 07-31-2005, 12:34 PM   #4
slzckboy
Member
 
Registered: May 2005
Location: uk - Reading
Distribution: slackware 14.2 kernel 4.19.43
Posts: 462

Original Poster
Rep: Reputation: 30
thnks for your efforts.

in the ncurses src files there is a similar application which basically is designed to show the basic capabilities of nucrses.

It has an app prints out the keycode constant representation and its interger equivalent value of any keys pressed.

for back space key in printed out " key pressed: 0631 ^?(ASCII control character)"

I will try your code and see what i get.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
NCurses Mohsen Programming 7 10-07-2006 10:53 AM
ncurses phoenix7 Linux - Software 6 08-30-2005 08:44 AM
Ncurses help MatSzor Programming 2 05-13-2004 09:03 AM
NCurses 4? gsibble Linux - Software 0 02-18-2004 02:31 AM
ncurses-5.2-28 conflicts with file from package ncurses-5.2-12 tubby Linux - Software 4 06-16-2002 12:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration