How can I capture keyboard events and use the monitor as a text display in Linux?

I have a server running in multi-user mode, which is connected to the keyboard and monitor. On a separate computer, I would like to SSH to the server and run a program that captures keyboard input and prints text on the monitor. The keyboard and monitor will never be used for any other purpose.

Currently, when the server is loading, the input screen (text) is displayed on the monitor. I know that I can write the appropriate device /dev/tty . However, the keyboard is also recorded in the same place.

How can I capture keyboard events and use the monitor as a text display in Linux?

Perhaps I am missing some basics for hardware interoperability in Linux. What do i need to know?

Ending the time for the reward - if someone wants to do this further, I will award accordingly:

I did the following to prevent any login programs from starting:

 systemctl mask serial-getty@ttyO0 systemctl mask systemd-logind systemctl mask getty@tty1 

Then I run my program when required:

 openvt -c 1 -f /path/to/my/program 

This will be a completely satisfying solution, except that -f bothers you. What works on tty1 ? Is there anything I can do to stop him? Should I stop him?

It is noteworthy that this solution satisfies all my needs. As far as I understand, I take on tty and get access to its stdin (I get keyboard output) and stdout (print for monitoring).

+6
source share
1 answer

If you want to switch to low-level programming, you do not have to handle stdin or stdout. Just talk to your keyboard device and console device directly.

Take a look at the source code for input-events from input-utils . It uses raw data to read a keyboard, mouse, or any other input device.

To avoid default processing on the keyboard, it uses the capture mode ( -g in the CLI), which means:

 ioctl(fd,EVIOCGRAB,1) 

in the file descriptor of the device. Reading the source events from /dev/input/event* more or less straightforward: #include <linux/input.h> , capture and then read the struct input_event structures from the device.

To write to the console, if you do not want to mess with TTY madness, and now that you are in low level mode, you can write directly to /dev/vcs or /dev/vcsa for color (a for attribute).

Basically, vcs has one byte per screen cell containing the code of the character to be displayed. There are two bytes in vcs : character and attributes. See man vcs more details.

+2
source

Source: https://habr.com/ru/post/988469/


All Articles