Keyboard shortcuts in the virtual terminal

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite
1












Is there any way of configuring keyboard shortcuts in the Linux virtual console?



For example, if I go to tty1 then press the key combination Ctrl+Alt+H, I would like the script /usr/bin/hello.sh to be executed.




Ideally, the shortcut would be available even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can also be a normal ELF binary or even a kernel module making system calls.




Example use cases



  1. I'm in the console and browsing the web with something like links and I want to turn down the screen brightness. I press Fn+End, which happens to be the brightness down key and produces a single keycode, and a program runs which reduces the brightness by writing something in a /sys file.

  2. I'm in a console text editor and listening to some music in the background that's being played by mpd. I press the ⏯ (play/pause) key, which again produces a single keycode, and that has the effect of executing a program which sends a signal to mpd to pause the current song.


Solution



Following dirkt's idea of using /dev/input, I created ksd (keyboard shortcuts daemon) for this purpose. The application can be started in the background and will take over the keyboard wherever you are, including before login and within X.







share|improve this question

















  • 1




    readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
    – quixotic
    Jul 13 at 15:35










  • Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
    – roaima
    Jul 13 at 15:44











  • @roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
    – rid
    Jul 13 at 22:48






  • 2




    @quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
    – rid
    Jul 13 at 22:50










  • @rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
    – quixotic
    Jul 14 at 4:37














up vote
6
down vote

favorite
1












Is there any way of configuring keyboard shortcuts in the Linux virtual console?



For example, if I go to tty1 then press the key combination Ctrl+Alt+H, I would like the script /usr/bin/hello.sh to be executed.




Ideally, the shortcut would be available even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can also be a normal ELF binary or even a kernel module making system calls.




Example use cases



  1. I'm in the console and browsing the web with something like links and I want to turn down the screen brightness. I press Fn+End, which happens to be the brightness down key and produces a single keycode, and a program runs which reduces the brightness by writing something in a /sys file.

  2. I'm in a console text editor and listening to some music in the background that's being played by mpd. I press the ⏯ (play/pause) key, which again produces a single keycode, and that has the effect of executing a program which sends a signal to mpd to pause the current song.


Solution



Following dirkt's idea of using /dev/input, I created ksd (keyboard shortcuts daemon) for this purpose. The application can be started in the background and will take over the keyboard wherever you are, including before login and within X.







share|improve this question

















  • 1




    readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
    – quixotic
    Jul 13 at 15:35










  • Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
    – roaima
    Jul 13 at 15:44











  • @roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
    – rid
    Jul 13 at 22:48






  • 2




    @quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
    – rid
    Jul 13 at 22:50










  • @rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
    – quixotic
    Jul 14 at 4:37












up vote
6
down vote

favorite
1









up vote
6
down vote

favorite
1






1





Is there any way of configuring keyboard shortcuts in the Linux virtual console?



For example, if I go to tty1 then press the key combination Ctrl+Alt+H, I would like the script /usr/bin/hello.sh to be executed.




Ideally, the shortcut would be available even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can also be a normal ELF binary or even a kernel module making system calls.




Example use cases



  1. I'm in the console and browsing the web with something like links and I want to turn down the screen brightness. I press Fn+End, which happens to be the brightness down key and produces a single keycode, and a program runs which reduces the brightness by writing something in a /sys file.

  2. I'm in a console text editor and listening to some music in the background that's being played by mpd. I press the ⏯ (play/pause) key, which again produces a single keycode, and that has the effect of executing a program which sends a signal to mpd to pause the current song.


Solution



Following dirkt's idea of using /dev/input, I created ksd (keyboard shortcuts daemon) for this purpose. The application can be started in the background and will take over the keyboard wherever you are, including before login and within X.







share|improve this question













Is there any way of configuring keyboard shortcuts in the Linux virtual console?



For example, if I go to tty1 then press the key combination Ctrl+Alt+H, I would like the script /usr/bin/hello.sh to be executed.




Ideally, the shortcut would be available even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can also be a normal ELF binary or even a kernel module making system calls.




