Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyboard Input

Status
Not open for further replies.

FinnMan

Technical User
Feb 20, 2001
75
US

How would I go about inputing characters from a script such as CRTL + SHIFT + N etc... ? Any link refs would be wonderful too.

I've looked into expect but I'm not really concerned with the terminal response at this point so I don't think that's what I need.

Thx,
FM
 
man printf
man ascii

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I knew it had to be something simple. thx!

./FM
 
Ok..I've been at this for hours now and have googled myself to death. For the life of me I can't figure out how to pass a SHIFT+ALT+N during a script. I'm unable to locate any ascii/hex values for such a combination.

Any insight would be very appreciated.

./FM
 
After some digging I've discovered that DCOP is the answer to the problem i'm dealing with....
 
Hi FinnMann,

The keys ALT, arrows, home, end, F1-F12, etc. are a mix of keys, and CTRL-<letter> is a ASCII code 1 to &quot;don't remember below to 32&quot; for example:

if you press A, the ASCII code will be 65
if you pres <ctrl>-A, the ascii code should be 1.
if you press UP-Arrow, ASCII code will be 27 (<ESC>) plus 65 (A). You see?

do you have gnu C or cc (Unix)? Unix (HP, SUN) has the getch() function, GNU C does not have this one. try this program in c++ (g++ in Linux) it comes from linux:

Code:
//readch.cpp
#include <stdio.h>
#include <termios.h>
//#include <term.h>
//#include <curses.h>
#include <unistd.h>
/*-------------------------------------------------------*/
static struct termios initial_settings, new_settings;
static int peek_character = -1;
/*-------------------------------------------------------*/
void init_keyboard()
{
  tcgetattr( 0, &initial_settings );
  new_settings = initial_settings;
  new_settings.c_lflag &= ~ICANON;
  new_settings.c_lflag &= ~ECHO;
  new_settings.c_lflag &= ~ISIG;
  new_settings.c_cc[VMIN] = 1;
  new_settings.c_cc[VTIME] = 0;
  tcsetattr( 0, TCSANOW, &new_settings );
}

void close_keyboard()
{
  tcsetattr( 0, TCSANOW, &initial_settings );
}

int kbhit()
{
  char ch;
  int nread;
 
  if( peek_character != -1 )
    return( 1 );
  new_settings.c_cc[VMIN] = 0;
  tcsetattr( 0, TCSANOW, &new_settings );
  nread = read( 0, &ch, 1 );  
  new_settings.c_cc[VMIN] = 1;  
  tcsetattr( 0, TCSANOW, &new_settings );
  if( nread == 1 ) {
    peek_character = ch;
    return( 1 );
  }
  return( 0 );
}

int readch()
{
  char ch;
  init_keyboard();
  if( peek_character != -1 ) {
    ch = peek_character;
    peek_character = -1;
    return( ch );
  }
  read( 0, &ch, 1 );
  close_keyboard();
  return( ch );
}

int main()
{
    char c,ext;
//	init_keyboard();

 	c=readch();  //       <---- getch()
	if(c!=27)    //   <-- a Fx, Arrow or insert,home,end,etc.
		printf(&quot;Character: %c - ASCII: %d\n\n&quot;,c,c);
	else {
		ext=readch();
		printf(&quot;Characters: %d - %d\n\n&quot;,c,ext);
	}
	
// 	close_keyboard();
}
// END of program

compile with:
g++ -c readch readch.cpp

this program will let you press a key with no enter, if this key is not an alphanumeric key, the first letter is a <ESC> character, so it is a function key.

Pls, note that some keys, like Home, F5 to F12, has 3 keyx combinations, example: F5 is <ESC>+1+18 (if I remember)

take a look to this page:

Hope this help you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top