简体   繁体   English

如何从屏幕(虚拟)键盘上获得压力? (机器人)

[英]How to get pressure from on screen (Virtual) keyboard? (Android)

I need to get some information from the on screen keyboard such as pressure, KeyDown and KeyUp in Android but don't know how to do that. 我需要从屏幕键盘上获取一些信息,例如Android中的压力,KeyDown和KeyUp,但不知道该怎么做。

The android official site says that: android官方网站说:

Note: When handling keyboard events with the KeyEvent class and related APIs, you should expect that such keyboard events come only from a hardware keyboard. 注意:在使用KeyEvent类和相关API处理键盘事件时,应该期望此类键盘事件仅来自硬件键盘。 You should never rely on receiving key events for any key on a soft input method (an on-screen keyboard). 您绝不应该依靠软输入法(屏幕键盘)上的任何键来接收键事件。

I also tried the following method without success. 我也尝试了以下方法,但没有成功。 It actually works with the hardware keys like back button. 它实际上可以与后退按钮之类的硬件键一起使用。

myEditText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            return false;
        }
    });

I was thinking of finding a way of extending the on screen keyboard, but still no success! 我当时想找到一种扩展屏幕键盘的方法,但仍然没有成功! Does anyone have ever tried doing this? 有没有人尝试过这样做? I'd appreciate your help in advance. 非常感谢您的帮助。

UPDATE: After trying many solutions I came up with the same solution of using my own keyboard, suggested by krossovochkin, ultimately. 更新:尝试了多种解决方案后,最终我得到了由krossovochkin建议的使用自己的键盘的相同解决方案。 It seems that the solution is not too bad if one wants to modify the Android's keyboard as a new one. 如果要修改Android键盘为新键盘,该解决方案似乎还不错。 This way, it appears in the "Settings --> Input Methods" so that the user can switch to the new keyboard, which is good since it is accessible from all other apps. 这样,它会出现在“设置->输入法”中,以便用户可以切换到新键盘,这很好,因为可以从所有其他应用程序访问它。 Since it is not yet clear that whether it is possible to get the pressure from the standard virtual keyboard, therefore I thought the question could be left open. 由于尚不清楚是否可以从标准虚拟键盘上获得压力,因此我认为这个问题可能会悬而未决。

You can try to call getPressure() method from MotionEvent Link: http://developer.android.com/reference/android/view/MotionEvent.html#getPressure%28int%29 你可以尝试调用getPressure()从方法MotionEvent链接: http://developer.android.com/reference/android/view/MotionEvent.html#getPressure%28int%29

Code snippet: 程式码片段:

view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float pressure = event.getPressure();
    }
});

UPDATE: You can create your own keyboard and getPressure from it. 更新:您可以创建自己的键盘并从中获取压力。 Maybe user will not like using your keyboard instead of his default keyboard. 也许用户不喜欢使用您的键盘而不是默认键盘。 But I think this is the best solution for your situation. 但是我认为这是针对您情况的最佳解决方案。

More information about: 有关以下内容的更多信息:
KeyboardView: http://developer.android.com/reference/android/inputmethodservice/KeyboardView.html 键盘视图: http : //developer.android.com/reference/android/inputmethodservice/KeyboardView.html
Example of creating your own keyboard: https://github.com/rciovati/Android-KeyboardView-Example 创建自己的键盘的示例: https : //github.com/rciovati/Android-KeyboardView-Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM