文档管理中心
指南系统基础功能Input Kit(多模输入服务)优先响应系统功能键开发指导

优先响应系统功能键开发指导

场景介绍

每个系统功能键均具有默认功能,由系统固定实现,比如音量键是用来调节设备音量,但是部分应用在特定场景下期望定制这部分按键的功能,本篇指导用于支撑这部分应用的诉求达成。

常见使用场景:阅读类型应用期望通过音量键翻页,相机应用期望通过音量键拍照等应用响应系统功能键做其他业务的场景。

支持功能键列表:从API version 16开始支持音量加按键和音量减按键。从API version 21开始,新增支持多媒体播放/暂停、多媒体下一首和多媒体上一首按键。从API version 26.0.0开始,新增支持智控键上滑和智控键下滑,非设备通用键值,使用前请判断当前设备是否支持相关按键事件上报。

约束与限制

  • 应用窗口为前台焦点窗口时,优先响应才生效。
  • 应用选择高于系统优先响应指定的系统功能键后,功能键的默认行为将失效,所以应用需要确保只有在确定响应时才激活该功能。

接口说明

按键按下事件常用接口如下表所示,接口详细介绍请参考@ohos.multimodalInput.inputConsumer (全局快捷键)

展开
接口名称 描述
on(type: "keyPressed", options: KeyPressedConfig, callback: Callback<KeyEvent>): void 订阅指定按键按下事件,拦截系统默认响应。
off(type: "keyPressed", callback?: Callback<KeyEvent>): void 取消按键事件订阅,恢复系统默认响应。

开发步骤

应用开启时调用on方法订阅按键按下事件,应用关闭时再用off方法取消订阅按键按下事件。

应用内优先响应系统功能键

在电子书或新闻阅读应用中,用户希望通过音量键或智控键滑动控制翻页(例如:音量加键向上翻页、音量减键向下翻页、智控键上滑向上翻页、智控键下滑向下翻页),需注意智控键上滑及下滑非设备通用键值,使用前请判断当前设备是否支持相关按键事件上报;在相机或扫码类应用中,用户按音量键可直接拍照,而不跳转系统相机应用。

