1. Check out our app, Forums for Android! Download and leave feedback here!

Handleing key events

Discussion in 'Application Development' started by krishnan, Jun 10, 2010.

  1. krishnan

    krishnan Member
    Thread Starter
    6

    Jun 10, 2010
    8
    1
    6
    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 (Text):
    1.  
    2.  
    3. public class DynamicMenu extends Activity{
    4. //implements  OnKeyListener {
    5.     Menu theMenu;
    6.     int i = 0;
    7.    /** Called  when the activity is first created. */
    8.     @Override
    9.     public  void onCreate(Bundle savedInstanceState) {
    10.          super.onCreate(savedInstanceState);
    11.          setContentView(R.layout.main);
    12.     }
    13.    
    14.    
    15.     @Override
    16.      public boolean onCreateOptionsMenu(Menu menu)
    17.     {
    18.         theMenu = menu;
    19.        new  MenuInflater(getApplication()).inflate(R.menu.sample, menu);
    20.         return (super.onCreateOptionsMenu(menu));
    21.     }
    22. //   @Override
    23. //    public boolean onKey(View v, int keyCode, KeyEvent event) {
    24. //       // TODO Auto-generated method stub
    25. //      System.out.println("###  onKey");
    26. //      return false;
    27. //   }
    28.  
    29. }
     

    Advertisement

  2. dev47

    dev47 Member
    16

    Apr 13, 2010
    17
    5
    16
    Here you go :)

    Code (Text):
    1.  
    2.     @Override
    3.     public boolean onKeyDown(int keyCode, KeyEvent msg) {
    4.         if (keyCode == KeyEvent.KEYCODE_BACK) {
    5.             log("Pressed Back!");
    6.             return true; // <- Will 'override' default behaviour
    7.  
    8.         }
    9.         return super.onKeyDown(keyCode, msg);
    10.     }
    11.  
     

Share This Page

Loading...