Android:onKeyDown直到鼠标点击后才会触发

发布于 2024-11-27 17:00:48 字数 1040 浏览 0 评论 0原文

我有一个 onKeyDown 事件,一旦触发,它应该显示图像,但我注意到,尽管多次按下相应的键,但直到我用鼠标单击画布上的任意位置时,图像才会出现。对实际问题以及如何进行有什么建议吗?这个概念相当新,所以不太确定可能缺少什么。 *完整编辑和粘贴类。 谢谢

public class BuccaneerView extends TileView {

public static final int PLAYER = 1;
public static final int GREEN_STAR = 2;



Coordinate P_Location;

public BuccaneerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initBucc();
}

private void initBucc() {

    this.setFocusable(true);



    Resources r = this.getContext().getResources();

    resetTiles(4);
    loadTile(PLAYER, r.getDrawable(R.drawable.aerialplayer));
    loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));

    /**/

    P_Location = new Coordinate(5,5);
    setTile(PLAYER, P_Location.x, P_Location.y);

}


 @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {

     if (keyCode == KeyEvent.KEYCODE_SPACE) 
     {

         setTile(GREEN_STAR, 1, 0);         
     }


     return super.onKeyDown(keyCode, msg);
 }

 public void update()
 {


 }

}

I have an onKeyDown Event which is supposed to display an image once triggered, but I have noticed that despite pressing the corresponding key several times, the image does not appear until I click anywhere on the canvas with my mouse. Any suggestions on the actual problem and how to proceed? Pretty new to the concept so not quite sure what may be missing.
*Edited and pasted class in its entirety.
Thanks

public class BuccaneerView extends TileView {

public static final int PLAYER = 1;
public static final int GREEN_STAR = 2;



Coordinate P_Location;

public BuccaneerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initBucc();
}

private void initBucc() {

    this.setFocusable(true);



    Resources r = this.getContext().getResources();

    resetTiles(4);
    loadTile(PLAYER, r.getDrawable(R.drawable.aerialplayer));
    loadTile(GREEN_STAR, r.getDrawable(R.drawable.greenstar));

    /**/

    P_Location = new Coordinate(5,5);
    setTile(PLAYER, P_Location.x, P_Location.y);

}


 @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {

     if (keyCode == KeyEvent.KEYCODE_SPACE) 
     {

         setTile(GREEN_STAR, 1, 0);         
     }


     return super.onKeyDown(keyCode, msg);
 }

 public void update()
 {


 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑白记忆 2024-12-04 17:00:48

您似乎将 onKeyDown 视为您的 on 方法之一。

return super.onKeyDown(keyCode, msg);

这样做是一件坏事,就好像您调用了这个函数并想要返回按下的键一样。将其更改为简单的 return false 这意味着您正在处理键盘正在执行的操作。

编辑

您使用 onKeyDown 而不是 onKey 有什么原因吗?这是我使用的一些额外代码,它使用一个布尔数组(pressedKeys,长度为 128),稍后您可以使用它来检查该数组以查看是否按下了某个键,

public boolean onKey(View v, int keyCode, KeyEvent event) 
{
     if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) 
     {
         if(keyCode > 0 && keyCode < 127)
             pressedKeys[keyCode] = true;
     }

    if (event.getAction() == android.view.KeyEvent.ACTION_UP) 
    {
        if(keyCode > 0 && keyCode < 127)
            pressedKeys[keyCode] = false;
    }

    keyEventsBuffer.add(keyEvent);
}
   return false;
}

因此您可以说

If(pressedKeys[KeyYouWantToCheck])
{
//Do some stuff if that key is down
}

You seem to be treating onKeyDown as one of your on methods.

return super.onKeyDown(keyCode, msg);

Is a bad thing to do, its as if you have called this function and want to return what key has been pressed. Change it to simply be return false which will mean you are handling what they keyboard is doing.

EDIT

Is there any reason you use onKeyDown and not onKey? Here is some extra code which I use, it uses an array of booleans (pressedKeys, which is 128 in length) and you can later use it to check the array to see if a key is pressed

public boolean onKey(View v, int keyCode, KeyEvent event) 
{
     if (event.getAction() == android.view.KeyEvent.ACTION_DOWN) 
     {
         if(keyCode > 0 && keyCode < 127)
             pressedKeys[keyCode] = true;
     }

    if (event.getAction() == android.view.KeyEvent.ACTION_UP) 
    {
        if(keyCode > 0 && keyCode < 127)
            pressedKeys[keyCode] = false;
    }

    keyEventsBuffer.add(keyEvent);
}
   return false;
}

So with this you can then say

If(pressedKeys[KeyYouWantToCheck])
{
//Do some stuff if that key is down
}
因为看清所以看轻 2024-12-04 17:00:48

据猜测,按键事件不会传递到您设置按键侦听器的任何位置。如果之间存在另一个视图,该视图是侦听器并且停止关键事件的传播(即从此方法返回 true),则可能会发生这种情况。有一些视图默认执行此操作(例如大多数键的 EditText)。如果您可以编辑您的问题并包含更多代码,或者描述您的活动是如何设置的,那将会很有帮助。

通过“单击画布”,您可能会更改焦点并使关键事件传递到不同的视图。这可以解释为什么您在单击后突然看到关键侦听器正在工作。

At a guess, the key event is not being delivered to whatever you have set the key listener on. This can happen if there is another view in between which is a listener and which stops the propagation of the key event (i.e. returning true from this method). There are some views which do this by default (e.g. EditText for most keys). It would be helpful if you could edit your question and include more code, or describe how your activity is setup.

By 'clicking on the canvas' you are probably changing the focus and making the key event being delivered to a different view. This could explain why you suddenly see the key listener working after clicking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文