To find the answer to your initial query, refer to the plunkr provided below.
If you include $event in your ng-click function, you can retrieve the event in your controller. In my demonstration, I verified if altKey was true, indicating whether the alt key was pressed simultaneously with the click. Additionally, ctrlKey, shiftKey, and the specific mouse button pressed are accessible. Refer to the MouseEvent object documentation here - http://www.w3schools.com/jsref/dom_obj_event.asp
The controller setup is as follows:
angular.module('exampleApp', [])
.controller("ItemCtrl", function($scope){
$scope.items = [
{text: "Bob", id: 1},
{text: "Alice", id: 2},
{text: "Frank", id: 3},
{text: "Lisa", id: 4}
];
$scope.itemList = [];
$scope.addItemIdToList = function(event, item){
if(event.altKey){
if(isItemInList(item)){
removeItemIdFromList(item);
} else {
addItemIdToList(item);
}
} else {
addItemIdAsSingleSelection(item);
}
};
var isItemInList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
return indexOfItem > -1;
}
var removeItemIdFromList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
$scope.itemList.splice(indexOfItem, 1);
};
var addItemIdToList = function(item){
$scope.itemList.push(item.id);
};
var addItemIdAsSingleSelection = function(item){
$scope.itemList = [item.id];
};
})
http://plnkr.co/edit/RAX5oxkTomXxryp0sNNc
If the logic starts getting more intricate, employing a directive would be advisable.
For addressing the second question, foundational components are exhibited in the proceeding example:
angular.module('exampleApp', [])
.directive('keypressEvents', function ($document, $rootScope) {
return {
restrict: 'E',
link: function () {
console.log('linked');
$document.on('keypress', function(e) {
if(e.altKey){
var s = 223;
var a = 229;
if(e.which == s){
$rootScope.$broadcast("add_next_id");
} else if(e.which == a){
$rootScope.$broadcast("remove_last_id");
}
}
})
}
}
})
.controller("ItemCtrl", function($scope, $rootScope){
$scope.items = [
{text: "Bob", id: 1},
{text: "Alice", id: 2},
{text: "Frank", id: 3},
{text: "Lisa", id: 4}
];
$scope.itemList = [1];
$rootScope.$on('add_next_id', function (evt, obj, key) {
$scope.$apply(function () {
addNextId();
});
});
$rootScope.$on('remove_last_id', function (evt, obj, key) {
$scope.$apply(function () {
removeLastId();
});
});
var addNextId = function(){
var lastId = $scope.itemList[$scope.itemList.length - 1];
if(lastId < $scope.items.length){
$scope.itemList.push(lastId+1);
}
};
var removeLastId = function(){
if($scope.itemList.length > 1){
$scope.itemList.pop();
}
};
$scope.isItemInList = function(item){
var indexOfItem = $scope.itemList.indexOf(item.id);
return indexOfItem > -1;
}
})
http://plnkr.co/edit/PyyjfRMovygeq9qNbzWo
We are monitoring key presses on the document and rechecking for altKey. Subsequently, if the keyCode corresponds to our defined hotkeys, we forward a signal to $rootScope using $rootScope.$broadcast(), which the controller listens to via $rootScope.$on().
In this scenario, pressing alt+s will append additional ids, whereas alt+a will reduce them back to the initially selected one.