Gnome Shell Extension Key Binding

How to handle keyboard events in gnome shell extensions?

I came across this snippet in gcampax's gtk-js-app template some time ago, which may be related to what you're doing:

// Due to limitations of gobject-introspection wrt GdkEvent and GdkEventKey,
// this needs to be a signal handler
this.connect('key-press-event', Lang.bind(this, this._handleKeyPress));

and

_handleKeyPress: function(self, event) {
return this.main_search_bar.handle_event(event);
},

I haven't had a need to use keyboard events yet, and this is Gtk in GJS, but the same limitation may be affecting gnome-shell extensions.

UPDATE

I've been doing some keybinding stuff lately, and if attaching a signal handler to the global object is working, you can do something like this:

global.display.connect("key-press-event", (widget, event, user_data) => {
let [success, keyval] = event.get_keyval(); // integer
let keyname = Gdk.keyval_name(keyval); // string keyname

if (keyname === "Control_L") {
// Dialog code or eg. this.keys_array.push("<Ctrl>");
}
});

There's also some Shell keybinding code here and some shell-global documentation here that might give you more clues. Wish I could help more but I'm wrestling my own GJS atm ;)

ADDENDUM

There is a good answer here with an example class with informative logging, as well as a speculative explanation. I've also found this functionality is exposed over DBus which might be more convenient in some cases:

Bus Name: org.gnome.Shell -> Path: /org/gnome/Shell -> Interface: org.gnome.Shell

Relevant Methods:

  • GrabAccelerator(String accelerator, UInt32 flags) -> (UInt32 action)
  • UngrabAccelerator(UInt32 action) -> (Boolean success)

Signal:

  • AcceleratorActivate(UInt32, Dict of {String, Variant})

In Gnome (gnome-shell) can you set multiple shortcut keys instead of just a single?

Yes, you can. This is verified to work on Ubuntu 20.10 and 21.04RC.

The following commands acheive the requested outcome:

gsettings set org.gnome.desktop.wm.keybindings switch-windows-backward "['<Shift><Super>Tab', '<Shift><Alt>Tab']"
gsettings set org.gnome.desktop.wm.keybindings switch-windows "['<Super>Tab', '<Alt>Tab']"

For more options see here:

# list all keybindings
gsettings list-recursively | grep -e org.gnome.desktop.wm.keybindings -e org.gnome.settings-daemon.plugins.media-keys -e org.gnome.settings-daemon.plugins.power | sort

# confirm no other keybinding conflicts
gsettings list-recursively | grep '<Control>1'

# set multiple keybindings for "Switch to Workspace 1"
gsettings set org.gnome.desktop.wm.keybindings switch-to-workspace-1 "['<Super>Home', '<Control>1']"

# confirm value is set correctly
gsettings get org.gnome.desktop.wm.keybindings switch-to-workspace-1

from here.

Create a GNOME shell extension that enables and disables the webcam

You should probably start by looking through the tutorial on the GNOME Wiki:

https://wiki.gnome.org/Projects/GnomeShell/Extensions/Writing

When spawning your command, you're probably going to want to use pkexec instead of sudo. I wouldn't recommend doing anything requiring superuser access in an extension, but pkexec will at least work properly in a GUI environment.

To spawn your command, you can probably get away with GLib.spawn_command_line_async(), although I always prefer GSubprocess myself.

gnome-shell-extensions Not able to upload the complete directory

These files must be in the top-level of the Zip:

  • metadata.json
  • extension.js
  • prefs.js (Optional file)
  • stylesheet.css (Optional file)

That is the only requirement. The -r and -j functions are explained by zip --help:

-j junk (don't record) directory names

-r recurse into directories

If all your extension files are in one, top-level directory then -j will work, otherwise it will probably break your directory hierarchy. You can check the layout of a zip with unzip -l.

Typically you will zip with zip -r extension@domain.zip [path with metadata.json]:

$ ls
extensions.js metadata.json

$ zip -r extension@domain.zip .
adding: metadata.json (deflated 33%)
adding: extension.js (deflated 55%)

$ unzip -l extension@domain.zip
Archive: extension@domain.zip
Length Date Time Name
--------- ---------- ----- ----
194 04-25-2020 17:47 metadata.json
864 04-25-2020 17:47 extension.js
--------- -------
1058 2 files


Related Topics



Leave a reply



Submit