Pressing keys in the terminal

Asked

Viewed 135 times

0

Hello, in my Ubuntu 16.04, at the time of running a program in C (as well as in sublime), when I press a key, keeps repeating the same key until release. So far so good. But if I don’t release this key and I press another key, this one keeps repeating and it’s like I’ve released the first key.

There is a way to check if both keys are pressed simultaneously?

1 answer

0

There are keys and keys. You cannot read (through a scanf) for example the combination of keys A and B pressed at the same time, as it is not a character, it simply does not exist AB.

To use some key combination Voce will have to use the following: CTRL, ALT and SHIFT, combined with some character key, i.e., the characters from A to Z.

Note that all program shortcuts are CTRL + (A-Z), ALT + (A-Z) or SHIFT + (A-Z) it is also common to use the keys F(1-12) (Functions Keys).

In reality some combinations of these keys emit a non-printable character, a control character that your software can use to perform some specific function.

Compile this code and see the value difference in key combinations with CTRL, ALT and SHIFT.

#include <stdlib.h>
#include <stdio.h>

int main()
{
    unsigned char key;

    printf("press any key:>");
    scanf("%c", &key);
    printf("key pressed:>%X \n", key);

    return 0;
}
  • Right, below is my repository on github: https://github.com/CarlosAdir/C-Programs/tree/master/Tutoriais/Arquivos_kbhitgetch. One folder above, has the tutorial kbhitgetch.pdf, explains this part of data input. But Compile and run the 8-extra code. c and hold a key. It shows each key pressed. But the question would be: how to know if two keys are pressed, if there is any specific configuration of the finished for example. The code compiled and ran on Ubuntu 16.04 perfectly.

  • I was able to easily compile the code, but I don’t think I understood your question yet. You wanted to detect when I pressing and safe the X key, leave it pressed and then press the Y key? Would that be the detection that I’m looking for?

  • Yes. When I press and hold the X key, it starts to appear several "You pressed X" and if you release it stops to appear." You pressed X". But if I do not release X and press and hold Y, stop "You pressed X" and it appears "You pressed Y" several times, as if I had released X. .

  • I get it, I never like to say there’s no way to do it because in the computer world there’s always a way. But note the following: When you press and hold the X key, then press and hold the Y key without releasing the X key, release the Y key and see that the program no longer detects that the X key is being pressed. Therefore, based on this behavior, it seems that the moment Voce presses the Y 'kills' the X.

  • Yeah, I’ll look a little deeper into that. Thank you!

Browser other questions tagged

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