#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Catching singals from a keyboard


    Hi,
    I'm struggling heavily with theoretically simple program which supposed to detect pressed key on keyboard. Let me give you an example:

    1) Program should display some message like: 'Hello world'.

    2) If user press X key program should restart, clear screen and loop itself.

    3) If user press ESC key program should shut down.

    Ok, here's my attempt. Don't know how to set 2 conditions in do while loop :(
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
            
        signed char key;
        fflush(stdin);
    
        printf("Hello world\n");
            
        if(getchar()=='x')
        {
            system("cls");
            printf("Hello world\n");
        }
        else if(key!= 27)
        {
            while( kbhit() )
            {
                key= getch();
            }
        }
        return 0;
    }

    How can I got this 2 conditions working in the same program ?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Perhaps something like this (untested, I don't use DOS compilers)
    Code:
    while ( 1 ) {
        if ( kbhit() ) {
            int ch = getch();
            if ( ch == 'x' ) {
                printf("X\n" );
            } else if ( ch == 27 ) {
                printf("Escape while you can!\n" );
            } else {
                printf("Dude, what's %c\n", ch );
            }
        } else {
            printf("Nothing happening, taking a nap\n");
            sleep(1);  // perhaps you have delay(1000)
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Hyderabad
    Posts
    10
    Rep Power
    0

    use nested loops


    Use nested loop concepts to trouble shoot the problem
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by salem
    Perhaps something like this (untested, I don't use DOS compilers)
    Code:
    while ( 1 ) {
        if ( kbhit() ) {
            int ch = getch();
            if ( ch == 'x' ) {
                printf("X\n" );
            } else if ( ch == 27 ) {
                printf("Escape while you can!\n" );
            } else {
                printf("Dude, what's %c\n", ch );
            }
        } else {
            printf("Nothing happening, taking a nap\n");
            sleep(1);  // perhaps you have delay(1000)
        }
    }
    "undefined reference to 'sleep' in the last line, says codeblocks...no other errors.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    Hm... it's not working well, in VS, neither in Code::Blocks.

    How can I get away with sleep function ?
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Well if you're using visual studio or code::blocks, then stop using kbhit() and conio.h - they belong to DOS, not Win32.

    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
    These seem like good functions to read up on
    PeekConsoleInput Reads data from the specified console input buffer without removing it from the buffer.
    ReadConsole Reads character input from the console input buffer and removes it from the buffer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0
    salem> it's useful only in C++, I can't get this working in pure C.

    Code:
    while ( 1 ) {
        if ( kbhit() ) {
            int ch = getch();
            if ( ch == 'x' ) {
                printf("X\n" );
            } else if ( ch == 27 ) {
                printf("Escape while you can!\n" );
            } else {
                printf("Dude, what's %c\n", ch );
            }
        } else {
            printf("Nothing happening, taking a nap\n");
            sleep(1);  // perhaps you have delay(1000)
        }
    }
    This code is not doing what it's meant to do.

    How can I modify it to:

    a) printf some text
    b) IF X is pressed program should restars, clear screen (cls) and printf some text again (it's like restart)
    c) IF ESC is pressed program should shutdown
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    > salem> it's useful only in C++, I can't get this working in pure C.
    There's nothing C++ specific about the Win32 console API.

    > I can't get this working in pure C.
    That's because 'pure' C knows nothing about kbhit() or getch().

    How can I modify it to:

    a) printf some text
    b) IF X is pressed program should restars, clear screen (cls) and printf some text again (it's like restart)
    c) IF ESC is pressed program should shutdown
    a) does the program I wrote respond to 'x' and ESC key presses (you see different messages).
    If it does, then you're most of the way towards your assignment.

    At least, you're not getting any more code until you make a stab at changing the code to do what YOU want in particular.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Originally Posted by salem
    > salem> it's useful only in C++, I can't get this working in pure C.
    There's nothing C++ specific about the Win32 console API.

    > I can't get this working in pure C.
    That's because 'pure' C knows nothing about kbhit() or getch().


    a) does the program I wrote respond to 'x' and ESC key presses (you see different messages).
    If it does, then you're most of the way towards your assignment.

    At least, you're not getting any more code until you make a stab at changing the code to do what YOU want in particular.
    what library do you include to use kbhit() and getch() ? (conio.h or stdio.h? what are these two libraries, what do they include in them as in what functions, so I know what to include where?)

    and getch() works on codeblocks without any library included....

    and just tested, kbhit also works..

    maybe codeblocks automatically adds the libraries...

    And also, the code is not working...not sure what is wrong...I included conio.h and stdio.h.
    I've got understood the concept of the code, but not sure why its not working...

    It doesn't detect the keyboard hits...just keeps printing "nothing happening...". ESC doesn't work either.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    74
    Rep Power
    2
    Okay, I've got it working...

    Actually the problem was the delay mechanism you used ( sleep(1) ) was not working, so even though the code actually worked, I couldn't see the result cuz it went away so fast.

    I've added a different delaying blank loop, and it works.

    Code:
    #include<conio.h>
    #include<stdio.h>
    
    
    main()
    {int i;
    
    while ( 1 ) {
        if ( kbhit() ) {
            int ch = getch();
            if ( ch == 'x' ) {
                printf("X\n" );
            } else if ( ch == 27 ) {
                printf("Escape while you can!\n" );
            } else {
                printf("Dude, what's %c\n", ch );
            }
        } else {
            printf("Nothing happening, taking a nap\n");
            for(i=0;i<100000000;i++)
    {};  // perhaps you have delay(1000)
        }
    }
    }
    And I think this way of delaying uses alot of processor resource...

    does sleep() function work the same way(as in the code below) or is it different?

    Code:
    #include<conio.h>
    #include<stdio.h>
    
    
    main()
    {int i;while ( 1 ) {
        if ( kbhit() ) {
            int ch = getch();
            if ( ch == 'x' ) {
                printf("X\n" );
            } else if ( ch == 27 ) {
                printf("Escape while you can!\n" );
            } else {
                printf("Dude, what's %c\n", ch );
            }
        } else {
            printf("Nothing happening, taking a nap\n");
            sleep(1);  // perhaps you have delay(1000)
        }
    }
    }
    
    sleep(int j)
    {
        int i;
        j=j*500000000;
        for(i=0;i<j;i++)
    {}}
  20. #11
  21. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    conio.h is (should have been 'was') a crutch to enable cross-over DOS programmers to ease their way into a new world.

    20+ years later, they're still limping along, chained to the past.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo