import { Injectable } from '@angular/core' ; @ Injectable ( { providedIn : 'root' , } ) export class KeyboardHandlerService { handle ( event , items , selectedIndex ) { let newIndex = - 1 ; switch ( event . key ) { case 'ArrowUp' : case 'ArrowLeft' : newIndex = selectedIndex - 1 ; break ; case 'ArrowDown' : case 'ArrowRight' : if ( selectedIndex === undefined ) { newIndex = 0 ; } else if ( selectedIndex < items . length - 1 ) { newIndex = selectedIndex + 1 ; } break ; case 'Home' : newIndex = 0 ; break ; case 'End' : newIndex = items . length - 1 ; break ; default : break ; } return newIndex ; } }