How do you make a button that performs a specific command?

this can be achieved by creating gnome-shell-extension.

Tested in gnome-shell --version 3.38.1

Run below commands one by one to create required files.

mkdir -p $HOME/.local/share/gnome-shell/extensions/KBD
touch $HOME/.local/share/gnome-shell/extensions/KBD/extension.js
touch $HOME/.local/share/gnome-shell/extensions/KBD/metadata.json

extension.js file contents, copy paste in above created extension.js file. Notice the 11th line, Util.spawnCommandLine("bash /home/admin/mykbd.sh") replace /home/admin/mykbd.sh with path of your script that toggles what you are looking for.

'use strict';

const St = imports.gi.St;

const Main = imports.ui.main;
const Util = imports.misc.util;

let button;

function _myKBD () {
Util.spawnCommandLine("bash /home/admin/mykbd.sh")
}

function init() {
    button = new St.Bin({ style_class: 'panel-button',
                          reactive: true,
                          can_focus: true,
                          track_hover: true });
                          
    let icon = new St.Icon ({ icon_name: 'input-keyboard-symbolic',
                      style_class: 'system-status-icon' });
    button.set_child(icon);
    button.connect('button-press-event', _myKBD);
}

function enable() {
        Main.panel._rightBox.insert_child_at_index(button, 0);
}

function disable() {
        Main.panel._rightBox.remove_child(button);
}

metadata.json file contents, copy paste in above created metadata.json file.

{
  "name": "KBD",
  "description": "KBD",
  "uuid": "KBD",
  "shell-version": [
    "3.36"
  ]
}

once you are done with copy pasting in the two files. Refresh the gnome-shell with Alt+F2 r Enter method or logout and login.

Then to enable the extension, run below command.

gnome-extensions enable KBD

Again Refresh the gnome-shell with Alt+F2 r Enter method or logout and login.

Now you will see the keyboard button at the top right corner. when you click it, it runs the script you prepared.

enter image description here

Please note that, extensions are disabled when screen is locked and enabled once unlocked. So you wont be able to click this button on lock screen.


A very elegant solution has been provided in this answer by UnKNOWn, but it is rather technical. For completeness, I would want to add a more straightforward method using desktop launchers, yet in this particular case, I would go for the approach of UnKNOWn.

You can at any time create a custom desktop launcher that runs your command or script. Placed in an appropriate location, it will appear in the Applications overview, from where you can pin it to the dash. In Ubuntu, you also could place it on your desktop to launch it from there.

A desktop launcher is a text file with the .desktop extension, formatted in a specific way. If you place it in the folder .local/share/applications, it will be picked up in the application overview.

Thus, you could create the file ~/.local/share/applications/toggle-keyboard.desktop to launch your script. The contents should look like:

[Desktop Entry]
Name=Toggle Keyboard
Comment=Toggle keyboard on or off
Exec=<name or path to your script>
Terminal=false
Type=Application
Icon=<name or path to the icon >
Categories=Utility;