spyder.api.shortcuts#

Helper classes to get and set shortcuts in Spyder.

Classes

SpyderShortcutsMixin()

Provide methods to get, set and register shortcuts for widgets.

class spyder.api.shortcuts.SpyderShortcutsMixin[source]#

Bases: SpyderConfigurationObserver

Provide methods to get, set and register shortcuts for widgets.

get_shortcut(name: str, context: str | None = None, plugin_name: str | None = None) str[source]#

Get a shortcut sequence stored under the given name and context.

Parameters:
  • name (str) – The shortcut name (e.g. “run cell”).

  • context (str, optional) – Name of the shortcut context, e.g. “editor” for shortcuts that have effect when the Editor is focused or “_” for global shortcuts. If not set, the widget’s CONF_SECTION will be used as context.

  • plugin_name (str, optional) – Name of the plugin where the shortcut is defined. This is necessary for third-party plugins that have shortcuts with a context different from the plugin name.

Returns:

shortcut – Key sequence of the shortcut.

Return type:

str

Raises:

configparser.NoOptionError – If the shortcut does not exist in the configuration.

set_shortcut(shortcut: str, name: str, context: str | None = None, plugin_name: str | None = None)[source]#

Set a shortcut sequence with a given name and context.

Parameters:
  • shortcut (str) – Key sequence of the shortcut.

  • name (str) – The shortcut name (e.g. “run cell”).

  • context (str, optional) – Name of the shortcut context, e.g. “editor” for shortcuts that have effect when the Editor is focused or “_” for global shortcuts. If not set, the widget’s CONF_SECTION will be used as context.

  • plugin_name (str, optional) – Name of the plugin where the shortcut is defined. This is necessary for third-party plugins that have shortcuts with a context different from the plugin name.

Raises:

configparser.NoOptionError – If the shortcut does not exist in the configuration.

register_shortcut_for_widget(name: str, triggered: Callable, widget: QWidget | None = None, context: str | None = None, plugin_name: str | None = None)[source]#

Register a shortcut for a widget that inherits this mixin.

Parameters:
  • name (str) – The shortcut name (e.g. “run cell”).

  • triggered (Callable) – Callable (i.e. function or method) that will be triggered by the shortcut.

  • widget (QWidget, optional) – Widget to which this shortcut will be registered. If not set, the widget that calls this method will be used.

  • context (str, optional) – Name of the shortcut context, e.g. “editor” for shortcuts that have effect when the Editor is focused or “_” for global shortcuts. If not set, the widget’s CONF_SECTION will be used as context.

  • plugin_name (str, optional) – Name of the plugin where the shortcut is defined. This is necessary for third-party plugins that have shortcuts with a context different from the plugin name.