[GH #753] Open context menu through the shortcutManager

Tags: #<Tag:0x00007fce69339520> #<Tag:0x00007fce69339368>

Hello, I’m trying to open the contextMenu by using the shortcutManager. My goal would be to allow the use of the ContextMenu key, but I can’t find the method to open it.
Thank you for any help!

Here is my code :

   componentDidMount() {
       console.log('EditorTable: componentDidMount');
       const hot = this.getHotInstance();
       const gridContext = hot.getShortcutManager().getContext('grid');

       gridContext.addShortcut({
         group: 'group_ID',
         runOnlyIf: () => hot.getSelected() !== void 0,
         keys: [['n']],
         callback: () => {
           // TODO
         },
       });
       this.listenToElectronEvents();
     }

Hi @maxz57

Thank you for contacting us. Here’s a method that will open the contextMenu programatically: https://handsontable.com/docs/javascript-data-grid/api/context-menu/#open

So the whole logic would look like this:

hot.getPlugin('contextMenu').open()

Thank you for getting back to me. The following command as such throw the following error :
Uncaught TypeError: Cannot read properties of undefined (reading 'pageY')
I tried passing different arguments for its location such as 0,0 or hot.getSelectedLast() but it keeps opening the context menu below the last cell. Is there any way around that ? (I’m using handsontable 12.1.3 by the way and can’t upgrade the package to newer versions due to needs of my project)

Hi @maxz57

Yes, the event or coordinates needs to be passed to make it work. However, while preparing a new example I noticed that there is an issue with re-positioning the contextMenu and it turned it that it’s a bug. We have it already reported internally.

Currently, we also work on Accessibility for Handsontable, and one of the features will be the possibility to open the contextMenu through default keyboard shortcut so that will also fix the re-positioning issue. I’ll let you know once we fix it.

For now it is possible to open the contextMenu by a custom shortcut, but due to the issues mentioned above it will always open below the table. Here’s an example: https://jsfiddle.net/handsoncode/t6ynp4w1/

Thank you for your answer, this is good to know that there’s an issue yet.
Thanks again for your time and investigations.