使用屏幕键盘提交表单

发布于 2025-01-17 03:15:34 字数 1070 浏览 0 评论 0原文

我正在尝试使用屏幕键盘提交表单,但不知道如何处理。本质上,当我单击表单上的提交按钮时,我需要能够看到在屏幕键盘中单击的字符。

我有一个指令,其中包含用于列出事件的方法,例如 onFocus onBlur。我有一个组件,列出了不同的按键和鼠标事件,例如 keyPressed _enterPressed 等。

下面的 stackblitz 链接,

这是我的代码和service.ts

private _enterPressed: Subject<void>;

get enterPressed() {
  return this._enterPressed;
}

fireEnterPressed() {
  this._enterPressed.next();
}

指令

private onEnter() {
  let element = this.el.nativeElement;
  let inputEl = element.querySelector('input');
  alert("Enter"+ inputEl.value);
}

app component.ts

submit() {
  //submit forms
  alert('submit');
}

这里是stackblitz 代码示例 https://stackblitz.com/edit/onscreen-keyboard-5uxhyo?file=src%2Fapp%2Fapp.component.ts

当我使用屏幕键盘在输入字段中输入字符时,并且然后我单击“提交”按钮,我看不到输入的字符,但是如果我在计算机键盘上正常键入并单击“提交”,那么我可以看到警报的值。

知道我该如何解决这个问题吗?

I am trying to submit a form using an on screen keyboard but am not sure how to go about this. Essentially I need be able to see the characters that I click on in the on screen keyboard when I click the submit button on my form.

I have a directive that has the methods I use for listing the events like onFocus onBlur. I have a component that lists the different key and mouse events like keyPressed _enterPressed etc.

Here is my code and a stackblitz link below

service.ts

private _enterPressed: Subject<void>;

get enterPressed() {
  return this._enterPressed;
}

fireEnterPressed() {
  this._enterPressed.next();
}

directive

private onEnter() {
  let element = this.el.nativeElement;
  let inputEl = element.querySelector('input');
  alert("Enter"+ inputEl.value);
}

app component.ts

submit() {
  //submit forms
  alert('submit');
}

Here is a stackblitz example of the code https://stackblitz.com/edit/onscreen-keyboard-5uxhyo?file=src%2Fapp%2Fapp.component.ts

When I use the on screen keyboard to input characters into the input fields, and then I click the submit button I do not see the characters that I have entered however if I type normally on my computers keyboard and click submit then i can see the values alerted.

Any idea how I can fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2025-01-24 03:15:34

您的键盘仅更改“element.value”。

您可以在 osk-input.directive 中注入“NgControl”

constructor(@Optional()private control:NgControl,
            private el: ElementRef, private keyboard: KeyboardService) {}

,并在函数 onKeyonBackspace 中添加 this.control.control.setValue (...)

  private onKey(key: string) {
    let element = this.el.nativeElement,start = element.selectionStart,end = element.selectionEnd;

    this.measure.textContent = element.value.substr(0, start) + key;
    element.value =
      element.value.substr(0, start) + key + element.value.substr(end);

    //<--add this lines--//
    if (this.control)
       this.control.control.setValue(element.value) 
    ...
  }

  private onBackspace() {
    let element = this.el.nativeElement,start = element.selectionStart,end = element.selectionEnd;
    ...
    this.measure.textContent = element.value.substr(0, start);
    element.value = element.value.substr(0, start) + element.value.substr(end);

    //<--add this lines--//
    if (this.control)
       this.control.control.setValue(element.value) 
    ...
  }

顺便提一句。你的 onEnterFunction 应该像

  private onEnter() {
    const ev = new KeyboardEvent('keydown',{code:"Enter",key: "Enter",
    keyCode: 13})
    this.el.nativeElement.dispatchEvent(ev)
  }

这样,你可以在 .html 中使用你

<form [formGroup]="setupForm">
  <input appOskInput formControlName="email" 
        (keydown.enter)="mobile.focus()" />
  <input #mobile appOskInput formControlName="mobile" 
        (keydown.enter)="button.focus()" />
  <button #button type="button" (click)="submit()">Submit</button>
</form>

分叉 stackblitz

Your keyboard only change the "element.value".

You can inject the "NgControl" in your osk-input.directive

constructor(@Optional()private control:NgControl,
            private el: ElementRef, private keyboard: KeyboardService) {}

And in your functions onKey and onBackspace add this.control.control.setValue(...)

  private onKey(key: string) {
    let element = this.el.nativeElement,start = element.selectionStart,end = element.selectionEnd;

    this.measure.textContent = element.value.substr(0, start) + key;
    element.value =
      element.value.substr(0, start) + key + element.value.substr(end);

    //<--add this lines--//
    if (this.control)
       this.control.control.setValue(element.value) 
    ...
  }

  private onBackspace() {
    let element = this.el.nativeElement,start = element.selectionStart,end = element.selectionEnd;
    ...
    this.measure.textContent = element.value.substr(0, start);
    element.value = element.value.substr(0, start) + element.value.substr(end);

    //<--add this lines--//
    if (this.control)
       this.control.control.setValue(element.value) 
    ...
  }

BTW. Your onEnterFunction should be some like

  private onEnter() {
    const ev = new KeyboardEvent('keydown',{code:"Enter",key: "Enter",
    keyCode: 13})
    this.el.nativeElement.dispatchEvent(ev)
  }

So, you can use in your .html like

<form [formGroup]="setupForm">
  <input appOskInput formControlName="email" 
        (keydown.enter)="mobile.focus()" />
  <input #mobile appOskInput formControlName="mobile" 
        (keydown.enter)="button.focus()" />
  <button #button type="button" (click)="submit()">Submit</button>
</form>

Your forked stackblitz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文