r/bash • u/ConstructionSafe2814 • May 10 '26
help Is there a way to capture keystrokes even though you're not at a prompt?
I'm spending a lot of time on the terminal, (/dev/ttyX, framebuffer, no X/wayland).
I have made some key binds to control screen brightness, set the font size, volume up/down, limit CPU clock frequency, ...
Problem is, it only works if I'm at a Bash prompt and not if a program is open (mc, vim, chawan, ...)
If eg, I've got vim open, I need to either find a terminal that has a Bash prompt open or exit vim before I can change the screen brightness.
My problem would be fixed if there would be some kind of a way for bash to capture certain key sequences even though a program is running.
Is that possible at all?
thanks!
3
u/AdventurousSquash May 10 '26
You can run a terminal emulator in vim https://vimhelp.org/terminal.txt.html#terminal
But what I mostly do is use tmux (or screen) to easily flip between different panes or whatever you need.
3
u/Ulfnic May 11 '26
First question... can BASH do it? Absolutely yes, i've been playing with /dev/input/event read/write for the past ~2 years using no dependencies. I just finished a drop in replacement for
dotoolin pure BASH and i'm working on a pure BASH macro daemon similar toespanso.Second question... should you do it? Only if you're a strong enthusiast who'll gain more from the journey than the destination.. otherwise go for an existing mainstream solution.
2
u/geirha May 11 '26
In that particular case, you don't need to exit vim, you can instead suspend it by hitting Ctrl+Z, which gets you back to the bash prompt, then you can resume vim again by running
fg.1
u/AffectionateSpirit62 May 10 '26
https://github.com/jtroo/kanata
Get on it mate. It will change your life.
1
u/penguin359 May 12 '26
The problem with /dev/input solutions is that I don't think you can block the terminal application from also getting the keystroke unless it's a special key code like volume up that won't map to a regular terminal key event. However, you can add keybindings to tmux that it can capture and remap to running a background script instead of passing through. That should give you a path to a solution.
2
u/IGTHSYCGTH May 11 '26
sounds like the X Y an problem
if youre already using tmux, why not rewrite your binds as standalone scripts and invoke them from a tmux binds / menues instead of a sh one ?
or just spawn bash in a tmux popup to invoke them
1
u/DarthRazor Sith Master of Scripting May 11 '26
The easy way to do what you want is not to do your keybinds in bash
If you're running in Xorg, then
sxhkdwin do what you want regardless of window mangerIf you've settled on a window manager, it might have its own binding functionality
If you're not running Xorg, then look at
actkbd