Handleing key events


Last Updated:

  1. krishnan

    krishnan Member This Topic's Starter

    Joined:
    Jun 10, 2010
    Messages:
    8
    Likes Received:
    1
    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

    Joined:
    Apr 13, 2010
    Messages:
    17
    Likes Received:
    5
    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...