keyboard input

hello everyone i am pretty new to C++ and have looked over many topics about this but cant seem to grasp it

what i want to know is how can i have it to where if i press a certain key on my
keyboard something happens

Example: if i press 'F'
cout << "you pressed F";

any help would be nice
you could allegro or sfml libraries
but for a quick solution

1
2
3
4
5
6
7
8
#include iostream
using namespace std;
int main(){
char input;
cin>>input;
if (input==f)
{//do something}
return 0;}


or if you need to more key options, you could always replace the if statement with a switch statement

if you want something that runs through the entire program( for example if anytime during the program, someone presses something something will happen, you need to learn about multithreading- check out sfml)
What you want is called "unbuffered input". I didn't think I had a simple example here, but a quick google search found me this one (Windows only):
http://www.cplusplus.com/forum/beginner/9503/
You can do the same sort of thing on *nix systems too, but as I am not at home I'll leave that to you to google if you want it.
(Hmm, how about this: http://www.cplusplus.com/forum/beginner/5619/#msg25139 )

Hope this helps.
2 questions

1. i got TheCreators example to work but i still have to press enter to confirm it
is there a way around this >.>?

2. what exactly do you mean by allegro or sfml libraries >.<?

anyway thanks for the replies :D
First you need to know the basic data types for this (int, char, string, etc...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string> // To use string
using namespace std;

int numberInput
char charictorInput
string stringInput

int main()
{
	cout << "Please enter a number: ";
	cin >> numberInput;

	cout << "Please enter a charictor: ";
	cin >> charictorInput

	cout << "Please enter a word: ";
	cin >> stringInput;

	cout << numberInput << charictorInput << stringInput;


	cin.get();
	return EXIT_SUCCESS;
}


I hope this helped you, you may also want to look up a tutorial if you don't understand this.
1) using my method this is not possible. duoas gave interesting info on unbuffered input. In my opinion, unbuffered input would be the best solution.

2) you could use allegro features. download the allegro library and use specific features made available through the library. im not too familiar with allegro but this would basically give what your looking for:

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

#include <allegro.h>
using namespace std;

int main()
{
       allegro_init();
       install_keyboard();

       if (key[KEY_F])
{
       //do something
}
       return 0;
}


END_OF_MAIN();


This basically does something if the f key is pressed (you dont need to press enter and the key can be pressed at any time). You could change the if statement with a switch if your looking for more keys. My advice to you is too still check out unbuffered input as Duoas suggested
Last edited on
Topic archived. No new replies allowed.