Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind to keydown event of li tag in Angular2?

Tags:

angular

using (click)="onSelect(hero), I can bind to click event of li tag. I can also bind to mouseover event of li tag. But I can't bind to keydown event of li tag. li tag supports click,mouseover,keydown propety, so I think I can use keydown event(down arrow) to navigate to the next item in this list box.

Where is the official docs I can look for?

    <div class = "body-container">
        <ul class = "heroes">
            <li *ngFor = "let hero of heroes" (click)="onSelect(hero)" (keydown)="onKeydown()" (mouseover)="onKeydown()" class="bl-list-item" [class.bl-list-item-checked]="hero === selectedHero">
                <div class="guide-label">
                    <span style="width:50px" [class.fa-check]="hero === selectedHero" [class.li-fa-check]="hero === selectedHero"></span>
                    <div class="guide-code-column">{{hero.id}}</div>
                    <div class="guide-name-column">{{hero.name}}</div>
                </div>
            </li>
        </ul>
    </div>

    export class SearchComponent {
        heroes: Hero[] = [];
        selectedHero: Hero;

        constructor(private heroService: HeroService) { }

        ngOnInit(): void {
            this.heroService.getHeroes()
            .then(heroes => this.heroes = heroes);
        }

        showDialog = false;

        onSelect(hero: Hero): void {
            this.selectedHero = hero;
        }

        onKeydown(): void {
            console.log("onKeydown");
        }
     }
like image 399
niaomingjian Avatar asked Mar 11 '23 14:03

niaomingjian


2 Answers

You basically start with the event-type and then add a series of dot-delimited modifiers. For example:

First, the limitations. The native KeyEventsPlugin plugin only support keydown and keyup events, not keypress. And, these key combinations can only be bound to a specific element (or host) - the plugin doesn't appear to support the global "document:" or "window:" event-scope. There is also no implicit support for browser-overrides. Meaning, if you need to cancel the default-behavior of the key-combination, you have to do it yourself (with $event.preventDefault()).

keydown.a
keydown.b
keydown.c
keydown.dot
keydown.Spacebar
keydown.meta.Enter
keydown.alt.Enter
keydown.control.Enter
keydown.shift.Enter
keydown.meta.o
keydown.meta.s
keydown.meta.f
keydown.escape

The "special key" modifiers are:

alt control meta - The Command key on Mac and the Windows key on Windows. shift There are then two replacement keys that are there just keep the syntax from breaking:

Space - Or, you can use "Spacebar". Dot - Since the modifiers are dot-delimited.

  <input
    (keydown.Enter)="handleKeyEvent( $event, 'Enter' )"
    (keydown.alt.Enter)="handleKeyEvent( $event, 'ALT + Enter' )"                                   (keydown.control.Enter)="handleKeyEvent( $event, 'Control + Enter' )"
    (keydown.meta.Enter)="handleKeyEvent( $event, 'Meta + Enter' )"
    (keydown.shift.Enter)="handleKeyEvent( $event, 'Shift + Enter' )"
    (keydown.Escape)="handleKeyEvent( $event, 'Escape' )"
    (keydown.ArrowLeft)="handleKeyEvent( $event, 'Arrow Left' )"
    (keydown.ArrowUp)="handleKeyEvent( $event, 'Arrow Up' )"
    (keydown.ArrowRight)="handleKeyEvent( $event, 'Arrow Right' )"
    (keydown.ArrowDown)="handleKeyEvent( $event, 'Arrow Down' )"
 autofocus>
like image 157
wuno Avatar answered Mar 24 '23 07:03

wuno


The element has to be focused and for that it needs to be an input element or have the tabindex set to >= "0".

See also Capture key press (or keydown) event on DIV element

You can capture keyboard events globally like (window:keydown)="onKeydown($event)" but I don't know if this helps in your case.

like image 40
Günter Zöchbauer Avatar answered Mar 24 '23 06:03

Günter Zöchbauer