Results 1 to 11 of 11
  1. #1
    ellisean55 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Lightbulb KeyEvent Problems

    Hello there,

    I am currently working on a simple game of Snake to be run on Android 2.2. I want to use the soft-keys below the main screen so that the game window can fill the whole screen and fingers will not obstruct the game view.
    My problem is that I cannot seem to get the program to recognize the key presses at all... here is the section of code in question....



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


    super.onKeyDown(keyCode, event);

    if (keyCode == KeyEvent.KEYCODE_MENU) {
    if (mMode == READY | mMode == LOSE) {
    /*
    * At the beginning of the game, or the end of a previous one,
    * we should start a new game.
    */
    initNewGame();
    setMode(RUNNING);
    update();
    return (true);
    }


    if (mDirection != SOUTH) {
    mNextDirection = NORTH;
    }
    return (true);
    }


    //if statements for turning left via menu key
    if (keyCode == KeyEvent.KEYCODE_MENU) { //left
    if (mDirection == NORTH) {
    mNextDirection = WEST;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_MENU) { //left
    if (mDirection == WEST) {
    mNextDirection = SOUTH;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_MENU) { //left
    if (mDirection == SOUTH) {
    mNextDirection = EAST;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_MENU) { //left
    if (mDirection == EAST) {
    mNextDirection = NORTH;
    }
    return (true);
    }


    //controls for turning right via search key
    if (keyCode == KeyEvent.KEYCODE_SEARCH) { //left
    if (mDirection == NORTH) {
    mNextDirection = EAST;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_SEARCH) { //left
    if (mDirection == WEST) {
    mNextDirection = NORTH;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_SEARCH) { //left
    if (mDirection == SOUTH) {
    mNextDirection = WEST;
    }
    return (true);
    }
    if (keyCode == KeyEvent.KEYCODE_SEARCH) { //left
    if (mDirection == EAST) {
    mNextDirection = SOUTH;
    }
    return (true);
    }




    return super.onKeyDown(keyCode, event);



    When the program is run, the menu and search keys do not cause any response. Hitting the back key exits the game, while hitting the home key causes the game to pause and then exit.

    Without posting the ENTIRE code ( ) , could the problem be any one of the following, or is it something that is obviously going over my head?
    • no focus set
    • a view that has not been focused properly
    • lack of knowledge / experience
    • or am i just being dumb


    To whoever answers this post, a thank you in advance

    Sean
    Last edited by ellisean55; 03-22-2012 at 03:49 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: KeyEvent Problems

    There's an Android subforum here you might have better luck in.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default Re: KeyEvent Problems

    Moved from New to Java.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ellisean55 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: KeyEvent Problems

    thanks for the move; wasn't totally sure where to post but I figured you guys would end up moving it anyways. Hopefully I can get this solved by the 6th... maybe then you guys could have you own home-brewed version of Snake on all your Android phones

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: KeyEvent Problems

    Beginning Android Game Programming (APress) starts with a Snake-like game as its first (non-Hello World) program.
    Can't remember what controls it uses (I have a feeling it's tilting the device), but I'll have a look if I remember this evening.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ellisean55 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: KeyEvent Problems

    Quote Originally Posted by Tolls View Post
    Can't remember what controls it uses (I have a feeling it's tilting the device), but I'll have a look if I remember this evening.
    Thank you; I hadn't thought of using the phones gyroscope. Depending on how that goes, it would be nice to later incorporate an option to use touch, tilt or the softkeys.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: KeyEvent Problems

    Had a quick skim through the book and they add a couple of buttons to the screen, which is what you're trying to avoid, so no luck there I'm afraid.
    I was thinking about how those 4 buttons function, though, and aren't they intercepted higher up in Android than your Activity?
    The MENU for example results in whatever menu items you have assigned to the Activity being displayed, and similarly for SEARCH which will access any attached search framework (I think).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    ellisean55 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: KeyEvent Problems

    Quote Originally Posted by Tolls View Post
    Had a quick skim through the book and they add a couple of buttons to the screen, which is what you're trying to avoid, so no luck there I'm afraid.
    I was thinking about how those 4 buttons function, though, and aren't they intercepted higher up in Android than your Activity?
    The MENU for example results in whatever menu items you have assigned to the Activity being displayed, and similarly for SEARCH which will access any attached search framework (I think).
    I was thinking the same thing, but I'm having trouble making my activity "appear" above all other activities.
    Any ideas?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default Re: KeyEvent Problems

    I think you're battling the OS though, which is rarely a good idea.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    ellisean55 is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default Re: KeyEvent Problems

    But i have found that in other open-sourced games, the menu and search keys are often used to open menus. In addition to this, when running the program on an emulator, a pc keyboard (for the function keys) works the game just fine, because the emulator uses the function keys as the soft keys on the phone.

  11. #11
    troll4u is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

Similar Threads

  1. Catching TAB via KeyEvent?
    By Toll in forum Advanced Java
    Replies: 6
    Last Post: 07-06-2011, 03:19 AM
  2. Listening KeyEvent from two JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-11-2010, 10:31 PM
  3. How to dispatchEvent(KeyEvent evt) to an applet
    By crikey in forum Java Applets
    Replies: 17
    Last Post: 09-24-2010, 10:43 PM
  4. Help in KeyEvent
    By chyrl in forum AWT / Swing
    Replies: 15
    Last Post: 05-26-2010, 08:12 AM
  5. JOptionPane KeyEvent
    By mine0926 in forum NetBeans
    Replies: 5
    Last Post: 05-05-2010, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •