如何箭头键来欺骗我的机器人活动 [英] how to spoof arrow keys to my activity on android

查看:200
本文介绍了如何箭头键来欺骗我的机器人活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图覆盖音量按钮作为上/下箭头键(即他们应该围绕继续前行我所有的活动重点关注,能够查看。)

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.)

要做到这一点,我重写我的活动 dispatchKeyEvent()方法,注意,我也尝试过的onkeydown()但我的音量键事件的一些部分还是会通到系统中,我的设备有声音反馈,当你改变音量。我仍然可以听到蜂鸣声,但成交量实际上并没有被改变。切换到 dispatchKeyEvent()抢走了蜂鸣声从系统中来了。

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()方法:

@Override
public 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);
}

它设置了,现在它正在使用不同的技术来上下,但也都正常工作,将焦点切换的方式。对于向上键我只是做它传递方向键上键和发生在音量按钮(所以当我preSS应该preSS,当同样的动作手动调用dispatchKeyEvent()我释放它应该释放)我的输出向上按钮看起来是这样的:

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:

press 24
press 19
release 24
release 19

有向下按钮,我调整了稍微因为我想,也许一个事实,即DPAD preSS正在发生前的量产版被打破,所以我所做的都将preSS和释放DPAD可运行按钮

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

    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 25
release 25
press 20
release 20

如果我插上USB键盘到我的设备和preSS焦点移到正确的方向键,我看到输出了:

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:

press 19
release 19

和换下来:

press 20
release 20

唯一的区别(只要我可以告诉)是关注的焦点实际上移动正确的,当我preSS我的键盘上的箭头,并且不动弹​​的时候我preSS音量按钮(其中恶搞箭头按钮)

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?

推荐答案

我想通了,最终,你需要使用仪器仪表类发送的关键事件,你必须做掉由于某种原因,主线程。以下是将发送向下和向上的片段:

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:

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();

如果你使用这些insdie的问题,将正确地移动重点围绕覆盖dispatchKeyEvent。

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

这篇关于如何箭头键来欺骗我的机器人活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