Angular 仅提供键子集的简写,您应该只监听任何 keydown 事件并使用键盘事件在代码中找出它。
<span class="template-row"
(click)="passPreviewForDisplay()"
(keydown.enter)="addTemplateToFroala()"
(keydown.control.shift.f)="handle($event)"
(keydown.arrowleft)="closeTree($event)"
(keydown.arrowright)="openTree($event)"
(keydown)="checkAsterisk($event)">
{{template.name}}
</span>
在控制器中:
const ASTERISK_CODE = 999; //I don't actually know the keycode for the german asterisk but you could find it easily by logging a keydown event from that key
checkAsterisk(kb: KeyboardEvent) {
if (kb.shiftKey && kb.keyCode === ASTERISK_CODE) {
console.log('asterisk poressed');
}
}
如果这是您需要在整个应用程序中执行的操作,您可以非常轻松地为它创建一个指令,例如:
const ASTERISK_CODE = 999; //I don't actually know the keycode for the german asterisk but you could find it easily by logging a keydown event from that key
@Directive({
selector: '[asteriskPress]',
host: { '(keydown)': 'checkAsterisk($event)' }
})
export class AsteriskPressDirective {
@Output() asteriskPress: EventEmitter<KeyboardEvent> = new EventEmitter<KeyboardEvent>();
checkAsterisk(kb: KeyboardEvent) {
// check if shift key pressed and keyCode is asterisk
if (kb.shiftKey && kb.keyCode === ASTERISK_CODE) {
this.asteriskPress.next(kb);
}
}
}
然后在模板中使用它(在正确声明/导出/导入等之后):
<span class="template-row"
(click)="passPreviewForDisplay()"
(keydown.enter)="addTemplateToFroala()"
(keydown.control.shift.f)="handle($event)"
(keydown.arrowleft)="closeTree($event)"
(keydown.arrowright)="openTree($event)"
(asteriskPress)="reactToPress($event)">
{{template.name}}
</span>