• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Handleing key events

krishnan

Newbie
Hi
I am new to android. and trying to check what key is pressed from the emulator keyboard.
Here is my code. Which is not giving me expected results. I have commented some of the code as that was not working.
Please let me know how i can fix this issue.

Code:
public class DynamicMenu extends Activity{
//implements  OnKeyListener {
    Menu theMenu;
    int i = 0;
   /** Called  when the activity is first created. */
    @Override
    public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
    }
    
    
    @Override
     public boolean onCreateOptionsMenu(Menu menu)
    {
        theMenu = menu;
       new  MenuInflater(getApplication()).inflate(R.menu.sample, menu);
        return (super.onCreateOptionsMenu(menu));
    }
//   @Override
//    public boolean onKey(View v, int keyCode, KeyEvent event) {
//       // TODO Auto-generated method stub
//      System.out.println("###  onKey");
//      return false;
//   }

}
 
Here you go :)

Code:
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent msg) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            log("Pressed Back!");
            return true; // <- Will 'override' default behaviour

        }
        return super.onKeyDown(keyCode, msg);
    }
 
Back
Top Bottom