need help with the use of arrow keys

Reply

Join Date: May 2005
Posts: 2
Reputation: kramer147 is an unknown quantity at this point 
Solved Threads: 0
kramer147 kramer147 is offline Offline
Newbie Poster

need help with the use of arrow keys

 
0
  #1
May 13th, 2005
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!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: need help with the use of arrow keys

 
0
  #2
May 13th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 2
Reputation: kramer147 is an unknown quantity at this point 
Solved Threads: 0
kramer147 kramer147 is offline Offline
Newbie Poster

Re: need help with the use of arrow keys

 
0
  #3
May 13th, 2005
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.......
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: need help with the use of arrow keys

 
0
  #4
May 13th, 2005
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".
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 0
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: need help with the use of arrow keys

 
0
  #5
May 13th, 2005
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]     }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 32
Reputation: sinrtb is an unknown quantity at this point 
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: need help with the use of arrow keys

 
0
  #6
May 15th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: need help with the use of arrow keys

 
0
  #7
May 15th, 2005
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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 32
Reputation: sinrtb is an unknown quantity at this point 
Solved Threads: 0
sinrtb sinrtb is offline Offline
Light Poster

Re: need help with the use of arrow keys

 
0
  #8
May 15th, 2005
Worked great thanks soo much
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: need help with the use of arrow keys

 
0
  #9
May 15th, 2005
Anytime. Glad I could help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC