Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7901927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:21:34+00:00 2026-06-03T09:21:34+00:00

The onKeyDown method does not function. When i press the button the bitmap s

  • 0

The onKeyDown method does not function. When i press the button the bitmap s does not move.
When it is done directly by putting j=j+5; inside canvas it works.
When did it directly in onkeydown j+5 then also it was’nt moving.

public class SmileyView extends SurfaceView implements SurfaceHolder.Callback {
        public SmileyView(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
            holder = getHolder();
             s= BitmapFactory.decodeResource(context.getResources(),R.drawable.smiley);
             holder.addCallback(this);
             sd=context.getResources().getDrawable(R.drawable.lander_plain);
        }

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

        return rv.dokey();
        }
    private int i=100;private int j=0;
        private Bitmap s;private SurfaceHolder holder;private RenderView rv ;
         boolean grun=true;long t=0;private boolean key=false;private Drawable sd;volatile private int f=0;
        public void resume()
        {
            rv=new RenderView();
        }


        @Override
        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
            grun=true;
            if(rv!=null)
            rv.start();
        }

        @Override
        public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
        try {
            grun =false;
            rv.join();
            rv=null;
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }


    class RenderView extends Thread  {

        RenderView(){
        super();

        }
        @Override
        public void run()
        {
            SurfaceHolder h=holder;
            while(grun)
            {
                Canvas ca=null;
                try{ca = h.lockCanvas(null);
                   synchronized (h) {

                dodraw(ca);
                   }
                }
                finally
                {
                    if(ca!=null)
                    h.unlockCanvasAndPost(ca);
                }
            }
        }
        public void dodraw(Canvas canvas)

        {   canvas.drawColor(Color.BLACK);
            canvas.drawBitmap(s, j, j, null);

            sd.setBounds(10, 10, 230, 180);
            sd.draw(canvas);
            key =false;

            canvas.restore();
        }

         boolean dokey()
        {
            j=j+5;
            return true;
        }
    }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:21:35+00:00Added an answer on June 3, 2026 at 9:21 am

    The view has to have focus in order to accept key events, and besides forcing focusable on the element, theres no good way to make your SmileyView take focus. Instead, Override onKeyDown in your activity to pass through the key event, if this surfaceview is the only thing on the screen:

    public class MyActivity extends Activity {
      private SmileyView mSmileyView
    
      @Override
      protected boolean onKeyDown(int keyCode, KeyEvent event) {
        return mSmileyView.onKeyDown(keyCode, event);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

document.onkeydown.toString() returns onkeydown function but I only want to get this function's name. Is
I have a function that is triggered by the onkeydown event of a textbox.
Is it possible to have keyboard event listener canvas.addEventListener('onkeydown', ev_keydown, false); like we have
I try to override this method and stuck with some problem public boolean onKeyDown(int
I have a Button/Edittext to reset my App-Settings.... If a press the button(no long
This code does not validate. This is counter-intuitive to me. Inputs belong in a
I need to show alert with yes no button on back key press for
I am having difficulty finding a cross-browser-compatible, Javascript-based method to attach a function to
hi guys after i call the method onKeyDown and my app closes without get
I am new to android development, making a simple game and using onKeyDown(.....) function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.