How to assign an action to the physical volume keys in Codename One?

房东的猫 提交于 2021-02-08 07:50:53

问题


I would like my Codename One app to react to volume up / down keys (the physical keys that lay on the device side). Actually I already have a button that does the action but I would also like to offer the possibility to use the volume keys to launch the action.

I found the method keyPressed() which prototype reads

public void keyPressed(int keyCode)

but I was not able to actually find the keyCode constant for volume up / down.

How can I assign the volume up / down keys to an action with Codename One ?

Any help appreciated,


回答1:


Key pressed/released won't be invoked for volume since that is way too deep in the native call chain and very device dependent. Volume keys are always treated as a special case so we make them do "what's expected" and provide some tailored callbacks but we don't support the use case of "re-purposing" volume keys.

It might be possible by changing the AndroidImplementation class to include a special case (possibly triggered via a display property) and submitting a pull request for that.




回答2:


hope it's OK to jump on this thread: @Shai, you mention that CN1 "provides some tailored callbacks" for the volume buttons. I tried finding this in the documentation and the code (searching on "volume") but didn't succeed - can you give a hint on how to get such callbacks? My use case is to check if a volume button is pushed when starting up the app and take some special actions.



来源:https://stackoverflow.com/questions/40976239/how-to-assign-an-action-to-the-physical-volume-keys-in-codename-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!