LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-15-2004, 01:07 AM   #1
rasika
LQ Newbie
 
Registered: Jan 2004
Posts: 3

Rep: Reputation: 0
how do i scan a character from keyboard without the user having to press enter


how do i scan a character from keyboard without the user having to press enter
 
Old 01-15-2004, 01:14 AM   #2
shellcode
Member
 
Registered: May 2003
Location: Beverly Hills
Distribution: Slackware, Gentoo
Posts: 350

Rep: Reputation: 32
1) either use getch() from the ncurses library (dont forget to add -lncurses to gcc)

2) use my function:

int getch()
{
int ch;
struct termios oldt;
struct termios newt;
tcgetattr(STDIN_FILENO, &oldt); /*store old settings*/
newt = oldt; /* copy old settings to new settings */
newt.c_lflag &= ~(ICANON | ECHO); /* change settings
tcsetattr(STDIN_FILENO, TCSANOW, &newt); /*apply the new settings immediatly */
ch = getchar(); /* standard getchar call */
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); /* reapply the old settings */
return ch; /* return received char */
}

im sure this has been asked before....
 
Old 07-21-2004, 09:26 AM   #3
sridharacharya
LQ Newbie
 
Registered: Jan 2003
Location: Hyderabad
Posts: 1

Rep: Reputation: 0
Scan character without pressing Enter key

Dear Mr. Shell code,

I want to scan characters from key board without pressing the enter key and then print the typed characters.

I am running the following program which uses your getch() program (mentioned in LQ thread ID:134966)

#include<stdio.h>
#include "GeTcH.h"
main()
{

while(1){
printf("You pressed %c\n", GeTcH());
}
}


It could not be compiled and it gave the following errors. Is any library file lacking.
###################################################################################
In file included from test.c:2:
GeTcH.h: In function `GeTcH':
GeTcH.h:9: storage size of `oldt' isn't known
GeTcH.h:10: storage size of `newt' isn't known
GeTcH.h:11: `STDIN_FILENO' undeclared (first use in this function)
GeTcH.h:11: (Each undeclared identifier is reported only once
GeTcH.h:11: for each function it appears in.)
GeTcH.h:13: `ICANON' undeclared (first use in this function)
GeTcH.h:13: `ECHO' undeclared (first use in this function)
GeTcH.h:14: `TCSANOW' undeclared (first use in this function)

##########################################################################

How can I solve this.

Last edited by sridharacharya; 07-21-2004 at 09:33 AM.
 
Old 07-21-2004, 11:22 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
Here's a complete (and fixed) version of shellcode's program.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>

static struct termios oldt;

void restore_terminal_settings(void)
{
    tcsetattr(0, TCSANOW, &oldt);  /* Apply saved settings */
}

void disable_waiting_for_enter(void)
{
    struct termios newt;

    /* Make terminal read 1 char at a time */
    tcgetattr(0, &oldt);  /* Save terminal settings */
    newt = oldt;  /* Init new settings */
    newt.c_lflag &= ~(ICANON | ECHO);  /* Change settings */
    tcsetattr(0, TCSANOW, &newt);  /* Apply settings */
    atexit(restore_terminal_settings); /* Make sure settings will be restored when program ends  */
}

int main()
{
    int ch;

    disable_waiting_for_enter();

    /* Key reading loop */
    while (1) {
        ch = getchar();
        if (ch == 'Q') return 0;  /* Press 'Q' to quit program */
        printf("You pressed %c\n", ch);
    }

    return 0;
}

Last edited by Hko; 07-21-2004 at 11:47 AM.
 
Old 07-22-2004, 04:22 AM   #5
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 419

Rep: Reputation: 35
This thread deals with the same situation:
http://www.linuxquestions.org/questi...ht=cooked+mode

Last edited by jinksys; 07-22-2004 at 04:23 AM.
 
Old 07-23-2004, 06:45 AM   #6
tuxfood
Member
 
Registered: Aug 2003
Location: kerala , India
Distribution: RH9 , FC1 ,
Posts: 141

Rep: Reputation: 15
hi..

was just going thru the forum ven i saw this article..

but frankly i can't understand a thing in the above code given by Hko ..

Mr.Hko plz could u explain the code to this newbie..?

thnks

tuxfood
 
Old 02-11-2010, 09:54 PM   #7
Mogli
Member
 
Registered: Jun 2008
Posts: 41

Rep: Reputation: 15
Thanks Hko

Thank you very much...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
getline, why press enter twice?? blizunt7 Programming 3 09-06-2005 11:34 PM
"Disk boot failure. Enter system disk and press enter" Fear58 Linux - Hardware 3 06-25-2004 06:17 PM
Cant press enter for install B McHack Mandriva 4 01-22-2004 09:58 PM
Random windows open when I press enter in KDE pointdexter Linux - Software 0 11-12-2003 03:05 PM
press 'I' to enter interactive startup? xiaoxiao Linux - Software 3 04-06-2001 08:39 AM

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

All times are GMT -5. The time now is 09:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration