×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    How to detect when/which key is pressed?

    Hi!

    I would like to make a program that increments counter when I press RIGHT or number 6 key and decrements it when I press LEFT or number 4. How can I do that?

    Should i use command listener? I try it but I didn't get it work.
    Last edited by qwerty_wwd; 2006-05-08 at 07:55.

  2. #2
    Regular Contributor
    Join Date
    Mar 2006
    Posts
    116

    Re: How to detect when/whitch key is pressed?

    hi,
    It would be much more easier to make use of the OfferKeyEventL() to achieve what u desire. in the OfferKeyEventL() Method u can get the Keycode of the key pressed. every key will have a unique key code. so once u know the key code for 6 or right button, u can compare that in the OfferKeyEventL() and if the comparison is true then it means that u have pressed 6 or right key, and then u can correspondingly perform the desired operation like incrementing a counter and so on.
    with regards,
    Hemanth.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Re: How to detect when/which key is pressed?

    Will that work in Java also?

  4. #4
    Super Contributor
    Join Date
    Feb 2006
    Posts
    689

    Re: How to detect when/which key is pressed?

    Hi,
    No command listener wont solve your problem.There are methods called keypressed,keyReleased and keyRepeated where u will recieve the key events.
    a particular value is there for key 6 and 4.u will get this from printing out the keyCode values.....Sorry this is the case with j2me

  5. #5
    Super Contributor
    Join Date
    Mar 2006
    Posts
    516

    Re: How to detect when/which key is pressed?

    Hi,

    the following document solves ur problem
    http://www.forum.nokia.com/info/sw.n..._v1_0.pdf.html

    Wang

  6. #6
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

    Re: How to detect when/which key is pressed?

    This tutorial takes you up to a stage where you are moving an image on screen in response to key presses. That should be easy to chance into incrementing counters.

    shmoove

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    28

    Re: How to detect when/which key is pressed?

    Quote Originally Posted by shmoove
    This tutorial takes you up to a stage where you are moving an image on screen in response to key presses. That should be easy to chance into incrementing counters.

    shmoove
    Thaks for the tutorial.

    How can I get this KeyRepeated event work? If I do it like this KeyPressed works fine but if I keep key pressed nothing happens.

    Code:
    protected void paint( Graphics g )
    	{
    		//set the current color of the Graphics contect to the specified RRGGBB colour
    		
                    
                    g.setColor( 0xFFFFFF );
                    g.fillRect( 90, 90, 30, 30 );
                    g.drawLine(50+Startup.counter,50,50+Startup.counter,60);
                    
                    g.setColor( colour );
                    g.fillRect( 50, 50, Startup.counter, 10 );
                    g.drawString(Integer.toString(Startup.counter),100,100,16|1);
    	}
    	
    	/* 
    	* called when a key is pressed and this Canvas is the
    	* current Displayable 
    	*/
    	protected void keyPressed( int keyCode )
    	{
    		//get the game action from the passed keyCode
    		int gameAction = getGameAction( keyCode );
    		switch( gameAction )
    		{
    			case LEFT:
    			//set current colour to red
    			colour = 0xFF0000; 
                            Startup.counter--;
    			break;
    			
    			case RIGHT:
    			//set current colour to green
    			colour = 0x00FF00;
                            Startup.counter++;
    			break;
    			
    					}
    		//schedule a repaint of the Canvas after each key press as
    		//currently do not have any main game loop to do this for us.
    		repaint();
    	}
    
    	/* 
    	* key repeated
    	*/
    	protected void keyRepeated( int keyCode )
    	{
                int gameAction = getGameAction( keyCode );
    		switch( gameAction )
    		{
    			case RIGHT:
                            Startup.counter++;
                            repaint();
    			break;
                    }
                    
    	}
    [EDIT]
    Sorry, that code works fine. My emulator just doesn't understand it.


    But now I have a new question. Is it possible to use numpad keys without using Canvas? I have tried but it says cannot find symbol: method getGameAction(int). I have imported javax.microedition.lcdui.*; but it won't help.
    Last edited by qwerty_wwd; 2006-08-22 at 13:15.

  8. #8
    Super Contributor
    Join Date
    Apr 2003
    Location
    USA, CA
    Posts
    7,191

    Re: How to detect when/which key is pressed?

    getGameAction() is a method in Canvas. Unless you are displaying a canvas you cannot use it. If you are using the high-level UI elements like Form and TextBox, you are stuck with using Commands

    Hartti

Similar Threads

  1. Handling key pressed events
    By yuava in forum Mobile Java General
    Replies: 8
    Last Post: 2011-07-08, 08:35
  2. Replies: 2
    Last Post: 2006-02-23, 06:42
  3. Replies: 2
    Last Post: 2005-12-01, 10:03
  4. How to detect center navi key is pressed with Edit key not hold.
    By hanwang in forum Symbian User Interface
    Replies: 1
    Last Post: 2004-01-12, 07:40
  5. How can i handling when two key pressed.
    By abovesun in forum Mobile Java Media (Graphics & Sounds)
    Replies: 4
    Last Post: 2003-09-27, 22:58

Posting Permissions

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