What is AngularJS way to create global keyboard shortcuts?

后端 未结 12 2135
一整个雨季
一整个雨季 2020-11-30 22:04

I suppose that I should use directive, but it seems strange to add directive to body, but listen events on document.

What is a proper way to do this?

UPDATE:

12条回答
  •  青春惊慌失措
    2020-11-30 22:46

    Here's an example of an AngularJS service for keyboard shortcuts: http://jsfiddle.net/firehist/nzUBg/

    It can then be used like this:

    function MyController($scope, $timeout, keyboardManager) {
        // Bind ctrl+shift+d
        keyboardManager.bind('ctrl+shift+d', function() {
            console.log('Callback ctrl+shift+d');
        });
    }
    

    Update: I'm now using angular-hotkeys instead.

提交回复
热议问题