简体   繁体   中英

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.

The android official site says that:

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. 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. It seems that the solution is not too bad if one wants to modify the Android's keyboard as a new one. 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

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
Example of creating your own keyboard: https://github.com/rciovati/Android-KeyboardView-Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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