【发布时间】:2018-01-26 14:40:13
【问题描述】:
我正在尝试控制用户可以在我的 ionic 3 应用程序中输入特定输入的内容。
例如,我有一个input type='text',我只允许使用数字和下划线,我正在尝试使用键盘事件来实现这一点。
这适用于浏览器,但不适用于实际的 android,因为所有 keyCode 都是 0;
请建议如何处理这种事情,我是否使用了最好的方法?
HTML代码:
<form [formGroup]="pinForm">
<div class="modal-input">
<ion-item>
<ion-input type="text" [(ngModel)]="pin" #pinNumber></ion-input>
</ion-item>
</div>
</form>
ts 代码:
@ViewChild('pinNumber') inputPin:any;
@HostListener("keydown", ["$event"])
onKeyDown(e: KeyboardEvent) {
//here i am controlling the input to do what ever i need
console.log(e);
}
上面的代码在电脑上的浏览器中有效,但在实际设备上无效 如下图全为0
那么如何知道按下了什么键呢?
altGraphKey: 假
altKey: 假
气泡:真实
取消气泡:假
可取消:真
字符代码:0
剪贴板数据:未定义
ctrlKey: 假
当前目标:空
defaultPrevented: true
细节:0
事件阶段:0
键码:0
密钥标识符:“U+0000”
关键位置:0
layerX: 0
图层Y:0
位置:0
元密钥:假
pageX: 0
第Y页:0
返回值:假
shiftKey: 假
srcElement: input.text-input text-input-md
目标:input.text-input text-input-md
时间戳:1516972480017
类型:“keydown”
视图:窗口
其中:0
原型:键盘事件
【问题讨论】:
-
试试这个
@HostListener('document:keydown, ['$event']) -
@santoshsingh 还是一样。
-
你在什么设备上测试?
-
@PhilipBrack 我在 samnsung s8 和 samsung grand 2 android 4.4.2 上测试过
-
也试过
(keyup)="eventHandler($event)"eventHandler(e) { console.log(e.keyCode); }还是没有
标签: angular ionic-framework ionic3 keyboard-events keycode