943,521 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7348
  • C RSS
May 13th, 2005
0

need help with the use of arrow keys

Expand Post »
here is the sequence i'm using for my arrow keys

  1. #include<stdio.h>
  2.  
  3. unsigned char keyin
  4.  
  5. main()
  6. {
  7.  
  8. if(square==1)
  9. {//start square
  10. putimage(285,440,ste,COPY_PUT);
  11. while((keyin=getch())!='\r')
  12. {//up arrow key
  13. if(keyin==72)
  14. {//hit up arrow
  15. keyin=getch();
  16. square=2;
  17. }//hit up arrow
  18. }//up arrow key
  19. }//start square
  20. do
  21. {//game from start square on
  22. if(square==2)
  23. {//on square 2
  24. putimage(285,400,ste,COPY_PUT);
  25. }//on square 2
  26. square=17;
  27. if(square==17)
  28. {//on the final square, you win
  29. gamer=1;
  30. win=1;
  31. }//on the final square, you win
  32. }//game from start square on
  33. while(gamer!=1);
  34. cout<<"the while statement has closed.\n"; */
  35. getch();
  36.  
  37. }
<< moderator edit: added [code][/code] tags >>





the problem i'm having is that if you hit the arrow key, nothing happens.
if you then hit the enter key twice, what i want to happen, will happen.
if you try to just hit the enter key without first hitting the arrow key it will do nothing and just exit from all the if, do, and while statements.

please please help my game is due in like 8 hours!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kramer147 is offline Offline
2 posts
since May 2005
May 13th, 2005
0

Re: need help with the use of arrow keys

Hello... your code... is well.... um, yeah... here is a very basic example of how to get input from the keyboard in C (in the console) that I whipped up just for you

Generated: Fri May 13 01:22:44 2005

[001]     #include <curses.h>
[002]
[003]     int main()
[004]     {
[005]             initscr();
[006]             /* Input characters one by one */
[007]             cbreak();
[008]             [i]/* Do not show us what we type */[i]
[009]             noecho();
[010]
[011]             [i]/* Keyboard mapping, stdscr is the typic 24x80 console */[i]
[012]             keypad(stdscr, TRUE);
[013]
[014]             printw("subtronic's little curses demo\n");
[015]
[016]             /* Best way to represent an infinite loop :-) */
[017]             for (;;) {
[018]                     switch (getch()) {
[019]                     case KEY_UP:
[020]                             printw("UP\n");
[021]                             break;
[022]                     case KEY_DOWN:
[023]                             printw("DOWN\n");
[024]                             break;
[025]                     case KEY_LEFT:
[026]                             printw("LEFT\n");
[027]                             break;
[028]                     case KEY_RIGHT:
[029]                             printw("RIGHT\n");
[030]                             break;
[031]                     }
[032]             }
[033]
[034]             endwin();
[035]
[036]             return 0;
[037]     }

Compile with -lcurses
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 13th, 2005
0

Re: need help with the use of arrow keys

not sure what code that is, i'm using turbo c++ 3.0, does that make a difference? and as far as the code goes i just put in exactly what my computer science teacher gave me so.......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kramer147 is offline Offline
2 posts
since May 2005
May 13th, 2005
0

Re: need help with the use of arrow keys

Quote originally posted by kramer147 ...
not sure what code that is, i'm using turbo c++ 3.0, does that make a difference? and as far as the code goes i just put in exactly what my computer science teacher gave me so.......
Well why didn't you say any of this in your first post? As far as I know, "stdio.h" is part of the standard C library. I had to infer from your ugly ass code what you were trying to do and in what language. There's an old addage you should internalize, "HELP ME HELP YOU".
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 13th, 2005
0

Re: need help with the use of arrow keys

Quote originally posted by subtronic ...
Well why didn't you say any of this in your first post? As far as I know, "stdio.h" is part of the standard C library. I had to infer from your ugly ass code what you were trying to do and in what language. There's an old addage you should internalize, "HELP ME HELP YOU".
Oh and I fixed my little C program to correctly add in the bbCode for preprocessor directives and comments :-)... I really detest DaniWeb's "You have thirty minutes to edit your post" rule :/

