Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Feat: Add KeyCode Event Binding Directive #1953

Closed
@TheLarkInn

Description

@TheLarkInn

It would be great if there was a directive that would allow a user to bind a keypress event to specified key code and key code modifiers.

What exists currently on the web/ng:

  • Currently ng-keypress exists, but if you only need to build out more logic to watch which key codes are triggered.
  • On the opposite side of the spectrum there are simple and very handy directives such as seen below which allow you to bind a single keycode which evaluates the expression provided in the html:
angular.module("core").directive('hnEnterKey', function() {
    return function(scope, element, attrs) {
        element.bind("keydown keypress", function(event) {
            var keyCode = event.which || event.keyCode;
            if (keyCode === 13) {
                scope.$apply(function() {
                    scope.$eval(attrs.hnEnterKey);
                });

                event.preventDefault();
            }
        });
    };
});

I believe we could make something in the middle by allowing a user to add a directive, supply a key code and/or key code+modifiers, and an expression to evaluate.

Seen below is a modest Example:

<div class="container-demo">
   <input md-keycode="submitForm();" key-code="13" type="text" />

   <button>Cancel</button>
   <button>Submit</button>
</div>

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions