Getkey function

This is a discussion on Getkey function within the C++ Programming forums, part of the General Programming Boards category; I just need an example of the getkey funcion use. Any example willl work i just dont have my reference ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    13

    Getkey function

    I just need an example of the getkey funcion use. Any example willl work i just dont have my reference book and i havent been able to find a good source on the internet(except forums) for answering my questions. BTW thanks to everyone that has helped me out.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What getkey function would that be then? There isn't one in standard C++.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    13
    I just need to be able to take a keyboard input character and save it to a variable. Sorry I'm dont program enough to be good at it.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    How about the getch() function in conio.h (not standard - but common enough)

    Code:
    #include <conio>
     
    int main(int argc, char *argv)
    {
    	char input;
    	input = getch();
    	return 1;
    }
    Last edited by sean; 08-10-2004 at 01:23 PM.

  5. #5
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    Use this. I just came across this in my C++ book when I read this message. Lucky for you

    Code:
    #include <conio.h>
    	char dir;
    
    	dir = getche();
    
    	if (dir == 'y')
    	{
    		std::cout << "\nYou hit yes." << std::endl;
    	}
    	else
    	{
    		std::cout << "\nYou hit something else." << std::endl;
    	}
    Last edited by philvaira; 08-10-2004 at 02:17 PM.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by athos
    I just need to be able to take a keyboard input character and save it to a variable. Sorry I'm dont program enough to be good at it.
    If that's all you want, then there's no need for non-standard functions:
    Code:
     #include <iostream>
     
     int main(void)
     {
       int c;
       
       while ((c = std::cin.get()) != EOF)
       {
     	std::cout << (char)c << std::endl;
       }
       
       return(0);
     }
     
     /*
      Output:
      
     this is cool
     t
     h
     i
     s
     
     i
     s
     
     c
     o
     o
     l
     */
    But, are you actually asking for non-buffered IO, where you hit a key and the program responds without the user pressing [enter] ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    Well, all these responses are good. It just depends what you want for your program. If you need to hit 'y' without pressing enter, my code above is great for that. It sounded like that's what you needed since you mentioned a GetKey-like function. Anyway, good luck.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>my code above is great for that
    Only if their compiler has that non-standard function, otherwise there'll be compile errors and more questions/confusion from athos.

    athos, if you could expand on what it is you want, and what compiler/OS you're doing this on, it'd make our jobs a lot easier.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User philvaira's Avatar
    Join Date
    Apr 2004
    Posts
    72
    Yeah, so we don't murder eachother here. Still, conio.h is quite popular in most compilers that I've seen.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by philvaira
    Yeah, so we don't murder eachother here. Still, conio.h is quite popular in most compilers that I've seen.
    Ah, another DOS/Windows user... Humorous aren't they?

    [edit]On an aside, isn't this in the FAQ? Why, yes, yes it is![/edit]

    Quzah.
    Last edited by quzah; 08-11-2004 at 09:04 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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