收起
自动换行
深色代码主题
复制
  1. import { inputConsumer, KeyEvent, inputDevice, KeyCode } from '@kit.InputKit';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. const DOMAIN = 0x0000;
  5. @Entry
  6. @Component
  7. struct TestDemo14 {
  8. @State showComponent: boolean = false
  9. @State text: string = "Default monitoring for Volume Up and Down keys has been added."
  10. private volumeUpCallBackFunc: (event: KeyEvent) => void = () => {
  11. }
  12. private volumeDownCallBackFunc: (event: KeyEvent) => void = () => {
  13. }
  14. private slideUpCallBackFunc: (event: KeyEvent) => void = () => {
  15. }
  16. private slideDownCallBackFunc: (event: KeyEvent) => void = () => {
  17. }
  18. options1: inputConsumer.KeyPressedConfig = {
  19. key: KeyCode.KEYCODE_VOLUME_UP,
  20. action: 1, // 按下按键的行为
  21. isRepeat: false, // 优先消费掉按键事件,不上报
  22. }
  23. options2: inputConsumer.KeyPressedConfig = {
  24. key: KeyCode.KEYCODE_VOLUME_DOWN,
  25. action: 1, // 按下按键的行为
  26. isRepeat: false, // 优先消费掉按键事件,不上报
  27. }
  28. options3: inputConsumer.KeyPressedConfig = {
  29. key: KeyCode.KEYCODE_FINGERPRINT_SLIDE_UP,
  30. action: 1, // 按下按键的行为
  31. isRepeat: false, // 优先消费掉按键事件,不上报
  32. }
  33. options4: inputConsumer.KeyPressedConfig = {
  34. key: KeyCode.KEYCODE_FINGERPRINT_SLIDE_DOWN,
  35. action: 1, // 按下按键的行为
  36. isRepeat: false, // 优先消费掉按键事件,不上报
  37. }
  38. // 判断设备是否支持KEYCODE_FINGERPRINT_SLIDE_UP 和 KEYCODE_FINGERPRINT_SLIDE_DOWN
  39. private isFingerprintSlideKeySupported(): Promise<boolean> {
  40. return new Promise<boolean>((resolve) => {
  41. inputDevice.getDeviceList((error: BusinessError, ids: Array<Number>) => {
  42. if (error) {
  43. console.error(`keyPressed Failed to get device id list, error: ${
  44. JSON.stringify(error, ['code', 'message'])}`);
  45. resolve(false);
  46. return;
  47. }
  48. console.info(`keyPressed Device id list: ${JSON.stringify(ids)}`);
  49. for (let idTemp of ids) {
  50. let res = inputDevice.supportKeysSync(Number(idTemp), [KeyCode.KEYCODE_FINGERPRINT_SLIDE_UP,
  51. KeyCode.KEYCODE_FINGERPRINT_SLIDE_DOWN]);
  52. if (res[0] && res[1]) {
  53. console.info(`keyPressed ${idTemp} Device id list supportKeysSync : ${JSON.stringify(res)}`);
  54. resolve(true);
  55. return;
  56. }
  57. }
  58. resolve(false);
  59. });
  60. });
  61. }
  62. aboutToAppear(): void {
  63. try {
  64. // 点击了音量按键上事件回调
  65. this.volumeUpCallBackFunc = (event: KeyEvent) => {
  66. this.getUIContext().getPromptAction().showToast({ message: 'Volume Up key pressed' })
  67. // do something
  68. }
  69. // 点击了音量按键下事件回调
  70. this.volumeDownCallBackFunc = (event: KeyEvent) => {
  71. this.getUIContext().getPromptAction().showToast({ message: 'Volume Down key pressed' })
  72. // do something
  73. }
  74. // 智控键事件上滑回调
  75. this.slideUpCallBackFunc = (event: KeyEvent) => {
  76. this.getUIContext().getPromptAction().showToast({ message: 'Slide Up key pressed' })
  77. // do something
  78. }
  79. // 智控键事件下滑回调
  80. this.slideDownCallBackFunc = (event: KeyEvent) => {
  81. this.getUIContext().getPromptAction().showToast({ message: 'Slide Down key pressed' })
  82. // do something
  83. }
  84. this.isFingerprintSlideKeySupported().then((supported: boolean) => {
  85. this.showComponent = supported;
  86. }).catch((error: Error) => {
  87. console.error(`Failed to check fingerprint support: ${JSON.stringify(error)}`);
  88. this.showComponent = false;
  89. });
  90. } catch (error) {
  91. hilog.error(DOMAIN, 'InputConsumer', `Subscribe execute failed, error: %{public}s`,
  92. JSON.stringify(error, ["code", "message"]));
  93. }
  94. }
  95. build() {
  96. Column() {
  97. // 注册及去注册音量键事件
  98. Row() {
  99. Button('Add monitoring for Volume Up key')
  100. .onClick(() => {
  101. try {
  102. // 添加指定回调函数
  103. inputConsumer.on('keyPressed', this.options1, this.volumeUpCallBackFunc);
  104. this.getUIContext()
  105. .getPromptAction()
  106. .showToast({ message: 'Successfully added monitoring for Volume Up key!' })
  107. this.text = "Monitoring for Volume Up key has been added."
  108. } catch (error) {
  109. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  110. JSON.stringify(error, ["code", "message"]));
  111. this.getUIContext()
  112. .getPromptAction()
  113. .showToast({ message: 'Failed to add monitoring for Volume Up key!' })
  114. this.text = `Failed to add monitoring for Volume Up key: ${JSON.stringify(error, ["code", "message"])}`
  115. }
  116. })
  117. }.width('100%')
  118. .justifyContent(FlexAlign.Center)
  119. .margin({ top: 20, bottom: 20 })
  120. Row() {
  121. Button('Remove monitoring for Volume Up key')
  122. .onClick(() => {
  123. try {
  124. // 取消指定回调函数
  125. inputConsumer.off('keyPressed', this.volumeUpCallBackFunc);
  126. this.getUIContext()
  127. .getPromptAction()
  128. .showToast({ message: 'Successfully removed monitoring for Volume Up key!' })
  129. this.text = "Monitoring for Volume Up key has been removed."
  130. } catch (error) {
  131. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  132. JSON.stringify(error, ["code", "message"]));
  133. this.getUIContext()
  134. .getPromptAction()
  135. .showToast({ message: 'Failed to remove monitoring for Volume Up key!' })
  136. this.text = `Failed to remove monitoring for Volume Up key: ${JSON.stringify(error, ["code", "message"])}`
  137. }
  138. })
  139. }.width('100%')
  140. .justifyContent(FlexAlign.Center)
  141. .margin({ top: 20, bottom: 20 })
  142. Row() {
  143. Button('Add monitoring for Volume Down key')
  144. .onClick(() => {
  145. try {
  146. // 添加指定回调函数
  147. inputConsumer.on('keyPressed', this.options2, this.volumeDownCallBackFunc);
  148. this.getUIContext()
  149. .getPromptAction()
  150. .showToast({ message: 'Successfully added monitoring for Volume Down key!' })
  151. this.text = "Monitoring for Volume Down key has been added."
  152. } catch (error) {
  153. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  154. JSON.stringify(error, ["code", "message"]));
  155. this.getUIContext()
  156. .getPromptAction()
  157. .showToast({ message: 'Failed to add monitoring for Volume Down key!' })
  158. this.text = `Failed to add monitoring for Volume Down key: ${JSON.stringify(error, ["code", "message"])}`
  159. }
  160. })
  161. }.width('100%')
  162. .justifyContent(FlexAlign.Center)
  163. .margin({ top: 20, bottom: 20 })
  164. Row() {
  165. Button('Remove monitoring for Volume Down key')
  166. .onClick(() => {
  167. try {
  168. // 取消指定回调函数
  169. inputConsumer.off('keyPressed', this.volumeDownCallBackFunc);
  170. this.getUIContext()
  171. .getPromptAction()
  172. .showToast({ message: 'Successfully removed monitoring for Volume Down key!' })
  173. this.text = "Monitoring for Volume Down key has been removed."
  174. } catch (error) {
  175. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  176. JSON.stringify(error, ["code", "message"]));
  177. this.getUIContext()
  178. .getPromptAction()
  179. .showToast({ message: 'Failed to remove monitoring for Volume Down key!' })
  180. this.text =
  181. `Failed to remove monitoring for Volume Down key: ${JSON.stringify(error, ["code", "message"])}`
  182. }
  183. })
  184. }.width('100%')
  185. .justifyContent(FlexAlign.Center)
  186. .margin({ top: 20, bottom: 20 })
  187. // 注册及去注册智控键事件
  188. // 若不支持智控键上滑及智控键下滑按键,则关闭用户入口
  189. if (this.showComponent) {
  190. Row() {
  191. Button('Add monitoring for Slide Up key')
  192. .onClick(() => {
  193. try {
  194. // 添加指定回调函数
  195. inputConsumer.on('keyPressed', this.options3, this.slideUpCallBackFunc);
  196. this.getUIContext()
  197. .getPromptAction()
  198. .showToast({ message: 'Successfully added monitoring for Slide Up key!' })
  199. this.text = "Monitoring for Slide Up key has been added."
  200. } catch (error) {
  201. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  202. JSON.stringify(error, ["code", "message"]));
  203. this.getUIContext()
  204. .getPromptAction()
  205. .showToast({ message: 'Failed to add monitoring for Slide Up key!' })
  206. this.text = `Failed to add monitoring for Slide Up key: ${JSON.stringify(error, ["code", "message"])}`
  207. }
  208. })
  209. }.width('100%')
  210. .justifyContent(FlexAlign.Center)
  211. .margin({ top: 20, bottom: 20 })
  212. Row() {
  213. Button('Remove monitoring for Slide Up key')
  214. .onClick(() => {
  215. try {
  216. // 取消指定回调函数
  217. inputConsumer.off('keyPressed', this.slideUpCallBackFunc);
  218. this.getUIContext()
  219. .getPromptAction()
  220. .showToast({ message: 'Successfully removed monitoring for Slide Up key!' })
  221. this.text = "Monitoring for Slide Up key has been removed."
  222. } catch (error) {
  223. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  224. JSON.stringify(error, ["code", "message"]));
  225. this.getUIContext()
  226. .getPromptAction()
  227. .showToast({ message: 'Failed to remove monitoring for Slide Up key!' })
  228. this.text =
  229. `Failed to remove monitoring for Slide Up key: ${JSON.stringify(error, ["code", "message"])}`
  230. }
  231. })
  232. }.width('100%')
  233. .justifyContent(FlexAlign.Center)
  234. .margin({ top: 20, bottom: 20 })
  235. Row() {
  236. Button('Add monitoring for Slide Down key')
  237. .onClick(() => {
  238. try {
  239. // 添加指定回调函数
  240. inputConsumer.on('keyPressed', this.options4, this.slideDownCallBackFunc);
  241. this.getUIContext()
  242. .getPromptAction()
  243. .showToast({ message: 'Successfully added monitoring for Slide Down key!' })
  244. this.text = "Monitoring for Slide Down key has been added."
  245. } catch (error) {
  246. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  247. JSON.stringify(error, ["code", "message"]));
  248. this.getUIContext()
  249. .getPromptAction()
  250. .showToast({ message: 'Failed to add monitoring for Slide Down key!' })
  251. this.text = `Failed to add monitoring for Slide Down key: ${JSON.stringify(error, ["code", "message"])}`
  252. }
  253. })
  254. }.width('100%')
  255. .justifyContent(FlexAlign.Center)
  256. .margin({ top: 20, bottom: 20 })
  257. Row() {
  258. Button('Remove monitoring for Slide Down key')
  259. .onClick(() => {
  260. try {
  261. // 取消指定回调函数
  262. inputConsumer.off('keyPressed', this.slideDownCallBackFunc);
  263. this.getUIContext()
  264. .getPromptAction()
  265. .showToast({ message: 'Successfully removed monitoring for Slide Down key!' })
  266. this.text = "Monitoring for Slide Down key has been removed."
  267. } catch (error) {
  268. hilog.error(DOMAIN, 'InputConsumer', `Unsubscribe execute failed, error: %{public}s`,
  269. JSON.stringify(error, ["code", "message"]));
  270. this.getUIContext()
  271. .getPromptAction()
  272. .showToast({ message: 'Failed to remove monitoring for Slide Down key!' })
  273. this.text =
  274. `Failed to remove monitoring for Slide Down key: ${JSON.stringify(error, ["code", "message"])}`
  275. }
  276. })
  277. }.width('100%')
  278. .justifyContent(FlexAlign.Center)
  279. .margin({ top: 20, bottom: 20 })
  280. }
  281. Row() {
  282. Text(this.text)
  283. }
  284. .width('100%')
  285. .justifyContent(FlexAlign.Center)
  286. }.width('100%').height('100%')
  287. }
  288. }
在 指南 中进行搜索
请输入您想要搜索的关键词