putting condition on input

This is a discussion on putting condition on input within the C Programming forums, part of the General Programming Boards category; Hello all. I been workin on my pgm for the past 2-3 days, it's up to 418 lines of code ...

  1. #1
    Unregistered
    Guest

    putting condition on input

    Hello all. I been workin on my pgm for the past 2-3 days, it's up to 418 lines of code ( small i know ).

    I would like to know how to preform an action immediately when escape is pressed. The most simpilest way yet versitle.

    example:

    if ( /* code for detecting escape */ )

    printf("um..."); /* immediate action */

    I just spent 30min reading through tuts and links and things, can't really find it. Must've overlooked somewhere.

  2. #2
    ritter_kobbe
    Guest
    small example

    int c;

    c = getchar();

    // now press escape and then enter
    // printf("%d\n",c); would give 27, we use this figure

    switch(c)
    {
    case 27: // escape was pressed
    // action to do
    break;

    // case 3: // control C was pressed
    // break;

    default:
    break;
    }


    Hope this small example can help you.

  3. #3
    Unregistered
    Guest

    hmm

    Doesn't seem to work.

    First off /* This is a comment in code */

    Second,
    Im looking to have something done right when the key is pressed, not having to press enter. If i press the correct key, it's to late, i dont have to press enter to activate my choice.

    Can somebody point me to some good tutorials ( either detailed or several locations ) dealing with reading key presses from the keyboard and taking immediate action along with using loops.

    That'd be nice. It's better then bugging you with tons of questions. I've looked, just haven't found much.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    This isn't standard, but I'm guessing it's still pretty generic...

    Code:
    #include <keys.h>
    #include <pc.h>
    
    main ()
    {
     int key;
     for (;;)
     {
      key = getkey();
      switch (key)
      {
       case K_Esc: printf ("Esc!\n"); break;
       case 'q': case 'Q': exit (0); break;
       default: break;
      }
     }
    }
    At least that'll put you kn the right track maybe.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    This worked for me. Adapte it how you wish.
    Code:
    #define ESC 0x1B
    
    main()
    {
    char ch=0;
    
    while(ch!=ESC)
       {
       while(!kbhit()){}
       ch=getch();
       }
    printf("Done Sir Done");
    }
    There you go.

    Thantos

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21