How do you get a pressed character to a variable?

hey all!

I want to do a compare statement that compares what key the user has pressed,
it would look something like this:

(var) pressedkey;
cout<< "Press a key!";
(cin or get)>> pressedkey;
cout<< " You pressed "<< pressedkey << "key!";


But i dont want to compare characters, i want to compare for example did the user pressed "return(enter)" key, in scar (pascal based macro program) enter is char 13

So how do i do it?
Please help!
You can do it almost exactly the same:
1
2
3
4
char key;
std::cout << "Press a key: ";
std::cin   << key;
std::cout << "You pressed " << key << ".\n";

If you want to compare keys:
1
2
3
4
5
6
7
8
9
if (key == 0x1B) { // ASCII code for escape
    std::cout << "You pressed escape!\n";
    std::cin.get();
    exit(0);
} else if (key == 13) { // ASCII code for enter/return key {
    std::cout << "\n";
} else if (key == 32) { // ASCII code for space key
    std::cout << " ";
}
and so on.

You can also compare like this:

1
2
3
4
5
if (key == '\n') {
    // Do something
} else if (key == someChar) {
    // Do something
}


or like this:
1
2
3
if (' ' == 32) {
    std::cout << "ASCII code for space is 32!";
}

127 is the limit.

In fact, try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() {
    std::cout << "Numbers:\n";

    for (int i = 33; i < 127; ++i) {
        std::cout << i << " ";
    }

    std::cout << "\n\nCharacters:\n";

    for (char i = 33; i < 127; ++i) {
        std::cout << i << " ";
    }

    std::cin.get();
    return 0;
}
Last edited on
Look at my code:
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
26
27
while (key != 13)
        {
        
               int failurecountdown = 7;
               
                   do
                   {
                   Beep(270,500);
                       cout << "| system failure  ";
                       Sleep(500);
                       failurecountdown--;
                    }
                    while(failurecountdown >= 1);
               system("cls");       
              
                 Beep(1000,200);
                 Beep(2000,200);      
                 cout<< "|Use emergency over-ride key 13 (execute)";
                 
                 
                 cin.get(key);
                 if(key == 13)
                 {
                        break;
                 }
                       
}



Even when text " |use emergency over-ride key 13(execute)" appears and i press the enter (return) key it still doesnt break the loop, what am i doing wrong?
Last edited on
Because you're using cin.get() which doesn't work for this kind of thing.
cin.get() waits for the desired key to be pressed. It doesn't modify the contents of key, therefore it stays as whatever it was originally.

Use std::cin.
example plz?
Heres what i do:

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
26
27
28
29
using namespace std;
while (key != 13)
        {
        
               int failurecountdown = 7;
               
                   do
                   {
                   Beep(270,500);
                       cout << "| system failure  ";
                       Sleep(500);
                       failurecountdown--;
                    }
                    while(failurecountdown >= 1);
               system("cls");       
              
                 Beep(1000,200);
                 Beep(2000,200);      
                 cout<< "|Use emergency over-ride key 13 (execute)";
                 
                 
                 cin>> key;
                 if(key == 13)
                 {
                        break;
                 }
                        
              
    }


now when i press enter it just creates a new line and nothing happens untill i press a number or a letter
Last edited on
1
2
3
4
std::cin >> myKey;
std::cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');
if (myKey == 13) // Enter pressed
    doSomething();


I forgot about that. cin waits for enter... hm.
Try this:
key = std::cin.get()
cin.get() doesn't modify the contents of key, it just waits for the character you passed as a parameter to be pressed.
In the above example, cin.get() does modify key, because key is assigned the character pressed.
Last edited on
added key = std::cin.get();

still, same as doing cin.get(key);
it doesnt break out of the loop.
Last edited on
That's because of the nature of get(). You have to press the key, and then enter.

Take the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

int main() {
    char key;
    do {
        std::cout << "Press space to continue...\n";
        key = std::cin.get();

        if (key == 32) {
            return 0;
        }
    } while (true);
    return 0;
}


You have to press space, and then enter.

Bear with me.
Last edited on
Is there any way to press the char 13 only once? it would look beter

p.s., what is the asci number for end key?
Last edited on
I know, I'm trying to think of a way.
The only way I know of is to use getch(), but I really, really, really can't stress enough how much you should avoid using it.

Edit: 23
http://delphi.about.com/od/objectpascalide/l/blvkc.htm (virtual key codes)
http://www.asciitable.com/ (ASCII key codes)

Edit:
I think you'll need to use ncurses/curses. I don't know of any other way to do it, because the input is buffered so you have to press enter for the terminal to send the input to the program.
Last edited on
Ok, ill remember that, but could you tell me how to use that getch() thingy please?
No. Don't use it.
Ncurses most probably is able to do it.
Last edited on
Why not to use it?
It's old, deprecated, non-standard, platform (and sometimes compiler) dependant. It's only included in some compilers because of how popular it was.
ncurses or curses will be able to do the same thing, but in a better way.
ok, please post if you find a way, and thanks for helping me alot, i learned much from u :D
I told you a way.
Curses.
#include <curses.h>
and then you can use the getch() from curses. The other getch is from conio.h which is the one you should never use.
You'll also have to use the I/O functions included with curses.
Last edited on
...

Sigh. I suppose I really ought to finish my article on this kind of input.
In the meantime, see here
http://www.cplusplus.com/forum/articles/7312/page1.html#msg33734
im using dev-cpp,
it sais that it doesnt have the curses header file
Last edited on
SO CAN ANYONE HELP ME PLZ!@!!!!!!!!%#W@^$%$%*
Curses is not a standard library. You need to go download it.
Topic archived. No new replies allowed.