Skip to content

Instantly share code, notes, and snippets.

@jimthedev
Created April 13, 2020 17:59
Show Gist options
  • Save jimthedev/70ff81a2d3ab1e74b953ea7b83b747d9 to your computer and use it in GitHub Desktop.
Save jimthedev/70ff81a2d3ab1e74b953ea7b83b747d9 to your computer and use it in GitHub Desktop.
onKeyCombo directive angular.js angular1
const angular = require('angular');
function OnKeyComboDirective () {
return function (scope, element, attributes) {
// We're using a custom event listener on keyup
const comboListener = e => {
if ((e.ctrlKey || e.metaKey) && e.shiftKey && e.which == 65) {
setTimeout(function () {
scope.$eval(attributes.onKeycombo);
});
}
}
// Register the listener
document.addEventListener('keydown', comboListener);
// Clean up to prevent memory leaks
scope.$on('$destroy', function(){
document.removeEventListener('keydown', comboListener);
});
};
}
export default angular
.module('spsng.components.on-keycombo', [])
.directive('onKeycombo', OnKeyComboDirective)
.name;
// usage
/*
IMPORT THE DIRECTIVE:
import OnKeycomboModule from './on-keycombo/on-keycombo';
export default angular
.module('my-app', [ScrollIfModule])
// My app's controllers and directives here
.name;
In template:
on-keycombo="ctrl.handleCombo($event)"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment