如何欺骗箭头键到我在android上的活动

时间:2022-11-06 17:23:52

I am trying to override the volume buttons to act as Up/Down arrow keys (i.e. they should move focus around on all of my activities focus-able Views.)

我试图覆盖音量按钮以充当向上/向下箭头键(即他们应该将焦点移动到我的所有活动可聚焦视图上。)

To do so I am overriding my activities dispatchKeyEvent() method note that I also tried onKeyDown() but some portion of my volume key events was still going thru to the system, my device has audible feedback when you change the volume. I could still hear the beep but the volume was not actually being changed. Switching to dispatchKeyEvent() took away the beep coming from the system.

为此,我重写了我的活动dispatchKeyEvent()方法请注意我也尝试过onKeyDown()但是我的音量键事件的某些部分仍然通过系统,我的设备在你改变音量时有声音反馈。我仍然可以听到哔哔声,但音量实际上并未改变。切换到dispatchKeyEvent()会消除系统发出的蜂鸣声。

Here is my current dispatchKeyEvent() method:

这是我当前的dispatchKeyEvent()方法:

@Overridepublic boolean dispatchKeyEvent(KeyEvent ke){    int keyCode = ke.getKeyCode();    if(ke.getAction() == KeyEvent.ACTION_DOWN){        print("press " + keyCode);        if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)         {             mTxt.postDelayed(pressDown, 600);            return true;        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)         {             KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP);             dispatchKeyEvent(key);             return true;        }     }else if(ke.getAction() == KeyEvent.ACTION_UP){        print("release " + keyCode);        if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)         {             /*KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN);             dispatchKeyEvent(keyUp);*/            return true;        }else if(keyCode == KeyEvent.KEYCODE_VOLUME_UP)         {             KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_UP);             dispatchKeyEvent(keyUp);            return true;        }     }    return super.dispatchKeyEvent(ke);}

the way it is set up now it is using different techniques for up and down, but neither are working correctly to shift focus. For the Up key I simply make a manual call to dispatchKeyEvent() passing it the d-pad up key and the same action that took place on the volume button (so when I press it should press, and when I release it should release) My output for the up button looks like this:

现在它的设置方式是上下使用不同的技术,但两者都没有正确地转移焦点。对于Up键,我只需手动调用dispatchKeyEvent(),将d-pad up键和音量按钮上发生的相同操作传递给它(所以当我按下它时应该按下,当我释放时应该释放)我的up按钮输出如下所示:

press 24press 19release 24release 19

for the down button I tweaked it slightly because I thought maybe the fact that the dpad press was happening before the volume release was breaking it, so I made a runnable that will both press and release the dpad button

对于向下按钮我略微调整它因为我认为可能事实是在音量释放打破它之前发生了dpad按压,所以我做了一个可以按下并释放dpad按钮的可运行

    pressDown = new Runnable(){        @Override        public void run(){            KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN);             dispatchKeyEvent(key);             KeyEvent keyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN);             dispatchKeyEvent(keyUp);        }    };

and I delay that by part of a second so that I have a chance to release the volume. My output for the down button looks like this:

我延迟了一段时间,以便我有机会释放音量。我按下按钮的输出如下所示:

press 25release 25press 20release 20

if I plug in a usb keyboard to my device and press the arrow keys the focus moves correctly and I see the output for up:

如果我将USB键盘插入我的设备并按下箭头键,焦点会正确移动,我会看到输出为up:

press 19release 19

and for down:

并且为了:

press 20release 20

The only difference (so far as I can tell) is that the focus actually moves correctly when I press the arrows on my keyboard, and doesn't move at all when I press the volume buttons (which spoof the arrow buttons)

唯一的区别(据我所知)是当我按下键盘上的箭头时焦点实际上正确移动,当我按下音量按钮时(根据箭头按钮欺骗),焦点根本不移动

Am I overlooking something here? Can anyone help me to figure out how to spoof arrow keys to my activity so that they will actually move focus correctly?

我在这里俯瞰什么吗?任何人都可以帮我弄清楚如何欺骗我的活动箭头键,以便他们实际上正确地移动焦点?

1 个解决方案

#1


2  

I figured out eventually that you need to use the Instrumentation class to send the key events, and you have to do it off of the main thread for some reason. Here are the snippets that will send down and up:

我最终发现你需要使用Instrumentation类来发送键事件,并且由于某种原因你必须从主线程中完成它。以下是将向下发送的片段:

new Thread(new Runnable() {            @Override   public void run() {                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);   }   }).start();new Thread(new Runnable() {            @Override   public void run() {                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);   }   }).start();

If you use these insdie the overridden dispatchKeyEvent from the question it will correctly move focus around.

如果您使用这些insdie重写的dispatchKeyEvent,它将正确地移动焦点。

#1


2  

I figured out eventually that you need to use the Instrumentation class to send the key events, and you have to do it off of the main thread for some reason. Here are the snippets that will send down and up:

我最终发现你需要使用Instrumentation类来发送键事件,并且由于某种原因你必须从主线程中完成它。以下是将向下发送的片段:

new Thread(new Runnable() {            @Override   public void run() {                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);   }   }).start();new Thread(new Runnable() {            @Override   public void run() {                        new Instrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_UP);   }   }).start();

If you use these insdie the overridden dispatchKeyEvent from the question it will correctly move focus around.

如果您使用这些insdie重写的dispatchKeyEvent,它将正确地移动焦点。