Results 1 to 18 of 18
Thread: Help me with KeyListeners pls
- 09-19-2015, 10:41 PM #1
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
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 11:40 PM.
- 09-19-2015, 10:58 PM #2
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
- 09-19-2015, 11:09 PM #3
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
the keyListeners dont trigger when i press a key
- 09-19-2015, 11:17 PM #4
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.
- 09-19-2015, 11:21 PM #5
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
there i did it. is that what u meant?
- 09-19-2015, 11:25 PM #6
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
sorry a little sloppy
- 09-19-2015, 11:33 PM #7
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.
- 09-19-2015, 11:40 PM #8
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
there we go
- 09-20-2015, 12:02 AM #9
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.
- 09-20-2015, 12:06 AM #10
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
what do you mean?
- 09-20-2015, 12:17 AM #11
Re: Help me with KeyListeners pls
If you don't understand my response, don't ignore it, ask a question.
- 09-20-2015, 12:25 AM #12
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
First off Thats too advanced for my level of coding.
Secondly i'm using an Applet
- 09-20-2015, 12:27 AM #13
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
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
- 09-20-2015, 12:36 AM #14
Re: Help me with KeyListeners pls
Thats too advanced for my level of coding.If you don't understand my response, don't ignore it, ask a question.
- 09-20-2015, 12:58 AM #15
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
you can now mark this as SOLVED I DID IT!!!
- 09-20-2015, 01:03 AM #16
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.
- 09-20-2015, 01:47 AM #17
Member
- Join Date
- Sep 2015
- Posts
- 21
- Rep Power
- 0
Re: Help me with KeyListeners pls
I Simply Put this line of code in the init method:
PHP Code:setFocusable(true);
PHP Code://Whatever your JFrame caller is--> frame.setFocusable(true)
- 09-20-2015, 02:23 AM #18
Similar Threads
-
KeyListeners doesn't work in NB, while MouseListener does
By Warmonger in forum AWT / SwingReplies: 9Last Post: 05-29-2011, 07:38 PM -
KeyPressed trouble with KeyListeners
By CuddlyKittens11 in forum Advanced JavaReplies: 8Last Post: 05-28-2011, 10:31 PM -
KeyListeners not working
By superzipzop in forum AWT / SwingReplies: 3Last Post: 02-06-2011, 04:14 AM -
Problem with keylisteners
By dunafrothint in forum AWT / SwingReplies: 2Last Post: 11-23-2009, 06:54 PM -
[SOLVED] KeyListeners in JavaApplets
By Unome in forum Java AppletsReplies: 18Last Post: 10-30-2008, 11:36 PM
Bookmarks