开发者

Android: Repeat a dispatchKeyEvent while while a button is selected, focused, or pressed

My layout contains a button and an editText view. While the button is held down I want to have a keyboard key pressed over and over again until the button is released. Here is the basic concept:

while(button is held down){

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
                KeyEvent.KEYCODE_P));

dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
                KeyEvent开发者_如何学编程.KEYCODE_P));

}

Can anybody help me with this?


See View#setOnTouchListener() and View.OnTouchListener. When you receive a MotionEvent with ACTION_DOWN, you would start some repetition (e.g., create a Handler in your Activity, and call Handler#sendMessageDelayed(), where the delay is whatever repeat delay you want to use. As each message is received, you would first perform your key dispatches, and then call sendMessageDelayed() again to schedule the next key press. On MotionEvent.ACTION_UP, you would call Handler#removeMessages() to cancel the pending scheduled event.

As for simulating the keypress events, the dispatchKeyEvent() method you described might work, depending on what you want to achieve: would have to explain further, if that doesn't work for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