LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 09-30-2015, 12:52 AM   #1
Joshu
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Rep: Reputation: Disabled
C Programming, Keyboard input, ncurses.


Hi there everyone, first time poster, very long time reader. I'm trying to understand how I can get user input from the keyboard. For example if the user presses the letter 'a' the object moves to the left and if they press 'f' they move the object to the right. The following elementary code just shows the word TARGET move across the screen from left to right and then back again and repeats. I've tried googling and have been through a few stackoverflow threads but still can't seem to find an answer. My thinking is that I somehow use getch() to somehow scan for a '97/65' for the letter 'a/A'? If anyone could give me a helping hand or point me in the right direction that'd be great. Thankyou, and I look forward to contributing to the LQ community in the future.

Code:
#include <stdio.h>
#include <unistd.h>
#include <ncurses.h>
#define DELAY 60000

int main(int argc, char *argv[])
{
    int y, x = 0;
    int max_y, max_x, next_x = 0;
    int direction_x = 1;
    
    initscr();
    noecho();
    curs_set(FALSE);
    
    while (1) {
        getmaxyx(stdscr, max_y, max_x);
        clear();
        mvprintw(y, x, "TARGET");
        refresh();
        usleep(DELAY);
        
        next_x = x + direction_x;
        
        if (next_x >= max_x || next_x < 0) {
            direction_x*= -1;
        } else {
            x += direction_x;
        }
    }
    endwin();
}
 
Old 09-30-2015, 02:54 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I couldn't find any getch in your code
 
Old 09-30-2015, 04:18 AM   #3
Joshu
LQ Newbie
 
Registered: Sep 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks for the reply NevemTeve. I've tried to get getch() working multiple times and failed. It may not be possible, i've been searching for hours and have found no solution.
Edit. I could do this, but it would be good to have a getAsciCode() or something similar. I can just do this i suppose.

Code:
#headers

int main()
{
        char c;
        printf("Enter a character ");
        c = getchar();

        if (c == 'b') {
                printf("You hit the letter 'b'\n");
                printf("Now run this code\n");
        }
        else {
                printf("You didn't hit 'b'\n");
        }
}

Last edited by Joshu; 09-30-2015 at 04:34 AM.
 
Old 09-30-2015, 04:33 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, actually calling 'getch' is an important part of using it.

Code:
    int ch;
...
    while ((ch = getch()) != ERR) {
	printw("Keycode %d\n", ch);
	if      (ch=='A') printw("Upper-case 'A'\n");
	else if (ch=='a') printw("Lower-case 'a'\n");
	clrtoeol();
	refresh();
    }

Last edited by NevemTeve; 10-04-2015 at 03:59 AM. Reason: The %percent bug haven't solved itself yet.
 
Old 09-30-2015, 07:08 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
I would use the getc() and getchar() functions, they conform to the standards. There seem to be too many exceptions and special considerations for using getch(). When I search I find a lot more references back to Microsoft's NCurses library. They did something for their purposes and not a general C language case. I say stick with the more universal functions and use NevemTeve's example.
 
1 members found this post helpful.
Old 09-30-2015, 09:12 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by rtmistler View Post
When I search I find a lot more references back to Microsoft's NCurses library.
Huh? GNU is Not Unix, but it's not Microsoft either.

http://www.gnu.org/software/ncurses/ncurses.html


@NevemTeve: your % got eaten.
 
1 members found this post helpful.
Old 09-30-2015, 09:18 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> I would use the getc() and getchar() functions, they conform to the standards.

Also termios, I have to add, because the OP wants to read in raw mode. Plus, if he wants to interpret ESC-sequences generated by terminal then he has to reinvent ncurses... So it might be easier to use ncurses.
 
Old 10-03-2015, 10:08 PM   #8
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Get rid of 'stdio.h', you do not need it here.
 
  


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
[SOLVED] C programming ncurses Input question errigour Programming 1 01-28-2013 04:05 PM
[SOLVED] Beginng C programming - how to input decimals from keyboard and calcuate as floats? keithostertag Programming 4 02-17-2012 11:11 AM
Repeated "input: AT Translated Set 2 keyboard as /class/input/input" messages AcerKev Mandriva 2 09-16-2007 08:35 AM
Programming with NCurses indienick Programming 7 07-11-2006 02:56 PM
my mouse input is takes as keyboard input in BASH e1000 Slackware 5 12-08-2003 03:00 PM

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

All times are GMT -5. The time now is 04:10 PM.

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