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

Can't add separate 'keydown' and 'keyup' actions for the same key

Open mathiasm74 opened this issue 10 years ago • 7 comments

The code below should print both DOWN and UP, but only prints UP.

  hotkeys.bindTo($scope).add({
    combo: 'a', action: 'keydown', callback: function() { 
      console.log("DOWN");
    },
    combo: 'a', action: 'keyup', callback: function() { 
      console.log("UP");
    }
  });

mathiasm74 avatar Jun 03 '15 14:06 mathiasm74

seems like a bug in Mousetrap.

I opened https://github.com/ccampbell/mousetrap/issues/284

ofirdagan avatar Jul 05 '15 13:07 ofirdagan

+1 .. would be great for things like multiple select:

    hotkeys.bindTo($scope).add({
      combo:       'command',
      description: 'Multiple Select on',
      action:      'keydown',
      callback:    function(event, hotkey) {
        console.log('cmd down');
        $scope.multipleSelect = true;
      }
    }).add({
      combo:       'command',
      description: 'Multiple Select off',
      action:      'keyup',
      callback:    function(event, hotkey) {
        console.log('cmd up');
        $scope.multipleSelect = false;
      }
    });

maybe related to https://github.com/ccampbell/mousetrap/issues/304

felixroos avatar Oct 13 '15 08:10 felixroos

I smell something when it comes to key sequence (not combos) in Mousetrap, not gonna dive into their code again but it could possibly fire the event twice. Not sure but worth a check.

vicary avatar Oct 14 '15 06:10 vicary

This works fine in Mousetrap when angular-hotkeys is taken out of the equation, as you can see here.

The actual issue was introduced in this pull request:

        // no duplicates please
        _del(combo);

This deletes all previously assigned combinations matching the current one being binded, thus removing others even if they have a different action.

devonzara avatar Oct 27 '15 01:10 devonzara

@devonzara A great example of where we should use the callback reference to search for dedup, instead of removing everything previously assigned.

vicary avatar Oct 27 '15 07:10 vicary

That, or just pass the action as a second parameter through to the Mousetrap method as I've done here. :)

devonzara avatar Oct 27 '15 09:10 devonzara

It actually works if I add the 'keyup' action before I add the 'keydown' action.

ntrpnr avatar Oct 03 '16 20:10 ntrpnr