Example use cases



  1. I'm in the console and browsing the web with something like links and I want to turn down the screen brightness. I press Fn+End, which happens to be the brightness down key and produces a single keycode, and a program runs which reduces the brightness by writing something in a /sys file.

  2. I'm in a console text editor and listening to some music in the background that's being played by mpd. I press the ⏯ (play/pause) key, which again produces a single keycode, and that has the effect of executing a program which sends a signal to mpd to pause the current song.


Solution



Following dirkt's idea of using /dev/input, I created ksd (keyboard shortcuts daemon) for this purpose. The application can be started in the background and will take over the keyboard wherever you are, including before login and within X.









share|improve this question












share|improve this question




share|improve this question








edited Jul 16 at 5:01
























asked Jul 11 at 8:47









rid

13017




13017







  • 1




    readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
    – quixotic
    Jul 13 at 15:35










  • Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
    – roaima
    Jul 13 at 15:44











  • @roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
    – rid
    Jul 13 at 22:48






  • 2




    @quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
    – rid
    Jul 13 at 22:50










  • @rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
    – quixotic
    Jul 14 at 4:37












  • 1




    readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
    – quixotic
    Jul 13 at 15:35










  • Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
    – roaima
    Jul 13 at 15:44











  • @roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
    – rid
    Jul 13 at 22:48






  • 2




    @quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
    – rid
    Jul 13 at 22:50










  • @rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
    – quixotic
    Jul 14 at 4:37







1




1




readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
– quixotic
Jul 13 at 15:35




readline macros might be what you're looking for, and once configured will work anywhere you use bash (or a similarly readline-capable shell) rather than only in the console. see wiki.archlinux.org/index.php/readline#Macros
– quixotic
Jul 13 at 15:35












Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
– roaima
Jul 13 at 15:44





Do you want the script to be executed invisibly, or the line /usr/bin/hello.sh to be entered into the keyboard buffer as if you'd typed it at the shell prompt?
– roaima
Jul 13 at 15:44













@roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
– rid
Jul 13 at 22:48




@roaima, I would like the script to be executed invisibly, ideally even before logging in (in which case it would be executed with the privileges of a user that I specify). I don't mind modifying the kernel either, if that's the only way of accomplishing this. Also, it doesn't have to be a shell script, it can be a normal ELF binary as well.
– rid
Jul 13 at 22:48




2




2




@quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
– rid
Jul 13 at 22:50




@quixotic, unfortunately that only works at the bash prompt. I would like the shortcut to be available regardless of what program I'm currently in.
– rid
Jul 13 at 22:50












@rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
– quixotic
Jul 14 at 4:37




@rid yes, that would be a limitation of a readline solution. i think it's the closest you'll find in existing implementations; i'm unaware of multitasking desktop-alike getty replacements.
– quixotic
Jul 14 at 4:37










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted
+50










