Results 1 to 18 of 18
  1. #1
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Help me with KeyListeners pls

    My Problem is that when I press the desired key it just Doesn't trigger
    I need some help with this this is my code:

    PHP Code:
    package me.tnt417.main;
    
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.applet.Applet;
    import java.awt.*;
    
    
    
    
    public class Main extends Applet implements KeyListener {
         /**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	private Rectangle rect;
         public void init(){
        	 this.addKeyListener(this);
        	 
        	 rect = new Rectangle(0, 0, 50, 50);
    
         }
         public void paint(Graphics g){
        	 setSize(500,500);
        	 
         	Graphics2D g2 = (Graphics2D)g;
         	g2.setColor(Color.CYAN);
         	g2.fill (rect);
         	 
          }
         @Override
    	public void keyPressed(KeyEvent e) {
    		if(e.getKeyCode() == KeyEvent.VK_RIGHT){
    			rect.setLocation(rect.x + 2, rect.y);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_LEFT){
    			rect.setLocation(rect.x - 2, rect.y);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_UP){
    			rect.setLocation(rect.x, rect.y + 2);
    		}
    		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
    			rect.setLocation(rect.x, rect.y - 2);
    			System.out.println("down");
    		}
    		repaint();
    	}
    
    	
    	public void keyReleased(KeyEvent e) {
    		
    		
    	}
    
    	
    	public void keyTyped(KeyEvent e) {
    		
    		
    	}
    }
    Last edited by tnt417; 09-19-2015 at 10:40 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Help me with KeyListeners pls

    Define "doesn't work".
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    the keyListeners dont trigger when i press a key

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    Also please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    there i did it. is that what u meant?

  6. #6
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    sorry a little sloppy

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    What tags did you use? With code tags, The code should look like this:
    Java Code:
       int x = 123; // assign a value to x
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    there we go

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    Ok, that's much better. Does the component with the listener have the focus?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    what do you mean?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    First off Thats too advanced for my level of coding.
    Secondly i'm using an Applet

  13. #13
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    also i'm using eclipse
    and I've seen many tutorials about this which work for them but NOT for me and I even copy the code EXACTLY

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    Thats too advanced for my level of coding.
    You need to understand focus to use keylisteners. The tutorial will give you some background and perhaps some coding ideas.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    you can now mark this as SOLVED I DID IT!!!

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    Can you explain what you did so anyone having the same problem can read this thread and find a solution?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    tnt417 is offline Member
    Join Date
    Sep 2015
    Posts
    21
    Rep Power
    0

    Default Re: Help me with KeyListeners pls

    I Simply Put this line of code in the init method:
    PHP Code:
    setFocusable(true);
    if you were using a JFrame it would be:
    PHP Code:
    //Whatever your JFrame caller is--> frame.setFocusable(true)

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Help me with KeyListeners pls

    Thanks
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 9
    Last Post: 05-29-2011, 06:38 PM
  2. KeyPressed trouble with KeyListeners
    By CuddlyKittens11 in forum Advanced Java
    Replies: 8
    Last Post: 05-28-2011, 09:31 PM
  3. KeyListeners not working
    By superzipzop in forum AWT / Swing
    Replies: 3
    Last Post: 02-06-2011, 03:14 AM
  4. Problem with keylisteners
    By dunafrothint in forum AWT / Swing
    Replies: 2
    Last Post: 11-23-2009, 05:54 PM
  5. [SOLVED] KeyListeners in JavaApplets
    By Unome in forum Java Applets
    Replies: 18
    Last Post: 10-30-2008, 10:36 PM

Tags for this Thread

Posting Permissions

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