Something like Getasynckeystate() for Linux systems, is there something similar?

Asked

Viewed 58 times

0

I did a lot of research on the internet and couldn’t find something like Getasynckeystate() that isn’t in the windows.h library. Is there any library or path to follow that allows "direct" control of things like whether a key is being pressed or, knowing which key was pressed through some kind of buffer?

I think it would be something like "control of keyboard functions" or "control of its state". I’d like to know how it works.

I probably need to use something thread. But as for this question of having control over the things that are pressured, I would like a path on this, a library to study... something shell script.. Anything I can use with the C++.

1 answer

1

Inside the folder /dev/input of your Linux you will have information about the events of input of your system. They come in the following format:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

'time' is the timestamp, it Returns the time at which the Event happened. Type is for example EV_REL for relative Moment, EV_KEY for a keypress or release. More types are defined in include/Uapi/linux/input-Event-codes. h.

'code' is Event code, for example REL_X or KEY_BACKSPACE, Again a complete list is in include/Uapi/linux/input-Event-codes. h.

'value' is the value the Event Carries. Either a relative change for EV_REL, Absolute new value for EV_ABS (joysticks ...), or 0 for EV_KEY for release, 1 for keypress and 2 for autorepeat.

Within this documentation you can see how to use in the 2. Simple Usage. I believe, in your case, a simple fopen of your input file will already be needed. Information for input are in /usr/include/linux/input.h.

To get the information use read for blocker or ioctl for non-blocking. This tutorial may help you.

Look at this example:

FILE fd = fopen("/dev/input/eventX", "r"); //troque o path para o seu caso
char keys[16];
ioctl(fd, EVIOCGKEY(sizeof keys), &keys);
int keyb = keys[KEY_ESC/8]; //  ESC, por exemplo
int mask = 1 << (key % 8); 
bool state = !(keyb & mask);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.