keypress / if (char c == 'd')

hello there!

i found some code on the web for detecting and analyzing a keypress. i modified it to make it a bit smaller and get rid of useless lines.
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

using namespace std;

string d;

int main()
{

    struct termios oldSettings, newSettings;

    tcgetattr( fileno( stdin ), &oldSettings );
    newSettings = oldSettings;
    newSettings.c_lflag &= (~ICANON & ~ECHO);
    tcsetattr( fileno( stdin ), TCSANOW, &newSettings );    

    while ( 1 )
    {
        fd_set set;
        struct timeval tv;

        FD_ZERO( &set );
        FD_SET( fileno( stdin ), &set );

        int res = select( fileno( stdin )+1, &set, NULL, NULL, &tv );

        if( res > 0 )
        {
            char c;
            printf( "Input available\n" );
            read( fileno( stdin ), &c, 1 );
            std::cout << c << std::endl;
            break;
        }
    }
    
    if (c == 'd') cout << "d pressed";

}

the thing, which doesn't work, is the line i added. :D
 
if (c == 'd') cout << "d pressed";

i hope it's clear, what i'm trying to do. i just want to recognize, which key acutally has been pressed.

thank you in advance for any help! :)


-kay
Try moving it inside the while loop and resetting the key each time.
Wouldn't using something like SDL make more sense for a task like this?
closed account (Dy7SLyTq)
not neccesarily. thats only if you want to make a gui window. if you want to make a vim clone or something like that, ncurses would be better, but i guess this way works too. (i shudder at the c/c++ mixing like that though)
OP's code has nothing to do with GUI. If you are using X, you'll get X key events. (Or whatever your framework's key event system gives you.)

That program should not compile. Line 38 declares c, but you use it out of scope (lines 37 through 43) on line 46.

Good luck!
Thank you for the responses! I'm sorry for being away for a while, just read it.

So I simply moved line 38 to the top of the main-function and all works fine.

The only thing left is the detection of key-arrows. I've already read some topics about that. Most of them say, there will be two characters to analyse. But i never understood, how that's done.
Could you explain it to me or rather post some code? Would be great!


-kay
On PCs there will be two codes.

On *nix, you could a whole string of characters.

It might be worth your time to use NCurses.
Variable c is declared in if statement, which in turn is in while loop. Therefore variable c is dead if you go out of while loop.
closed account (Dy7SLyTq)
im pretty sure its dead if it goes out of the if statement. i could be wrong, but scope is limited to the matching end bracket
Don't forget globals also DTSCode.
@Hungduongn http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm
hungduongn wrote:
Variable c is declared in if statement, which in turn is in while loop. Therefore variable c is dead if you go out of while loop.
DTSCode wrote:
im pretty sure its dead if it goes out of the if statement. i could be wrong, but scope is limited to the matching end bracket
giblit wrote:
Don't forget globals also DTSCode.

Why are we all answering questions OP has resolved 18 hours ago?

Kay94 wrote:
So I simply moved line 38 to the top of the main-function and all works fine.

OP has new question:

Kay94 wrote:
The only thing left is the detection of key-arrows. ...
closed account (Dy7SLyTq)
Don't forget globals also DTSCode.

global still has scope; its to the end of the program.
So has anyone an idea, how key-arrows could be acknowledged here? :)

-kay
It depends on what platform you are on. If windows you can use the GetKeySync or GetAsynKeySync I believe they are called.
closed account (Dy7SLyTq)
there is no platform independent way. boost might have something. otherwise you would have to use something like *curses or sfml
Sorry I mean GetKeyState and GetAsynKeyState. Also check out the virtual keys ( left = VK_LEFT ect for the other keys ). Or this thread: http://www.cplusplus.com/forum/beginner/75529/ Also disch suggested using curse earlier same with dts
Last edited on
Topic archived. No new replies allowed.