10

How do I create a button on my taskbar along with the time and stuff that performs a specific task? I wanted to be able to disable and enable my keyboard with the help of a keyboard shortcut, but I realized that once I disabled my keyboard with a shortcut, I wouldn't be able to use the keyboard to enable it again. So, I want a button on my taskbar that does this for me with the click of a button, thus not requiring my keyboard.

Thanks.

4
  • So you already have a command to toggle that?
    – UnKNOWn
    Nov 4 '20 at 1:23
  • I found some command on some website to disable my keyboard. I attached a keyboard shortcut to this command. That's all I've done. And I also have a command to enable the keyboard back up, which I obviously can't bind to a shortcut, so I want to bind it to a button. Nov 4 '20 at 2:08
  • Ok, thats fine.. I am trying it now. Will get back to you, so that you can put what ever command/script you want in place.
    – UnKNOWn
    Nov 4 '20 at 2:14
  • I think it would be possible to do this with keyboard shortcuts. It would require some coding though.
    – danzel
    Nov 5 '20 at 14:51
18

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.

2
  • 1
    That’s a nice wallpaper you have ! Looks a bit like The Long Dark… :)
    – breversa
    Nov 4 '20 at 12:09
  • 1
    @breversa that is Lake Louise in Banff National Park, located in Alberta Canada Nov 5 '20 at 2:44
13

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;
4
  • ok so i placed it in ~/.local.... now what? Exec should have the path of the text file with the command right? I gave the path of a text file with the extension .sh which had the command. Nov 5 '20 at 4:37
  • Script should be set executable. The launcher will only appear in Applications overview if the Exec line points to a valid executable
    – vanadium
    Nov 5 '20 at 9:08
  • This is definitely the easy way to do this. I use it in Ubuntu/KDE.
    – Joe
    Nov 6 '20 at 7:58
  • i found an easier way: FIrst I switched to CInnamon environment, which was already installed, then I created a desktop launcher there as it has default option there(just by right clicking and then I switched back to Gnome. Nov 11 '20 at 1:29
5

A bit of a nuclear suggestion, but other optional desktops like MATE or Metacity (and probably others), still offer the "Create Custom Launcher" option through a mouse-click on the panel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.