Appearance
方向键监听
实验箱提供方向键监听,按下按键即可展示按下按键的方向。
操作流程
1、调用电子开关接口,配置引脚对应为方向键设备()
2、调用方向键监听
3、调用电子开关接口,还原配置引脚为光敏或滑动变阻器设备
方向键监听
导入模块
typescript
//电子开关模块
import ElectronicSwitch from '@ohos.openvalley.ElectronicSwitch';
//系统输入模块
import inputConsumer from '@ohos.multimodalInput.inputConsumer';
1、电子开关配置
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
DeviceType | number | 是 | 0:光线传感器,1:滑动变阻器,2:物理按键 |
返回值:
类型 | 说明 |
---|---|
boolean | 配置成功/失败 |
指定设备为物理按键
typescript
ElectronicSwitch.switchDevice(2);
2、方向键监听
左键监听
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
KEYCODE_DPAD_LEFT | number | 是 | 导航键 向左 |
开启左键监听
typescript
keyWatchOn(){
this.keyOptions3 = { preKeys: [], finalKey: 2014, isFinalKeyDown: true, finalKeyDownDuration: 0 }
this.callback3 = (keyOptions) => {
log("左")
this.resultMessage = "左"
}
inputConsumer.on('key', this.keyOptions3, this.callback3)
this.isOpenSensor = true
}
关闭监听
typescript
keyWatchOff(){
inputConsumer.off('key', this.keyOptions3, this.callback3)
this.isOpenSensor = false
}
右键监听
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
KEYCODE_DPAD_RIGHT | number | 是 | 导航键 向右 |
开启右键监听
typescript
keyWatchOn(){
this.keyOptions4 = { preKeys: [], finalKey: 2015, isFinalKeyDown: true, finalKeyDownDuration: 0 }
this.callback4 = (keyOptions) => {
log("右")
this.resultMessage = "右"
}
inputConsumer.on('key', this.keyOptions4, this.callback4)
this.isOpenSensor = true
}
关闭监听
typescript
keyWatchOff(){
inputConsumer.off('key', this.keyOptions4, this.callback4)
this.isOpenSensor = false
}
上键监听
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
KEYCODE_DPAD_UP | number | 是 | 导航键 向上 |
开启上键监听
typescript
keyWatchOn(){
this.keyOptions = { preKeys: [], finalKey: 2012, isFinalKeyDown: true, finalKeyDownDuration: 0 }
this.callback = (keyOptions) => {
log("上")
this.resultMessage = "上"
}
inputConsumer.on('key', this.keyOptions, this.callback)
this.isOpenSensor = true
}
关闭监听
typescript
keyWatchOff(){
inputConsumer.off('key', this.keyOptions, this.callback)
this.isOpenSensor = false
}
下键监听
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
KEYCODE_DPAD_DOWN | number | 是 | 导航键 向下 |
开启下键监听
typescript
keyWatchOn(){
this.keyOptions2 = { preKeys: [], finalKey: 2013, isFinalKeyDown: true, finalKeyDownDuration: 0 }
this.callback2 = (keyOptions) => {
log("下")
this.resultMessage = "下"
}
inputConsumer.on('key', this.keyOptions2, this.callback2)
this.isOpenSensor = true
}
关闭监听
typescript
keyWatchOff(){
inputConsumer.off('key', this.keyOptions2, this.callback2)
this.isOpenSensor = false
}
中键监听
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
KEYCODE_DPAD_CENTER | number | 是 | 导航键 确定键 |
开启中键监听
typescript
keyWatchOn(){
this.keyOptions6 = { preKeys: [], finalKey: 2054, isFinalKeyDown: true, finalKeyDownDuration: 0 }
this.callback6 = (keyOptions) => {
log("中键")
this.resultMessage = "中键"
}
inputConsumer.on('key', this.keyOptions6, this.callback6)
this.isOpenSensor = true
}
关闭监听
typescript
keyWatchOff(){
inputConsumer.off('key', this.keyOptions6, this.callback6)
this.isOpenSensor = false
}
3、电子开关还原配置
系统能力: SystemCapability.Sensors.EducationDeviceService
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
DeviceType | number | 是 | 0:光线传感器,1:滑动变阻器,2:物理按键 |
返回值:
类型 | 说明 |
---|---|
boolean | 配置成功/失败 |
指定设备
typescript
ElectronicSwitch.switchDevice(0);