1# 获取用户动作开发指导 2<!--Kit: Multimodal Awareness Kit--> 3<!--Subsystem: MultimodalAwareness--> 4<!--Owner: @dilligencer--> 5<!--Designer: @zou_ye--> 6<!--Tester: @judan--> 7<!--Adviser: @hu-zhiqiong--> 8 9## 场景介绍 10 11当应用需要获取用户动作时,可以调用motion模块,例如判断用户当前是用左手还是右手操作设备屏幕。 12 13详细的接口介绍请参考[Motion接口](../../reference/apis-multimodalawareness-kit/js-apis-awareness-motion.md)。 14 15## 获取操作手状态开发指导 16 17### 接口说明 18 19| 接口名 | 描述 | 20| ------------------------------------------------------------ | -------------------------------------- | 21| on(type:'operatingHandChanged',callback:Callback<OperatingHandStatus>):void; | 订阅操作手感知,操作手结果通过callback返回。 | 22| off(type: 'operatingHandChanged', callback?: Callback<OperatingHandStatus>): void; | 取消订阅操作手感知。 | 23| getRecentOperatingHandStatus(): OperatingHandStatus; | 获取最新的操作手状态。 | 24 25### 约束与限制 26 27 - 此功能如果设备不支持,将返回801错误码。 28 29 - 指关节操作不属于使用手操作场景。 30 31 - 窗口旋转场景,多指同时操作场景不支持。 32 33 - 能力有效范围:不包含距离屏幕边缘8mm内区域。 34 35### 开发步骤 36 371. 导入模块。 38 39 ```ts 40 import { motion } from '@kit.MultimodalAwarenessKit'; 41 import { BusinessError } from '@kit.BasicServicesKit'; 42 import { Callback } from '@ohos.base'; 43 ``` 44 452. 定义回调函数接收操作手结果 46 47 ``` 48 let callback:Callback<motion.OperatingHandStatus> = (data:motion.OperatingHandStatus) => { 49 console.info('callback success' + data); 50 }; 51 ``` 52 533. 订阅操作手感知 54 55 ``` 56 try { 57 motion.on('operatingHandChanged', callback); 58 console.info("on succeeded"); 59 } catch (err) { 60 let error = err as BusinessError; 61 console.error("Failed on and err code is " + error.code); 62 } 63 ``` 64 654. 取消订阅操作手感知 66 67 ``` 68 try { 69 motion.off('operatingHandChanged'); 70 console.info("off succeeded"); 71 } catch (err) { 72 let error = err as BusinessError; 73 console.error("Failed off and err code is " + error.code); 74 } 75 ``` 76 775. 获取最新操作手状态 78 79 ``` 80 try { 81 let data:motion.OperatingHandStatus = motion.getRecentOperatingHandStatus(); 82 console.info('get success' + data); 83 } catch (err) { 84 let error = err as BusinessError; 85 console.error("Failed get and err code is " + error.code); 86 } 87 ``` 88 89 90## 获取握持手状态开发指导 91 92### 接口说明 93 94| 接口名 | 描述 | 95| ------------------------------------------------------------ | -------------------------------------- | 96| on(type:'holdingHandChanged',callback:Callback<HoldingHandStatus>): void; | 订阅握持手感知,感知结果通过callback返回。 | 97| off(type: 'holdingHandChanged', callback?: Callback<HoldingHandStatus>): void; | 取消订阅握持手感知。 | 98 99### 约束与限制 100 101 - 此功能当前支持部分机型,若设置菜单中存在“握姿跟随”开关(可在“设置-系统”中查看),则表明该设备支持此功能,若无此开关,将返回801错误码。 102 - 设备屏幕需处于亮屏且解锁状态。 103 - 设备保护壳(若有)厚度不得超过3毫米。 104 - 需以五指自然握持设备,同时掌心区域接触设备(或拇指外的四指及掌心区域接触)。 105 - 握持时确保每根接触手指的接触面积尽可能大(理想情况下不低于30mm²)。 106 - 佩戴手套会显著降低识别准确率。 107 - 竖屏握持时,摄像头需朝上。 108 - 支持横屏握持,但需要注意:应用横屏时竖屏握持(握持设备短边),应用竖屏时横屏握持(握持设备长边),均属异常姿态,无法保证识别成功。 109 - 握持时屏幕需朝向握持人。 110 - 握持时不得同时接触其他物体(如桌面、其他身体部位等)。 111 - 未握持的识别依赖设备状态,设备非静止时无法保证识别成功。 112 113### 开发步骤 114 1151. 导入模块。 116 117 ```ts 118 import { motion } from '@kit.MultimodalAwarenessKit'; 119 import { BusinessError } from '@kit.BasicServicesKit'; 120 import { Callback } from '@ohos.base'; 121 ``` 122 1232. 定义回调函数接收握持手结果 124 125 ``` 126 let callback:Callback<motion.HoldingHandStatus> = (data:motion.HoldingHandStatus) => { 127 console.info('callback success' + data); 128 }; 129 ``` 130 1313. 订阅握持手感知 132 133 ``` 134 try { 135 motion.on('holdingHandChanged', callback); 136 console.info("on succeeded"); 137 } catch (err) { 138 let error = err as BusinessError; 139 console.error("Failed on and err code is " + error.code); 140 } 141 ``` 142 1434. 取消订阅握持手感知 144 145 ``` 146 try { 147 motion.off('holdingHandChanged'); 148 console.info("off succeeded"); 149 } catch (err) { 150 let error = err as BusinessError; 151 console.error("Failed off and err code is " + error.code); 152 } 153 ``` 154