开发者

Android: onKeyDown does not trigger until after mouse click

开发者 https://www.devze.com 2023-03-25 07:32 出处:网络
I have an onK开发者_StackOverfloweyDown 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 app

I have an onK开发者_StackOverfloweyDown 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()
 {


 }

}


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
}


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.

0

精彩评论

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