spyder.api.shortcuts#

Helper classes to get and set keyboard 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.

__init__() None[source]#

Helper 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 | None, 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 | None, optional) – Name of the plugin where the shortcut is defined. Defaults to the context name; necessary for third-party plugins that have shortcuts with a context different from the plugin name.

Returns:

shortcut – Key sequence of the shortcut, e.g. "Ctrl+Enter".

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) None[source]#

Set a shortcut sequence with a given name and context.

Parameters:
  • shortcut (str) – Key sequence of the shortcut, e.g. "Ctrl+Enter".

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

  • context (str | None, 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 | None, optional) – Name of the plugin where the shortcut is defined. Defaults to the context name; necessary for third-party plugins that have shortcuts with a context different from the plugin name.

Return type:

None

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) 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) to 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 | None, 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 | None, optional) – Name of the plugin where the shortcut is defined. Defaults to the context name; necessary for third-party plugins that have shortcuts with a context different from the plugin name.

Return type:

None