How to know when the enter key is pressed

how to know when enter key is pressed in console application

Page 1 of 1

12 Replies - 7608 Views - Last Post: 02 December 2010 - 08:02 PM Rate Topic: -----

#1 baseball435  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 68
  • Joined: 30-November 09

How to know when the enter key is pressed

Posted 06 November 2010 - 07:47 AM

hey everyone. I have been trying to figure this out but i havent found anything. What is a code for when you press the return/enter key it will output (cout) a message? Oh and by the way its for a console application.

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How to know when the enter key is pressed

#2 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: How to know when the enter key is pressed

Posted 06 November 2010 - 12:20 PM

#include <iostream>
using namespace std;

int main()
{

cout << "Hello" << endl;

cin.get(); //<<<<< Here cin.get(); is used to keep the console window open it will also make it to where you have to hit enter for the message to come up.


cout << "I won't go away unless you press enter" << endl;

cin.get(); 

return 0;

}


Hope that helps :)

This post has been edited by Alex6788: 06 November 2010 - 12:21 PM

Was This Post Helpful? 2
  • +
  • -

#3 Lesco  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: How to know when the enter key is pressed

Posted 06 November 2010 - 09:22 PM

Are you trying to re-output what the user had just entered before he presses the enter key, or just display a message when the user presses the enter?

Won't
cin.get()

just wait for any key to be pressed. That would work, although if he wants it to be specifically the enter key you might have to add some type of selection structure looking for the enter key specifically.

This post has been edited by Lesco: 06 November 2010 - 09:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to know when the enter key is pressed

Posted 06 November 2010 - 09:28 PM

View PostLesco, on 07 November 2010 - 01:22 PM, said:

That would work, although if he wants it to be specifically the enter key you might have to add some type of selection structure looking for the enter key specifically.


No. Not really. The action won't do anything until the <Enter> key is pressed.
Anything pressed before that will be ignored.
So no need to specifically look for the <Enter> key because it is the only key that will let the program progress.
Compile and try Alex6788's code and you'll see what it means.
Was This Post Helpful? 1
  • +
  • -

#5 Lesco  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 55
  • Joined: 05-December 08

Re: How to know when the enter key is pressed

Posted 06 November 2010 - 09:44 PM

Very nice, I actually just went and did some reading on cin.get() and it makes sense now. Just not something I regularly use. Thanks for the heads up though!
Was This Post Helpful? 0
  • +
  • -

#6 baseball435  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 68
  • Joined: 30-November 09

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 12:36 PM

no i mean like you press a key, lets say 'P' for this example, and it will display a message either a cout or a messagebox
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 12:58 PM

View Postbaseball435, on 09 November 2010 - 04:36 AM, said:

no i mean like you press a key, lets say 'P' for this example, and it will display a message either a cout or a messagebox


That's exactly the opposite question to your original posting!

Did it take you 2 days to realise you asked the wrong question?

Are you absolutely sure you know what your question is this time?

How about you give us a detailed description of exactly what you want to do and on what Operating System you want to do it on.
At the moment it is too unclear to be meaningful answered.
Was This Post Helpful? 0
  • +
  • -

#8 baseball435  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 68
  • Joined: 30-November 09

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 04:20 PM

sorry guys, i ment that I want to make a console program where when you press a certain button like the 'p' key then it will show a messagebox or cout something
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,441
  • Joined: 23-August 08

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 04:32 PM

Are you waiting for someone to write this for you, or are you going to write some code?
Was This Post Helpful? 0
  • +
  • -

#10 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 06:13 PM

You could try making a switch statement so when p is typed it displays a message.
Was This Post Helpful? 0
  • +
  • -

#11 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: How to know when the enter key is pressed

Posted 08 November 2010 - 06:14 PM

Ghetto style:

char * input;

cin >> input;

if(input == "P"){
cout << "win!";
}
else{
cout << "Fail!";
}


This post has been edited by creativecoding: 08 November 2010 - 06:21 PM

Was This Post Helpful? 0
  • +
  • -

#12 snowboarder6009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 11-September 08

Re: How to know when the enter key is pressed

Posted 02 December 2010 - 03:46 PM

Google is your frined :) the first page after googling "c++ know when enter key pressed" had the answer.

http://www.cplusplus.../beginner/2624/
Was This Post Helpful? 0
  • +
  • -

#13 ZeroSixx  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 43
  • Joined: 04-March 10

Re: How to know when the enter key is pressed

Posted 02 December 2010 - 08:02 PM

Check out the GetAsyncKeystate function. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1