×

Discussion Board

Results 1 to 10 of 10

Thread: keypress

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    keypress

    hi my key press codes is below
    Code:
     public void keyPressed(int keyCode) {
            
    		// what game action does this key map to?
    		int gameAction = getGameAction(keyCode);
    
    		if(gameAction == RIGHT) {
    			x += dx;
                          
    		} else if(gameAction == LEFT) {
    			x -= dx;
                           
    		} else if(gameAction == UP) {
    			y -= dy;
                  
    		} else if(gameAction == DOWN) {
    			y += dy;
                      
    		}else if(gameAction== FIRE)
                    {
                        setX=x;
                        setY=y;
                      mustRepaint=false;
                         
                     
                    }
                          repaint();
    
        }
    with this codes i need to keep pressing the left/right/down/up key in order for the image to move. Can anyone teach me how can i just hold the left/right/down/up key and the image will move continuously?

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: keypress

    Hi,
    What exactly is happening..right now in the code,
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  3. #3
    Registered User
    Join Date
    Feb 2007
    Location
    Tampere
    Posts
    370

    Re: keypress

    Hi,

    Refer this

    Use keyevents to move the pic. Change coordinates of the pic etc... as per your requirement
    Regards

    - MVP
    "Dare To Dream" - A.P.J.Kalam

  4. #4
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: keypress

    Hi,

    you can handle continuous key press events by implementing also keyRepeated and/or keyReleased methods.

    As an example, you could store the pressed key in the keyPressed method, and then reset it within the keyReleased one. Then, within your app main loop, by checking if and which key is currently pressed, you can actually implement your contious key press handling.

    Note that, if you're using GameCanvas, you can use the getKeyStates() method to implement it in an easier way.

    Hope it helps,
    Pit

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    Re: keypress

    Quote Originally Posted by jappit View Post
    Hi,

    you can handle continuous key press events by implementing also keyRepeated and/or keyReleased methods.

    As an example, you could store the pressed key in the keyPressed method, and then reset it within the keyReleased one. Then, within your app main loop, by checking if and which key is currently pressed, you can actually implement your contious key press handling.

    Note that, if you're using GameCanvas, you can use the getKeyStates() method to implement it in an easier way.

    Hope it helps,
    Pit
    Hi jappit.
    i've tried what you've advise there.
    this is my code:
    Code:
    public void keyPressed(int keyCode) {
            // what game action does this key map to?
            int gameAction = getGameAction(keyCode);
            
            if(gameAction == RIGHT) {
                x += dx;
            } else if(gameAction == LEFT) {
                x -= dx;
            } else if(gameAction == UP) {
                y -= dy;
            } else if(gameAction == DOWN) {
                y += dy;
            }else if(gameAction== FIRE) {
                setX=x;
                setY=y;
                mustRepaint=false;
            }
            repaint();
            
        }
        
        public void keyRepeated(int keyCode){
            int gameAction = getGameAction(keyCode);
            while(gameAction == RIGHT) {
                x += dx;
            } while(gameAction == LEFT) {
                x -= dx;
            } while(gameAction == UP) {
                y -= dy;
            } while(gameAction == DOWN) {
                y += dy;
            }
        }
    Logically it should be able to continously move while i hold down the buttons right, but it seems that only keypress is working, while keyrepeated is not.
    i have to tap the buttons repeatly to make it move
    Thanks for the help ^^

  6. #6
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: keypress

    Are you repainting your Canvas instance at each keyRepeated() call? Or, similarly, have you a thread that periodically repaints it? You should do this, to actually see the Image movement.

    Pit

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    Re: keypress

    Quote Originally Posted by jappit View Post
    Are you repainting your Canvas instance at each keyRepeated() call? Or, similarly, have you a thread that periodically repaints it? You should do this, to actually see the Image movement.

    Pit
    Hi jappit.
    yes i will repaint my instance at every keypress.
    but i duno how to go about doing it.
    Thanks

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    47

    Re: keypress

    Hi,
    What exactly is happening..right now in the code,
    Thanks,


    --------------------------------------------------------------------------------

    R a j - The Kernel
    hi currently is that my keypress allow the image to move but i have to keep pressing the key in order for it to move one distance.
    Now i want the image to move continuously with while my key is pressed. and only stop when it is released.

  9. #9
    Nokia Developer Champion
    Join Date
    Nov 2007
    Location
    Rome, Italy
    Posts
    2,406

    Re: keypress

    I've missed one important thing in your code: you shouldn't use while() loops to check key codes in your keyRepeated() method. Doing this way, you actually indefinitely loop your application. Instead, use if/else blocks as in your keyPressed() method, and it should work fine.

    Pit

  10. #10
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: keypress

    Hi,
    Thats perfect idea ..pit,
    It will work,shirodas do as stated by the pit.
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Similar Threads

  1. [Announce] keypress module 1.0.5
    By y.a.k in forum Symbian
    Replies: 74
    Last Post: 2013-07-04, 17:40
  2. simulating keypress
    By selvynz in forum Symbian
    Replies: 28
    Last Post: 2010-08-20, 12:26
  3. Replies: 8
    Last Post: 2009-07-28, 03:38
  4. Replies: 2
    Last Post: 2007-10-18, 00:02
  5. Replies: 1
    Last Post: 2007-07-09, 04:09

Posting Permissions

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