Angular Menu - Keyboard Support

An end user can use the following keys to interact with the UI component.

Key Action
Closes the submenu and focuses on its parent item.
Opens the submenu of the focused item.
Home Sets focus to the first item in the menu/submenu.
End Sets focus to the last item in the menu/submenu.
Space Switches the focused item to the selected state.
Enter Chooses the focused item.

You can implement a custom handler for a key using the registerKeyHandler(key, handler) method.

jQuery
JavaScript
function registerKeyHandlers () {
    const menu = $("#menuContainer").dxMenu("instance");
    menu.registerKeyHandler("backspace", function(e) {
        // The argument "e" contains information on the event
    });
    menu.registerKeyHandler("space", function(e) {
        // ...
    });
}
Angular
TypeScript
import { ..., ViewChild, AfterViewInit } from '@angular/core';
import { DxMenuModule, DxMenuComponent } from 'devextreme-angular';
// ...
export class AppComponent implements AfterViewInit {
    @ViewChild(DxMenuComponent, { static: false }) menu: DxMenuComponent;
    // Prior to Angular 8
    // @ViewChild(DxMenuComponent) menu: DxMenuComponent;

    ngAfterViewInit () {
        this.menu.instance.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.menu.instance.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxMenuModule
    ],
    // ...
})
Vue
<template>
    <DxMenu :ref="myMenuRef" />
</template>
<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import DxMenu from 'devextreme-vue/menu';

const myMenuRef = 'my-menu';

export default {
    components: {
        DxMenu
    },
    data() {
        return {
            myMenuRef
        }
    },
    computed: {
        menu: function() {
            return this.$refs[myMenuRef].instance;
        }
    },
    mounted: function() {
        this.menu.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.menu.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { Menu } from 'devextreme-react/menu';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.menuRef = React.createRef();
    }

    render() {
        return (
            <Menu ref={this.menuRef} />
        );
    }

    get menu() {
        return this.menuRef.current.instance;
    }

    componentDidMount() {
        this.menu.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.menu.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}

export default App;
See Also