Angular 2+ elegant way to intercept Command+S

前端 未结 3 1503
刺人心
刺人心 2021-02-20 00:10

Trying to implement a shortcut key combination for Command+S to save a form.

I\'ve read this - https://angular.io/guide/user-input, but it does not say anything about me

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 00:42

    Global listener, non-deprecated answer:

    @HostListener('window:keydown', ['$event'])
    onKeyDown(event: KeyboardEvent) {
        if ((event.metaKey || event.ctrlKey) && event.key === 's') {
            this.save();
            event.preventDefault();
        }
    }
    

提交回复
热议问题