I want to run CLI commands by just pressing a button on my highly configurable UHK keyboard. I copy some text and press a key on my UHK, and presto a CLI command is executed, something pops up on my screen or is brought to the front by a command to the window manager.
I have some higher F-keys mapped to some keys.
When I press them, a Kmonad config picks them up like so
(defcfg
;; Set your keyboard device file
input (device-file "/dev/input/by-id/usb-Ultimate_Gadget_Laboratories_UHK_60_v1-event-kbd")
output (uinput-sink "My Virtual Keyboard")
fallthrough true
allow-cmd true
)
(defalias
chromium-cielo (cmd-button "sudo -u shinichi python /home/shinichi/source/dotfiles/files/scripts/kmonad-handler.py cielopnl")
chromium-photon (cmd-button "sudo -u shinichi python /home/shinichi/source/dotfiles/files/scripts/kmonad-handler.py photon")
chromium-dexscreener-sol (cmd-button "sudo -u shinichi python /home/shinichi/source/dotfiles/files/scripts/kmonad-handler.py dexscreener")
chromium-bullx (cmd-button "sudo -u shinichi python /home/shinichi/source/dotfiles/files/scripts/kmonad-handler.py bullx")
)
(defsrc
f21 f22 f23 f24
)
(deflayer command-test @chromium-dexscreener-sol @chromium-bullx @chromium-cielo @chromium-photon)
The python script is the one that has access to the clipboard.
import os
import subprocess
import argparse
# Define a dictionary to map arguments to URLs
URL_MAP = {
...
}
def execute(argument):
# Set environment variables for Wayland
os.environ["WAYLAND_DISPLAY"] = "wayland-0"
os.environ["XDG_SESSION_TYPE"] = "wayland"
os.environ["XDG_RUNTIME_DIR"] = "/run/user/1000"
# Capture the clipboard content using wl-paste
try:
clipboard_content = subprocess.check_output(["wl-paste", "--no-newline"], text=True).strip()
except subprocess.CalledProcessError:
print("Error: Unable to retrieve clipboard content.")
return
if clipboard_content:
# Check if the argument is in the URL map
if argument in URL_MAP:
url = URL_MAP[argument] % clipboard_content
print("chromium", url)
# Optionally, you can use subprocess to open the URL in a browser
subprocess.run(["chromium", url])
else:
print(f"Error: Unknown argument '{argument}'. Available options are: {', '.join(URL_MAP.keys())}")
else:
print("Error: empty clipboard!")
def main():
# Set up argument parsing
parser = argparse.ArgumentParser(
description="Generate and open URLs based on clipboard content using specified arguments.",
epilog="Example usage: python clipboard_url.py photon"
)
parser.add_argument(
"argument",
choices=URL_MAP.keys(),
help="The type of URL to generate."
)
args = parser.parse_args()
execute(args.argument)
if __name__ == "__main__":
main()
I want to do this for many more things (like just press h to quickly have a htop) but the problem is, I only have F13-F24 to work with and some are already used by the system e.g. touchpad toggle, mic mute etc.
I heard about custom scancodes but evtest
doesn’t show them when I set a key to M160 for example. (I’m on Wayland,GNOME)
As you can see, I can have a Fn2 layer dedicated to shortcuts like these but it doesn’t matter much if I’m limited at the scancode/keycodes which I can send to the OS. Ctrl-Alt-F13-F24 might work but is just more clutter plus I don’t know how to get that to work in the Kmonad config. Any suggestions guys?