Fedora Linux Support Community & Resources Center
  #1  
Old 15th January 2011, 10:30 AM
drdigital Offline
Registered User
 
Join Date: Jan 2011
Posts: 3
linuxfedorafirefox
Can not read extended keys {UP DOWN, ...} in C

Hello Evey one,
I'm a fedora beginner and I want to make a program that read extended keys (UP DOWN LEFT RIGHT BUTTONS) in C :
I installed gcc and I can now compile .c applications.
First I had a problem with getch and I read some forums and installed some stuff I can now include curses.h

If I ran the following app as follow :
Code:
gcc testKeys.c -o test -lncurses
I get an immediate output 81 and -1 without pressing any keys

and if i ran it without the -lncurses option I get the following errors
In function `main':
testKeys.c.text+0xa): undefined reference to `stdscr'
testKeys.c.text+0x12): undefined reference to `wgetch'
testKeys.c.text+0x31): undefined reference to `stdscr'
testKeys.c.text+0x39): undefined reference to `wgetch'
collect2: ld returned 1 exit status

Code:
#include<stdio.h>
#include<curses.h>
int main()
{
char a,gScan;
a = getch();
printf("%d \n",gScan);

gScan = getch();
printf("%d \n",gScan);
switch(gScan)
{
case 72: printf("up \n",gScan);
break;
case 80: printf("down \n",gScan);
break;
case 75: printf("left \n",gScan);
break;
case 77: printf("right \n",gScan);
break;
}

return 0;
}
Can any one help me ????
Reply With Quote
  #2  
Old 21st January 2011, 10:33 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: Can not read extended keys {UP DOWN, ...} in C

This link explains why it's not working.

Code:
#include <curses.h>

int main(int argc, char *argv[]) {

    int c = 0;

    initscr();
    timeout(5000);

    while((c = getch()) != EOF)
        printf("%d\n", c);

    endwin();
    return 0;
}
By running the above program (which accepts one keystroke and displays its value; stop entering keystrokes for 5 seconds to end), you'll notice that pressing the arrow keys results in the keyboard sending more than one value. For example, the up arrow key sends ESC+[+A (27+91+65), the down arrow ESC+[+B (27+91+66), etc.

Last edited by giulix; 21st January 2011 at 11:13 AM.
Reply With Quote
  #3  
Old 21st January 2011, 11:42 AM
drdigital Offline
Registered User
 
Join Date: Jan 2011
Posts: 3
linuxfedorafirefox
Re: Can not read extended keys {UP DOWN, ...} in C

Thanks so much for your replay. and Yes, it worked. But what I can understand from what I just received ? I know that extended keys have 2 ascii chars , why getting three outputs and why the spaces before second and third line ?

mmmm, should I test the last output only for A B C D representing the chars ? and how can I do this ???

Last edited by drdigital; 21st January 2011 at 11:46 AM.
Reply With Quote
  #4  
Old 21st January 2011, 12:54 PM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: Can not read extended keys {UP DOWN, ...} in C

Just ignore those spaces, as they are a consequence of the character you're trying to print on the screen not being printable. Read wikipedia about control characters for an introduction on this topic. As for the actual explanation for the three-character sequence assigned to arrow keys on Unix terminals, I don't know (I am not THAT old!!! ).
What you can do is, after you've identified the first character as the Esc control character (ASCII decimal 27), getch() another character; if it's the [ (ASCII decimal 91) getch() one more and enter a switch to tests all the possible values associated with ^[ (Esc[) as in:
Code:
#include <curses.h>

int main(int argc, char *argv[]) {

    int c = 0;

    initscr();
    timeout(5000);

    while((c = getch()) != EOF) {
        printf("%d\n", c);
        if(c == 27) {
            c = getch();
            if(c == 91) {
                c = getch();
                switch(c) {
                    case 65:
                        printf("Up-arrow encountered\n");
                        break;
                    case 66:
                        printf("Down-arrow encountered\n");
                        break;
                    ...
                    default:
                        printf("Unexpected character %d encountered\n", c);
                        break;
                }
            }
        }
    }

    endwin();
    return 0;
}
Press Return after inserting the desired arrow key, as ncurses expects a newline to begin interpret the sequence

Last edited by giulix; 21st January 2011 at 01:10 PM.
Reply With Quote
  #5  
Old 21st January 2011, 09:17 PM
drdigital Offline
Registered User
 
Join Date: Jan 2011
Posts: 3
solarisfirefox
Re: Can not read extended keys {UP DOWN, ...} in C

Thanks so much for your efforts, I appreciate your help so much
Reply With Quote
Reply

Tags
extended, keys, read

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Finally some explaination as to why we can't find keys, and they can't read maps. notageek Wibble 1 24th February 2009 01:29 PM


Current GMT-time: 09:38 (Wednesday, 23-07-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat