angular-hotkeys icon indicating copy to clipboard operation
angular-hotkeys copied to clipboard

Cannot add multiple callbacks to the same combo.

Open vicary opened this issue 8 years ago • 8 comments

Not sure if feature or bug, consider this script.

app.controller('ParentCtrl', function($scope, hotkeys) {
  hotkeys.bindTo($scope).add({
    combo: 'enter',
    allowIn: ['INPUT', 'TEXTAREA'],
    callback: function($evt) {
      $evt.preventDefault();
      logKeypress($evt);
    }
  });
});

app.controller('ChildCtrl1', function($scope, hotkeys) {
  hotkeys.bindTo($scope).add({
    combo: 'enter',
    allowIn: ['INPUT', 'TEXTAREA'],
    callback: function($evt) {
      // Do something else ...
    }
  });
});

I wanted to override the default browser submit action and add custom actions for the ENTER key for each ng-view controller, but when ChildCtrl1 is in action, callback in ParentCtrl won't fire.

vicary avatar Oct 12 '15 04:10 vicary

Can we modify this line hotkeys.js:1469 to allow multiple bindings?

I tried to comment it out and it works on Hotkey.add(), while bindTo() already took care of unbinding with _del(), it turns out to be kind of a Mousetrap design flaw because Mousetrap.unbind() does not support a callback argument.

vicary avatar Oct 12 '15 05:10 vicary

Similar problem, but for other reasons what does not work anylonger since 1.6.

I'm trying to add behaviour when there is the ctrl key pressed, unfortunately it does not work in every browser, therefore the need to add multiple duplicate combos.

    hotKeyObj.add({
                combo: 'ctrl',
                action: 'keypress',
                callback: function (event, hotkey) {
                    keyStatusMap.ctrlPressed = true;
                }
            }).add({
                combo: 'ctrl',
                action: 'keydown',
                description: gettextCatalog.getString('Hightlight mode'),
                callback: function (event, hotkey) {
                    keyStatusMap.ctrlPressed = true;
                }
            }).add({
                combo: 'ctrl',
                action: 'keyup',
                callback: function (event, hotkey) {
                    keyStatusMap.ctrlPressed = false;
                }
            });

ceilino avatar Dec 01 '15 12:12 ceilino

Having the same problem as @ceilino described. I have assigned different callbacks to keyup and keydown of the same combo, and it no longer works.

jonathan-reisdorf avatar Jan 14 '16 14:01 jonathan-reisdorf

... and this is the reason: https://github.com/chieffancypants/angular-hotkeys/commit/a3b4b4c233abca579d186543b46c011580a42f65#diff-30ea385d3977d7c1a62acdb050ea857aR338

jonathan-reisdorf avatar Jan 14 '16 14:01 jonathan-reisdorf

+1

jayharvey avatar Feb 13 '16 02:02 jayharvey

+1

cmacdonnacha avatar May 04 '16 09:05 cmacdonnacha

Some great people made a PR on a dependency mousetrap-js/mousetrap/pull/12, we can make use of the new parameters here.

vicary avatar Jun 23 '16 07:06 vicary

+1

albertodarblade avatar Jun 05 '18 21:06 albertodarblade