I have the following $broadcast
to catch keypresses in Angular:
$document.bind('keypress', function(event) {
var key = event.which;
$rootScope.$broadcast('keypress', event);
$rootScope.$broadcast('keypress:' + key, event);
});
and I listen with $on
However, I want to detect when two keys are pressed at the same time, say enter
and s
are pressed at the same time (not a combination one one followed by another).
What is the best way of doing this?
EDIT
What I was thinking was to have:
var keys = [];
$document.bind('keydown', function(event) {
keys[event.which] = true;
});
$document.bind('keyup', function(event) {
delete keys[event.which];
});
$document.bind('keypress', function(event) {
var key = event.which;
var keysPressed = [];
angular.forEach(keys, function(value, key) {
keysPressed += 'keypress:' + key;
});
$rootScope.$broadcast('keypress', event);
$rootScope.$broadcast(keysPressed, event);
});
So if I have multiple keypresses, then I create the correct $broadcast
. The problem, however, becomes that the order matters now (i.e., if I press a
then enter
, then the $broadcast
is keypress:58keypress:13
and if I press the other way, I get keypress:13keypress:58
)