Thread: When a key is pressed, print to screen this... How?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    5

    When a key is pressed, print to screen this... How?

    Ok so Im pretty new to programming.
    I want something like
    When D is pressd, print the character in value 'brush' onto the screen. So if 'brush' had X in it, when the user presses D on the keyboard it prints X.
    I cant use cin , cause I dont want the D to be printed. I just want the value inside brush to be printed.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    have u looked into getch() ? located in conio.h (compiler specific)

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Alright, I sat down and did a little playing around, and then it hit me. If you use the getch() function in conio.h, you should be able to take single characters as input, which will not be displayed on the screen. Well, it works for me, anyway.

    #edit#
    Hah, someone beat me to it. I had my response open so long and didn't bother to check.
    Last edited by Vorok; 05-31-2003 at 11:48 PM.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    5
    Errr well can someone write a simple little example, cause all I have been using for about a week now is iostream.h :P

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    very basic
    Code:
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    int main()
    {
    	char ch;
    	do{
    	ch = getch();
    	
    	if (ch == 'a')
    		cout << "This will only be shown when you press lowercase a" << endl;
    	}while(1);
    	return 0;
    }
    now everytime u press a lowercase a, and only a lowercase a, will it display that message

    umm, its an infinite loop, i got lazy, so u will have to exit the console

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    5
    Thank you
    Great, seems to work. Thanks for the help everyone.
    Last edited by Woggy; 05-31-2003 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop until a key is pressed
    By kingneb in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2008, 03:22 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. Replies: 2
    Last Post: 07-25-2003, 03:01 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM