0

I want to be able to set a keyboard shortcuts for buttons in an application I'm building. I'd like to be able to pass in the keyboard button code as a parameter to make it configurable. Here's what I have so far using the documentation before I got stuck. HTML:

<div ng-controller="BtnCtrl">
                <button class="primary-btn" type="submit" ng-keypress="press($event, '12')">Button</button>
            </div>

JavaScript:

angular.module('App')
.controller('BtnCtrl', function ($scope) {
    $scope.press = function($event, hotKeyRef) {
        if ($event.keyCode==hotKeyRef) {
             //need some code here to trigger the button press
        }
    }
});

So using my approach, I'm unsure of a) how to trigger the button press from inside the function and b) whether this is the correct way of passing in the keyCode data.

I might also be taking completely the wrong approach, so any other guidance would be appreciated.

Thanks

MDalt
  • 1,681
  • 2
  • 24
  • 46

2 Answers2

0

For the question a).

The main uses of a < button > html element is to fire an event on a click. So if you want to use a keypress, why use this element ? I don't really see what you want to achieve. that seems controversal.

for b) :

By default, ng-keypress is intended to be used in an input element.

Otherwise, it seems that some posts, where I inquired, manage to make it work out. You can see what it can look like, for example on this post (Is it possible to listen for arrow keyspress using ng-keypress?) in which the person trying to setup the konami code.

Moreover, it seems that you can have trouble depending on which browser (Chrome, Firefox, Safari, IE) you uses. Be careful.

I hope this could help you.

Community
  • 1
  • 1
Louis Lecocq
  • 1,764
  • 3
  • 17
  • 38
  • All helpful points. Thanks. With regards to a), we have users who will be using a keyboard to navigate through the screens, so we want the ability to assign keyboard shortcuts to some of the buttons. If ng-keypress is intended for input, is there another way of triggering the button press? – MDalt Oct 12 '15 at 10:35
0

hi there is an excellent plugin for your scenario u can check the below link

https://github.com/chieffancypants/angular-hotkeys/

u can also check the below stackoverflow link

What is AngularJS way to create global keyboard shortcuts?

Community
  • 1
  • 1
Avinash Solanki
  • 1,091
  • 1
  • 10
  • 19