Exemplify v0.1 (by subtronic)
Generated: Fri May 13 09:40:57 2005
 
 
[001]     #include <curses.h>
[002]
[003]     // Let us test all comments :-)
[004]
[005]     int main()
[006]     {
[007]             initscr();
[008]             /* Input characters one by one */
[009]             cbreak();
[010]             /* Do not show us what we type */
[011]             noecho();
[012]
[013]             /* Keyboard mapping, stdscr is 24x80 console */
[014]             keypad(stdscr, TRUE);
[015]
[016]             printw("subtronic's little curses demo\n");
[017]
[018]             /* Best way to represent an infinite loop :-) */
[019]             for (;;) {
[020]                     switch (getch()) {
[021]                     case KEY_UP:
[022]                             printw("UP\n");
[023]                             break;
[024]                     case KEY_DOWN:
[025]                             printw("DOWN\n");
[026]                             break;
[027]                     case KEY_LEFT:
[028]                             printw("LEFT\n");
[029]                             break;
[030]                     case KEY_RIGHT:
[031]                             printw("RIGHT\n");
[032]                             break;
[033]                     }
[034]             }
[035]
[036]             endwin();
[037]
[038]             return 0;
[039]     }
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 15th, 2005
0

Re: need help with the use of arrow keys

Im using gcc and mingw and im not able to use curses ... is it non included library? I did try -lcurses
Last edited by sinrtb; May 15th, 2005 at 6:42 am. Reason: clarity
Reputation Points: 10
Solved Threads: 0
Light Poster
sinrtb is offline Offline
32 posts
since Apr 2005
May 15th, 2005
0

Re: need help with the use of arrow keys

If I recall correctly, mingw doesn't support curses "out of the box". You can download and install PDcurses from http://pdcurses.sourceforge.net/. Or if you still want to use getch outside of the curses library, this might work for you:
  1. #include <conio.h> /* Or whatever header getch may be defined in */
  2. #include <ctype.h>
  3. #include <stdio.h>
  4.  
  5. enum {
  6. UP = 72,
  7. LT = 75,
  8. RT = 77,
  9. DN = 80,
  10. };
  11.  
  12. int keycode(int c)
  13. {
  14. return (c == 0 || c == 224) ? getch() : c;
  15. }
  16.  
  17. int main(void)
  18. {
  19. int c;
  20.  
  21. while ((c = getch()) != '\r') {
  22. switch (keycode(c)) {
  23. case UP: puts("Up"); break;
  24. case DN: puts("Down"); break;
  25. case LT: puts("Left"); break;
  26. case RT: puts("Right"); break;
  27. default:
  28. if (isprint(c))
  29. printf("%c\n", c);
  30. else
  31. printf("%d\n", c);
  32. }
  33. }
  34.  
  35. return 0;
  36. }
If your compiler doesn't support getch, you have to write it yourself. Here's a Unix version using termios:
  1. #include <stdio.h>
  2. #include <termios.h>
  3. #include <unistd.h>
  4.  
  5. int getch(void)
  6. {
  7. int c;
  8. struct termios old, new;
  9.  
  10. tcgetattr(STDIN_FILENO, &old);
  11. new = old;
  12. new.c_lflag &= ~(ICANON | ECHO);
  13. tcsetattr(STDIN_FILENO, TCSANOW, &new);
  14. c = getchar();
  15. tcsetattr(STDIN_FILENO, TCSANOW, &old);
  16.  
  17. return c;
  18. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 15th, 2005
0

Re: need help with the use of arrow keys

Worked great thanks soo much
Reputation Points: 10
Solved Threads: 0
Light Poster
sinrtb is offline Offline
32 posts
since Apr 2005
May 15th, 2005
0

Re: need help with the use of arrow keys

Anytime. Glad I could help.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Use sys calls fork(),pipe(),open().....
Next Thread in C Forum Timeline: segy file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC