[Solved] - OnKeyDown

Put your problem here if it does not fit any of the other categories.

[Solved] - OnKeyDown

Postby res » Tue Nov 27, 2007 6:42 pm

I have a View which displays an image from the drawable res directory.

onDraw is working fine but when I try to use onKeyDown to respond to key events nothing is happening. I've placed a break point at the beginning of the method and using the debugger nothing happens.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.        @Override
  2.  
  3.         public boolean onKeyDown(int keyCode, KeyEvent event)
  4.  
  5.         {
  6.  
  7.                 if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
  8.  
  9.                         ypos+=10;
  10.  
  11.                
  12.  
  13.                 return true;
  14.  
  15.         }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


yPos is the co-ordinate of the image I'm attempting to move.

I'm missing something but I have no idea what.
Last edited by res on Tue Nov 27, 2007 7:42 pm, edited 2 times in total.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Top

Postby lordhong » Tue Nov 27, 2007 6:51 pm

what view you using? does the view supports keyevents?
lordhong
Developer
Developer
 
Posts: 42
Joined: Thu Nov 22, 2007 5:25 am
Location: New York

Postby res » Tue Nov 27, 2007 6:54 pm

I'm using the standard View (android.view.View). The documentation found at http://code.google.com/android/referenc ... /View.html states onKeyDown as an event.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby plusminus » Tue Nov 27, 2007 6:56 pm

Hello res,

please give us some more code (are you extending (Base)View or already an specialized View) so that we can see were your problem really is, just as lordhong mentioned above.

Perhaps you should call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.invalidate(); // makes this View being repainted
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

after changing your variable. :?:

Let us know if it helped or not.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby res » Tue Nov 27, 2007 7:11 pm

I'm extending the base View with

Code: Select all
class ShellView extends View


constructor

Code: Select all
   public ShellView(Context context, AttributeSet attrs, Map inflateParams){
      super(context,attrs,inflateParams);
      
      mImage = context.getResources().getDrawable(R.drawable.ball);
   }



here is my simple onDraw method

Code: Select all
@Override
   protected void onDraw(Canvas canvas)
   {
      super.onDraw(canvas);
      
      mImage.setBounds(xpos,ypos,xpos+mImage.getIntrinsicWidth(),ypos+mImage.getIntrinsicHeight());
      mImage.draw(canvas);
      
      invalidate();
      canvas.restore();
   }


And here is the onKeyDown

Code: Select all
@Override
   public boolean onKeyDown(int keyCode, KeyEvent event)
   {
      if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
         ypos+=10;
      else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
         ypos-=10;
      else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
         xpos-=10;
      else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
         xpos+=10;

      return true;
   }


In my onDraw method if I do something like ypos++ the ball will move so I know the screen is updating. It appears the key events aren't being recognised.

edit: The original code is based on GameDemo1 which bounces a blue ball around the screen.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby res » Tue Nov 27, 2007 7:34 pm

Ok, I just tried onMotionEvent and I can get that to work fine.

onKeyDown and onKeyUp are not firing any events when I press a key on the emulator keypad/d-pad.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Top

Postby res » Tue Nov 27, 2007 7:41 pm

SOLUTION

you need setFocusable(true) in the View constructor.

searching the discussion groups turned up this
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby plusminus » Tue Nov 27, 2007 7:49 pm

Hello res,

yes, you need to: setFocusable(true); because KeyEvents happen on the View that currently obtains the focus !

Think about the call of invalidate() in the draw()-method, as invalidate causes an draw() in the near future.(JavaDoc says exactly so ^^).
You should call invalidate() in the end of your onKeyDown()-method, what is more logical.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Anil » Thu Mar 06, 2008 8:08 pm

plusminus wrote:Hello res,

yes, you need to: setFocusable(true); because KeyEvents happen on the View that currently obtains the focus !

Think about the call of invalidate() in the draw()-method, as invalidate causes an draw() in the near future.(JavaDoc says exactly so ^^).
You should call invalidate() in the end of your onKeyDown()-method, what is more logical.

Regards,
plusminus


Hello plusminus,
I have the same problem but am having trouble understanding.
If I have a ListView, the user can select a list item, or click an on screen button. So why should I force focus on the list? Also, I see more than one sample in ApiDemos without the call to invalidate() at the end.

thanks,
Anil
Anil
Developer
Developer
 
Posts: 44
Joined: Fri Jan 04, 2008 9:15 pm

Postby plusminus » Fri Mar 07, 2008 12:07 am

Hello Anil,

what I meant was that he should not be calling invalidate() in onDraw(), what might produce like an infinite loop.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby alpesh_harsoda » Fri Nov 14, 2008 11:43 am

hiiii
what is the type mimage?
is it inputstream or ImageView?
waiting for reply,
thnks.
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Postby vadimz » Fri Aug 14, 2009 10:42 am

plusminus wrote:
yes, you need to: setFocusable(true); because KeyEvents happen on the View that currently obtains the focus !


For me it helped only when I made so:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setFocusable(true);
  2. setFocusableInTouchMode(true);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Second was required!
vadimz
Developer
Developer
 
Posts: 44
Joined: Thu Jul 30, 2009 5:04 pm
Location: Moldova

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests