ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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();
}
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");
}
}
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.
> 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.