Angular 2+ keydown 组合与德语键盘的 "*"星号键

标签 angular angular5

我想使用德语键盘结合 Shift 和星号键来处理 keydown 事件。

此时我可以使用任何其他组合来处理:

 <span class="template-row"
  (click)="passPreviewForDisplay()"
  (keydown.enter)="addTemplateToFroala()"
  (keydown.control.shift.f)="handle($event)"
  (keydown.arrowleft)="closeTree($event)"
  (keydown.arrowright)="openTree($event)">
  {{template.name}}
</span>

这是我处理简单情况的示例代码。我没有找到 keydown 事件的所有可用组合,也找不到处理 keydown.control.shift.* 之类的信息。我强调它应该从德语键盘调用。 https://en.wikipedia.org/wiki/German_keyboard_layout#/media/File:German-T2-Keyboard-Prototype-May-2012.jpg key 位于 Ü 旁边(右侧)。

最佳答案

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>

在 Controller 中:

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>

关于Angular 2+ keydown 组合与德语键盘的 "*"星号键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49734475/

相关文章:

redux - 如何从 ngrx 中的 Router_Cancel 获取以前的状态?

angular - 如何在 laravel 中实现 Angular 服务器端渲染?

javascript - Angular 返回 URL 错误

angular - 在 Angular2/4 中加载组件

Angular 2 - 检查后表达式已更改

css - 如何将值从 ts 文件传递​​到 angular 2+ 中同一组件中的 scss 文件?

angular - ionic 4 - Angular 6 : How to control Ionic router history to stop cache a view component?

具有最少文件数的 Angular ng 构建? (仅支持 Chrome 也可以)

html - 如何在angular6中的for循环内动态更改图像?

Angular *ngIf 内存使用