按键事件指组件与键盘、遥控器等按键设备交互时触发的事件(仅适用于所有可获焦组件,例如:Button)
//绑定该方法的组件获焦后,按键动作触发该回调 onKeyEvent(event:(event?:keyEvent)=>void)
案例代码:
/** * author:创客未来 * copyright:com.ckFuture.hrb * 按键事件 */ @Entry @Component struct KeyEventDemo { @State text: string = '' @State eventType: string = '' build() { Column() { Button('KeyEvent') .onKeyEvent((event: KeyEvent) => { if(event){ if (event.type === KeyType.Down) { this.eventType = 'Down' } if (event.type === KeyType.Up) { this.eventType = 'Up' } this.text = 'KeyType:' + this.eventType + '\nkeyCode:' + event.keyCode + '\nkeyText:' + event.keyText } }) Divider() Text(this.text).padding(15).fontSize(26) }.height(300).width('100%').padding(35) } }
注意:测试的时候需要按击键盘。
标签:eventType,KeyType,text,十五,按键,组件,event From: https://www.cnblogs.com/ckfuture/p/17913233.html