Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In angular2 how to detect Ctrl A key press?

Tags:

angular

I have a list of items and I am trying to give file traverse behavior like file explorer to the list of items meaning after selecting an item, if you hold shift key and press down arrow those items should get selected.

I have a list as mentioned below.

    <div class="container">
    <ul class="mylist">
    <li  tabindex="1">item1</li>
    <li  tabindex="2">item2</li>
    <li  tabindex="3">item3</li>
    <li tabindex="4">item4</li>
    <li tabindex="5">item5</li>
    <li tabindex="6">item6</li>
    <li tabindex="7">item7</li>
    <li tabindex="8">item8</li>
    <li tabindex="9">item9</li>
    <li tabindex="10">item10</li>
</ul>

If I am using (keydown.ctrl.a)="handleKey($event, item.name)", it's not recognizing ctrl and a button click. How can I achieve this in angular2?

like image 866
code1 Avatar asked Sep 07 '17 16:09

code1


2 Answers

Use control instead of ctrl:

(keydown.control.a)="handleKey($event, item.name)"
like image 80
Faisal Avatar answered Oct 26 '22 20:10

Faisal


Put this in handleKey to detects that Ctrl + a are pressed:

event.getModifierState && event.getModifierState('Control') && event.keyCode===65

DEMO

like image 43
Vega Avatar answered Oct 26 '22 18:10

Vega