napari icon indicating copy to clipboard operation
napari copied to clipboard

Overwriting keybindings does not work anymore

Open constantinpape opened this issue 1 year ago • 2 comments

🐛 Bug Report

Overwriting the keybindings for a napari viewer via @viewer.bind_key(..., overwrite=True) does not work..

💡 Steps to Reproduce

Here is a small example to reproduce this:

Add custom keybinding that clashes with existing keybinding:

import napari

v = napari.Viewer()
v.add_shapes()

# 's' clashes with the keybindings for selection of the
# shape layer, but should be overwritten due to overwrite=True
@v.bind_key("s", overwrite=True)
def hello_world():
    print("Hello workd")

napari.run()

Run the code and press 's':

https://github.com/user-attachments/assets/e1b26e4a-f45b-4ffe-91af-67fd574d09ce

as you see in the video, the keybinding is not registered and the selection in the shape layer is still done when pressing s.

💡 Expected Behavior

Keybindings are overwritten and pressing s prints to terminal in the example from above instead of switching to selection mode in the shape layer.

🌎 Environment

napari: 0.5.3 Platform: Linux-5.15.0-122-generic-x86_64-with-glibc2.31 System: Ubuntu 20.04.6 LTS Python: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0] Qt: 5.15.8 PyQt5: 5.15.9 NumPy: 1.26.4 SciPy: 1.14.1 Dask: 2024.8.2 VisPy: 0.14.3 magicgui: 0.8.3 superqt: 0.6.7 in-n-out: 0.2.1 app-model: 0.2.8 npe2: 0.7.7

OpenGL:

  • GL version: 4.6 (Compatibility Profile) Mesa 21.2.6
  • MAX_TEXTURE_SIZE: 16384
  • GL_MAX_3D_TEXTURE_SIZE: 2048

Screens:

  • screen 1: resolution 1920x1200, scale 1.0

Optional:

  • numba: 0.60.0
  • triangle not installed
  • napari-plugin-manager: 0.1.0

Settings path:

  • /home/pape/.config/napari/main_b5dbeec9e39599520f77f7a4048f1bb2ff39ddbe/settings.yaml Plugins:
  • micro-sam: 1.0.1 (24 contributions)
  • napari: 0.5.3 (81 contributions)
  • napari-console: 0.0.9 (0 contributions)
  • napari-skimage-regionprops: 0.10.1 (2 contributions)
  • napari-svg: 0.2.0 (2 contributions)
  • napari-tools-menu: 0.1.19 (0 contributions)
  • skan: 0.11.1 (2 contributions)

💡 Additional Context

This used to work in napari < 0.5.

constantinpape avatar Sep 29 '24 11:09 constantinpape

@constantinpape Sorry about that! It's a known issue and not actually 0.5.0 related? See, for example https://github.com/napari/napari/issues/6440

Anyhow, you can work around this by not using a viewer keybinding with @v.bind_key but a layer specific one. E.g.:

import napari

viewer = napari.Viewer()
s = viewer.add_shapes()

@s.bind_key('s', overwrite=True)
def hello_world(event):
     print('hello world')

napari.run()

That will work for that specific shapes layer. You can also do from napari.layers import Shapes and use @Shapes.bind_key

Hope that works for you?

FYI We have plans to change keybinding behavior, see: https://github.com/napari/napari/issues/7059 and we have a discussion/feedback here: https://github.com/napari/napari/issues/7056

psobolewskiPhD avatar Sep 29 '24 19:09 psobolewskiPhD

Hi @psobolewskiPhD and thanks for the quick feedback.

It's a known issue and not actually 0.5.0 related? See, for example #6440

I see. Then I assume that we only ran into this issue with 0.5.0 because it added new keybindings that clashed with ours.

Anyhow, you can work around this by not using a viewer keybinding with @v.bind_key but a layer specific one. E.g.:

import napari

viewer = napari.Viewer()
s = viewer.add_shapes()

@s.bind_key('s', overwrite=True)
def hello_world(event):
     print('hello world')

napari.run()

That will work for that specific shapes layer. You can also do from napari.layers import Shapes and use @Shapes.bind_key

Hope that works for you?

Ok, we can probably work around with this, but it's a bit hacky, since our keybindings are global and should be independent of the layer being selected. So I guess we would need to add the keybinding twice, once for the layer and once for the viewer. We'd also need to do some hacky things to get access to the viewer instance in the keybindings of the layer.

FYI We have plans to change keybinding behavior, see: #7059 and we have a discussion/feedback here: #7056

Thanks for the pointer!

constantinpape avatar Sep 30 '24 07:09 constantinpape