Partial answer (because it's just an outline, and untested):



Write a demon which listens to whatever /dev/input device corresponds to your main keyboard (there are symlinks, look at them). Start that demon as the user you specify, using whatever init system you have (systemd, sysv, whatever).



The demon processes key events as defined in input-events-codes.h (or look at the source code of evtest). It has a state machine that recognizes your desired key sequences, and spawns whatever process you specify when such a sequence is complete.



This should be available before you login, and will always execute as the same user, no matter what user you are logged in at the virtual console. It will also execute under X, again as the same user.




Alternative, if you want to execute something in a shell: Use tmux or a similar program which can bind key sequences to actions. I suppose it should also be possible to automatically start tmux and attach to a new session whenever you log in at a virtual console, but I haven't looked into that.



This won't work before log in, but will also work in graphical terminal emulators that have keyboard focus, and will execute the script as the user which is logged in.






share|improve this answer























  • That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
    – rid
    Jul 16 at 4:58










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454645%2fkeyboard-shortcuts-in-the-virtual-terminal%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted
+50










Partial answer (because it's just an outline, and untested):



Write a demon which listens to whatever /dev/input device corresponds to your main keyboard (there are symlinks, look at them). Start that demon as the user you specify, using whatever init system you have (systemd, sysv, whatever).



The demon processes key events as defined in input-events-codes.h (or look at the source code of evtest). It has a state machine that recognizes your desired key sequences, and spawns whatever process you specify when such a sequence is complete.



This should be available before you login, and will always execute as the same user, no matter what user you are logged in at the virtual console. It will also execute under X, again as the same user.




Alternative, if you want to execute something in a shell: Use tmux or a similar program which can bind key sequences to actions. I suppose it should also be possible to automatically start tmux and attach to a new session whenever you log in at a virtual console, but I haven't looked into that.



This won't work before log in, but will also work in graphical terminal emulators that have keyboard focus, and will execute the script as the user which is logged in.






share|improve this answer























  • That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
    – rid
    Jul 16 at 4:58














up vote
2
down vote



accepted
+50










Partial answer (because it's just an outline, and untested):



Write a demon which listens to whatever /dev/input device corresponds to your main keyboard (there are symlinks, look at them). Start that demon as the user you specify, using whatever init system you have (systemd, sysv, whatever).



The demon processes key events as defined in input-events-codes.h (or look at the source code of evtest). It has a state machine that recognizes your desired key sequences, and spawns whatever process you specify when such a sequence is complete.



This should be available before you login, and will always execute as the same user, no matter what user you are logged in at the virtual console. It will also execute under X, again as the same user.




Alternative, if you want to execute something in a shell: Use tmux or a similar program which can bind key sequences to actions. I suppose it should also be possible to automatically start tmux and attach to a new session whenever you log in at a virtual console, but I haven't looked into that.



This won't work before log in, but will also work in graphical terminal emulators that have keyboard focus, and will execute the script as the user which is logged in.






share|improve this answer























  • That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
    – rid
    Jul 16 at 4:58












up vote
2
down vote



accepted
+50







up vote
2
down vote



accepted
+50




+50




Partial answer (because it's just an outline, and untested):



Write a demon which listens to whatever /dev/input device corresponds to your main keyboard (there are symlinks, look at them). Start that demon as the user you specify, using whatever init system you have (systemd, sysv, whatever).



The demon processes key events as defined in input-events-codes.h (or look at the source code of evtest). It has a state machine that recognizes your desired key sequences, and spawns whatever process you specify when such a sequence is complete.



This should be available before you login, and will always execute as the same user, no matter what user you are logged in at the virtual console. It will also execute under X, again as the same user.




Alternative, if you want to execute something in a shell: Use tmux or a similar program which can bind key sequences to actions. I suppose it should also be possible to automatically start tmux and attach to a new session whenever you log in at a virtual console, but I haven't looked into that.



This won't work before log in, but will also work in graphical terminal emulators that have keyboard focus, and will execute the script as the user which is logged in.






share|improve this answer















Partial answer (because it's just an outline, and untested):



Write a demon which listens to whatever /dev/input device corresponds to your main keyboard (there are symlinks, look at them). Start that demon as the user you specify, using whatever init system you have (systemd, sysv, whatever).



The demon processes key events as defined in input-events-codes.h (or look at the source code of evtest). It has a state machine that recognizes your desired key sequences, and spawns whatever process you specify when such a sequence is complete.



This should be available before you login, and will always execute as the same user, no matter what user you are logged in at the virtual console. It will also execute under X, again as the same user.




Alternative, if you want to execute something in a shell: Use tmux or a similar program which can bind key sequences to actions. I suppose it should also be possible to automatically start tmux and attach to a new session whenever you log in at a virtual console, but I haven't looked into that.



This won't work before log in, but will also work in graphical terminal emulators that have keyboard focus, and will execute the script as the user which is logged in.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jul 16 at 5:17


























answered Jul 14 at 7:49









dirkt

13.8k2930




13.8k2930











  • That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
    – rid
    Jul 16 at 4:58
















  • That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
    – rid
    Jul 16 at 4:58















That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
– rid
Jul 16 at 4:58




That's exactly what I was looking for, thank you. I created ksd (keyboard shortcuts daemon) using /dev/input and /dev/uinput.
– rid
Jul 16 at 4:58












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f454645%2fkeyboard-shortcuts-in-the-virtual-terminal%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay