For your first question, see plunkr below.
If you pass $ event through the ng-click function, you can access the event in your controller. In my example, I checked if altKey was true, which checks if the Alt key was pressed at the same time as the button was pressed. You can also access ctrlKey, shiftKey and the mouse button pressed. See here the MouseEvent object - http://www.w3schools.com/jsref/dom_obj_event.asp
Controller:
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
When logic starts to get a little more complicated, it would be best to do this in a directive.
In the second question, the main parts can be seen in the following 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 listen to the document for keystrokes and again check for altKey. Then, if keyCode is one of our hotkeys, we send a message to $ rootScope using $ rootScope. $ Broadcast (), which the controller listens using the $ rootScope method. $ On ().
In the above example, alt + s will add more identifiers, and alt + a will remove them to the originally selected one.