【问题标题】:Simple keyboard in angular角度简单的键盘
【发布时间】:2019-12-10 15:03:16
【问题描述】:

我正在尝试以角度制作简单的键盘。当用户单击文本框时,我需要启用键盘,但是当我运行我的应用程序时,我得到了永久键盘。我的代码如下::

app.component.html:

<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
    <input (input)="onInputChange($event)" class="input" value={{value}} placeholder="Tap on the virtual keyboard to start" />
    <div class="simple-keyboard"></div>
</div>

app.component.ts:

import { Component, ViewEncapsulation } from "@angular/core";
import Keyboard from 'simple-keyboard';

@Component({
  selector: 'app-root',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './app.component.html',
  styleUrls: [
    '../../node_modules/simple-keyboard/build/css/index.css',
    './app.component.css'
  ]
})
export class AppComponent {
  value = '';
  keyboard: Keyboard;

  ngAfterViewInit() {
    this.keyboard = new Keyboard({
      onChange: input => this.onChange(input),
      onKeyPress: button => this.onKeyPress(button)
    });
  }

  onChange = (input: string) => {
    this.value = input;
    console.log('Input changed', input);
  };

  onKeyPress = (button: string) => {
    console.log('Button pressed', button);

    /**
     * If you want to handle the shift and caps lock buttons
     */
    if (button === '{shift}' || button === '{lock}') this.handleShift();
  };

  onInputChange = (event: any) => {
    this.keyboard.setInput(event.target.value);
  }

  handleShift = () => {
    let currentLayout = this.keyboard.options.layoutName;
    let shiftToggle = currentLayout === "default" ? "shift" : "default";

    this.keyboard.setOptions({
      layoutName: shiftToggle
    });
  };
}

我得到的输出如下:

output_keyboard

应用程序已加载键盘。但是我希望当我单击文本框时应该启用键盘。我该怎么做?

【问题讨论】:

  • 我不知道你的键盘,我想你的键盘可以有一个 @Input() 你传递一个 NgControl -a ngControl 是任何带有 [(ngModel)]、[formControlName] 或 [ formControl] 和每个(单击)键更改 NgControl。在输入焦点中,变量“keyBoardOn”等于 true,a (blur) 等于 false,*ngFir 显示或不显示键盘。但重复一遍,我无法想象你的键盘组件,或者即使它是一个组件

标签: angular angular6 angular7 angular8


【解决方案1】:

试试这个,我认为工作如你所愿


<!--The content below is only a placeholder and can be replaced.-->
<div style="text-align:center">
    <input on-focus="onInputFocus()" (input)="onInputChange($event)" class="input" value={{value}} placeholder="Tap on the virtual keyboard to start" />
    <div class="simple-keyboard"></div>
</div>

app.component.ts:

import { Component, ViewEncapsulation } from "@angular/core";
import Keyboard from 'simple-keyboard';

@Component({
  selector: 'app-root',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './app.component.html',
  styleUrls: [
    '../../node_modules/simple-keyboard/build/css/index.css',
    './app.component.css'
  ]
})
export class AppComponent {
  value = '';
  keyboard: Keyboard;

  ngAfterViewInit() {
  }

onInputFocus() {
this.keyboard = new Keyboard({
      onChange: input => this.onChange(input),
      onKeyPress: button => this.onKeyPress(button)
    });
}

  onChange = (input: string) => {
    this.value = input;
    console.log('Input changed', input);
  };

  onKeyPress = (button: string) => {
    console.log('Button pressed', button);

    /**
     * If you want to handle the shift and caps lock buttons
     */
    if (button === '{shift}' || button === '{lock}') this.handleShift();
  };

  onInputChange = (event: any) => {
    this.keyboard.setInput(event.target.value);
  }

  handleShift = () => {
    let currentLayout = this.keyboard.options.layoutName;
    let shiftToggle = currentLayout === "default" ? "shift" : "default";

    this.keyboard.setOptions({
      layoutName: shiftToggle
    });
  };
}

https://stackblitz.com/edit/angular-ggxgjg

【讨论】:

  • 你能解释一下我犯了什么错误吗?
  • "on-focus="onInputFocus()"
  • 我使用你的代码和控制台抛出“KEYBOARD_DOM_ERROR”这个错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-21
  • 1970-01-01
相关资源
最近更新 更多