how to interrupt the process with the keyboard ?!

Programming, for all ages and all languages.
Post Reply
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

how to interrupt the process with the keyboard ?!

Post by mohammed »

what i want to do is
cls
her :
x++
printf x
see if the key board pressed go to there
if not goto her
there
what i tried to do is that
while(1)
{
char m=getch();
case '1':
.
.
case '2':
.
.
}
but it didn't work cause it stopped every time with getch i tried that too
char m=getch();
while(1)
{
case '1':
.
.
case '2':
.
.

}
but it asked me for the first time and sure it didn't asked me again because it was in the loop
help me plz
User avatar
XCHG
Member
Member
Posts: 416
Joined: Sat Nov 25, 2006 3:55 am
Location: Wisconsin
Contact:

Post by XCHG »

Have you used the "switch" keyword?

Code: Select all

int main(int argc, char* argv[])
{
  while (1){
    char MyChar = getch();
    switch (MyChar){
      case 'a':
        //~
        break;
      case 'b':
        //~
        break;
      // ...
    }
  }
  return(0);
}
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Messy... let's see if I can make sense of that.

You have to realize that input is buffered.

When you start a program, the first time some character(s) are requested from stdin, the program will find that there is no input pending.

When you start typing characters, these do not get passed to the program immediately: What you type is still under control of the shell, which is why you can backspace etc.

Only when you press <Enter> does the input get processed by the shell process, and only then it becomes available as stdin to your program.

Let's say your program takes one character from your input, goes through a loop and asks for input again. If you typed anything else but a single <Enter>, there will be more than one character pending at stdin, i.e. your program does not promt you again but loops right again with the second character you typed, then the third, and so on until it has processed the <Enter> and stdin is empty again.

So if you want to wait for a keystroke, drain stdin until you hit a newline before doing the getch().
Every good solution is obvious once you've found it.
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

thank you guys
it requires Enter if i used getchar but i am using getch so it will go to next step without waiting for Enter
sure i used getch() and sure i used switch case
what i want to do is like the snake that we are playing in our mobiles or in the computer you press (left button )then the sanke go left until you press down then the snake go down until you press anything
what i tried is that

Code: Select all

#include <graphics.h>
#include <conio.h>
main()
{
int snake[3];
int x=15,y=15;
char m;
int driver=DETECT;
int mode=0;
initgraph(&driver,&mode,"");
putpixel(x,y,WHITE);
m=getch(); //the getch outside the loop so it will ask me one time and go to the loop 
while(1)
{
//if i put the getch() here it will ask me  every time :(
switch (m)
{
case 'm':
for(x=15;x<=800;x++)
{
if(m=getch()=='n')
exit(1);
else
{
putpixel(x,y,BLACK);
x++;
delay(800);
putpixel(x,y,WHITE);
}
}
break;
default:
exit(1);
break;
}
}
}
what is wrong here is that the keyboard wait for yout hit and then go for the loop wihtout asking again if i put the getch() inside the loop it will stop every time to ask me and i don't want both of them
i want to make the process and then check if the keyboard did't hited i will complete ....
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

you need something like kbhit() under windows. Which returns true if a key was hit/pressed, otherwise it returns false. As you are in control of your os you could implement kbhit as

Code: Select all

int kbhit(void) {
    //- return the bit 1 value of the keyboard controller status register.
    //- 0 = false, 1 = true.
    return(inb(0x0064) & 0x01);
}
then you use it like:

Code: Select all

for(;;) { //- better then while(1).
    if(!kbhit()) {
        //- no key pressed. do idle stuff.
    }
    else {
        //- key was pressed.
        switch(getch()){
        case 'm': //- do stuff for m
            break;
        case 'n': //- do stuff for n
            break;
        default:  //- disregard key.
        }
    }
}
hope this helps.

regards
Author of COBOS
mohammed
Member
Member
Posts: 93
Joined: Mon Jan 30, 2006 12:00 am
Location: Mansoura, Egypt

Post by mohammed »

kbhit() is a function located in conio.h thank you alot :)
your code is better than mine :wink:
Post Reply