Angular -ui global key bindings

I started using angular -ui keypress module and wondered if there is a way to make global keyboard shortcuts that work no matter where I am placed in the body.

I tried to associate my ui-keydown with the body, but since it is out of focus, key events do not fire.

eg:

<body ui-keydown="{'pageup':'nav_to($event, \'users\')'}"> 

I know that I can just focus the div and bind the key bindings to this, but what happens when I have a form and I want to access all the global key bindings in each field?

+6
source share
2 answers

Try in the main application controller:

  angular.element($window).on('keydown', function(e) { console.log(e); }); 
+16
source

You can create a controller and install it on the body tag, as well as set a key event callback:

 <body ng-controller="keycontroller" ui-keyup="{'enter':'callback($event)'}" > <input type="text" ng-model="test1" /> <input type="text" ng-model="test2" /> </body> 

And then install:

 function keycontroller($scope) { $scope.test1= "It should work here..."; $scope.test2= "...and also here."; $scope.callback = function fn($event) { console.log("Enter key pressed"); }; } var app = angular.module("app", ['ui.keypress']); app.controller("keycontroller", keycontroller); 
+2
source

Source: https://habr.com/ru/post/954542/


All Articles