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

0

I searched a lot on the internet and could not find something like GetAsyncKeyState () that is not in the windows.h library. Is there a library or some way forward that allows "direct" control of things like knowing if a key is being pressed, or knowing which key has been pressed through some kind of buffer?

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

Probably I need to use some thread stuff. But as for this matter of having control over the things that are pressed, I would like a way into that, a library to study ... something from shell script .. Something I can use with C ++.

    
asked by anonymous 31.05.2018 / 00:47

1 answer

1

Within the /dev/input folder of your Linux you will get information about the input events 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.

Inside this documentation you can see how to use it in the 2. Simple Usage part. I believe that, in your case, a simple fopen of the file relative to your input will already be necessary. input information is in /usr/include/linux/input.h .

To get the information use read to blocker or ioctl to non-blocker. This tutorial might 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);
    
31.05.2018 / 16:05