Eric Cherin
Eric Cherin

Reputation: 233

dart angular 2 respond to keydown event anywhere on page

I want to be able to control a view using the keyboard. How do you make the document accept keyboard input. I tried to do document.keydown, but that did not work.

I was able to get this working in my component class:

  @HostListener('click', const ['\$event'])
  void onClick(event) {
    x++;
  }

This adds one to x when I click directly on the div. I want to be able to press a key and call a function.

Upvotes: 0

Views: 566

Answers (1)

Hadrien Lejard
Hadrien Lejard

Reputation: 5904

you should take a look at this https://webdev.dartlang.org/angular/guide/user-input

But with your current code I would do something like that

@HostListener('keydown', const ['\$event'])
void onClick(event) {
  x++;
}

With Vanilla Dart

import "dart:html";

void main() {
  document.onKeyDown.listen((event) {
    print(event);
  });
}

Upvotes: 6

Related Questions