| | |
need help with the use of arrow keys
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 2
Reputation:
Solved Threads: 0
here is the sequence i'm using for my arrow keys
<< 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!!!
C Syntax (Toggle Plain Text)
#include<stdio.h> unsigned char keyin main() { if(square==1) {//start square putimage(285,440,ste,COPY_PUT); while((keyin=getch())!='\r') {//up arrow key if(keyin==72) {//hit up arrow keyin=getch(); square=2; }//hit up arrow }//up arrow key }//start square do {//game from start square on if(square==2) {//on square 2 putimage(285,400,ste,COPY_PUT); }//on square 2 square=17; if(square==17) {//on the final square, you win gamer=1; win=1; }//on the final square, you win }//game from start square on while(gamer!=1); cout<<"the while statement has closed.\n"; */ getch(); }
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!!!
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
Compile with -lcurses
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
•
•
•
•
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.......
•
•
•
•
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".
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] }
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:
If your compiler doesn't support getch, you have to write it yourself. Here's a Unix version using termios:
C Syntax (Toggle Plain Text)
#include <conio.h> /* Or whatever header getch may be defined in */ #include <ctype.h> #include <stdio.h> enum { UP = 72, LT = 75, RT = 77, DN = 80, }; int keycode(int c) { return (c == 0 || c == 224) ? getch() : c; } int main(void) { int c; while ((c = getch()) != '\r') { switch (keycode(c)) { case UP: puts("Up"); break; case DN: puts("Down"); break; case LT: puts("Left"); break; case RT: puts("Right"); break; default: if (isprint(c)) printf("%c\n", c); else printf("%d\n", c); } } return 0; }
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <termios.h> #include <unistd.h> int getch(void) { int c; struct termios old, new; tcgetattr(STDIN_FILENO, &old); new = old; new.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &new); c = getchar(); tcsetattr(STDIN_FILENO, TCSANOW, &old); return c; }
![]() |
Similar Threads
- How do you check if arrow keys are pressed? (C)
- SQL Plus command line: Arrow keys not giving previous commands back (Oracle)
- Find if user hit arrow keys (C++)
- moving around in an array with arrow keys (C++)
- special keys as inputs (Game Development)
Other Threads in the C Forum
- Previous Thread: UNIX: _vscprintf not compiling
- Next Thread: segy file
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch char cm copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fflush fgets file fork forloop framework frequency getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research scanf scripting segmentationfault sequential shape socket socketprograming stack standard string systemcall testing threads turboc unix user voidmain() wab windows.h





