简体   繁体   English

Android:在选择,聚焦或按下按钮时重复执行dispatchKeyEvent

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

My layout contains a button and an editText view. 我的布局包含一个按钮和一个editText视图。 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 . 请参见View#setOnTouchListener()View.OnTouchListener When you receive a MotionEvent with ACTION_DOWN , you would start some repetition (eg, 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. 当您收到一个MotionEventACTION_DOWN ,你会开始有些重复(例如,创建一个Handler在你的活动,并要求处理程序#sendMessageDelayed() ,其中延迟是您想要使用任何重复延迟。由于每个消息被接收,你首先执行您的按键调度,然后再次调用sendMessageDelayed()安排下一次按键操作;在MotionEvent.ACTION_UP ,您将调用Handler#removeMessages()取消待处理的计划事件。

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. 至于模拟按键事件,您描述的dispatchKeyEvent()方法可能会起作用,具体取决于您要实现的目标:如果这对您不起作用,则需要进一步说明。

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

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