Emulate special Keyboard input (F21, F22, F23) through CLI

Context: I have a 2-in-1 convertible laptop which is not behaving as a convertible (see this post on the forum) so I need to catch KEYBOARD_KEY libinput events and emulate them through CLI to trigger and induce some behaviours.

Particularly: I’m trying to use the detect-tablet-mode-git (AUR) which has two parts; the first is a Ruby script that monitors events on a specific event# peripheral (by-path) while the second is a config file that contains the actions to perform when the trigger is set.

To make this work the first step is to monitor what actually gets triggered in the system when I fold the screen to (and from) tablet-mode.

$ sudo libinput debug-events
-event3   DEVICE_ADDED            Ideapad extra buttons			seat0 default group7  cap:k
-event2   DEVICE_ADDED            AT Translated Set 2 keyboard		seat0 default group8  cap:k
 event2   KEYBOARD_KEY            +0.000s		KEY_F21 (191) pressed
 event2   KEYBOARD_KEY            +0.009s		KEY_F21 (191) released
-event3   KEYBOARD_KEY            +0.011s		KEY_F23 (193) pressed
 event3   KEYBOARD_KEY            +0.011s		KEY_F23 (193) released
-event3   KEYBOARD_KEY            +26.513s	KEY_F22 (192) pressed
 event3   KEYBOARD_KEY            +26.513s		KEY_F22 (192) released

Where KEY_F21 and KEY_F23 take place upon going from laptop- to tablet-mode, while KEY_F22 happens on the way back.

What I’m looking for is a way to emulate those keys’ events through CLI (or code) so I can:

  1. Discern what event corresponds to what, meaning which key is enabling/disabling the keyboard and which one is disabling/enabling the touchpad (failing to do the latter);
  2. Find out if another key, maybe F24, would compensate for the missing activate-touchpad activity;
  3. Identify exactly what peripheral event# I should monitor (event2 or event3 in the example) so to avoid unwanted triggering;
  4. Possibly use this CLI-generated event as part of the steps to be executed on trigger activation.