Checking to see if key is pressed

This is a discussion on Checking to see if key is pressed within the C Programming forums, part of the General Programming Boards category; What is the command for that ? I need to do a range of actions depending upon which key is ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    59

    Checking to see if key is pressed

    What is the command for that ? I need to do a range of actions depending upon which key is pressed , so for ex. if key H is pressed the Help section is printed on screen .

  2. #2
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Check the FAQ on how to get input without having the user press Enter.
    Last edited by sea_4_ever; 08-27-2007 at 11:03 PM. Reason: "press" has two 's's in it. I forgot one.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by ki113r View Post
    What is the command for that ? I need to do a range of actions depending upon which key is pressed , so for ex. if key H is pressed the Help section is printed on screen .
    Hmm, well it depends on your needs I would guess. If h is in the input it was definitely pressed. You can code an event-driven system quite easily. For example
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int dispatcher( const char job )
    {
      switch( tolower( job ) )
      {
        case 'a':
          puts( "Welcome to the Matrix!" ); 
          /** call a function to print a welcome message. **/
          fflush( stdout );
          return 0;
        case 'b':
          return 0; /** Just exit. **/
        default:
          break; /** This case does nothing. **/
      }
      return job;
    }
    
    int main( void )
    {
      char line[80];
      do
      {
        puts( "Enter a for the red pill, b for the blue pill." );
        fflush( stdout );
        fgets( line, sizeof line, stdin );
      }
      while( dispatcher( line[0] ) );
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Hi citizan thanks for that. Sorry I forgot to mntion this but I was looking for something that runs in the background so to speak ,,, ie there is no input required from the user , it for ex. just displays help info when key h is pressed no matter what part of the program the user is in. Here is info what I received from someone else , but I am not sure how to use this in my situation :

    Code:
    while(1)
    {
    if(kbhit())
    { /* write your keybd traps here */ }
    
    //write your rest of the program here.
    }

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Look into ncurses or PDCurses (additional ncurses support for windows).

    If you're on windows, you can use GetAsyncKeyState() Or similar functions...

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Has anyone ever wondered what would have happened if Neo had picked the blue pill?
    A lot of inventions came from the Matrix movies, if they never happened, what state would the world be in now?
    Maybe, the guys who made the Matrix would have chaneled their energy into something more "worthwhile" like a new FF game!
    The world MIGHT have been a better place.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    NVM it turns out you don't have to have it running in the background .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  2. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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