Results 1 to 17 of 17
Thread: Applet key event not recognized
- 10-02-2012, 01:53 AM #1
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Applet key event not recognized
I have written an applet with mouse and keyboard event handlers. Some text is wriiten to the screen via the Paint() method and key strokes are supposed to be written to the screen (via a message in Paint(). Keystrokes are also written to the status bar via showStatus().
The problem is that keystrokes are not recognized UNTIL the mouse is clicked on the applet. After that first click, the key event works as expected. It's like there is no focus on the applet to start with?
gord c
- 10-02-2012, 02:48 AM #2
Re: Applet key event not recognized
Make sure the component with the key listener can get the focus and has the focus. The Component class has some methods that will do that. Look at the API doc for Component and Find "focus" to find the methods
If you don't understand my response, don't ignore it, ask a question.
- 10-02-2012, 04:56 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: Applet key event not recognized
Norm, There are no (user) components in this simple applet so I can't program a requestFocus(). I tried this.requestFocus() in the Init method but this is no good. Only a physical mouse click on the applet activates the KeyListener. Perhaps it is possible to program a mouse click: that might work.
I see that several people have had difficulty with key events (not responding). Another solution might be to use a container such as a Swing Panel and then requestFocus on that. Any comments?
gord c
- 10-02-2012, 05:03 PM #4
Re: Applet key event not recognized
The applet class extends Component.There are no (user) components in this simple applet
What other methods from the Component class did you try? There are several that are related to focus.If you don't understand my response, don't ignore it, ask a question.
- 10-02-2012, 08:29 PM #5
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
- 10-02-2012, 08:43 PM #6
Re: Applet key event not recognized
The applet class by extending the Component class is a Component. See the API doc for the applet class.What Component
You can call any of the Component class's methods from within a method in the applet class.to what do I apply these toIf you don't understand my response, don't ignore it, ask a question.
- 10-02-2012, 10:16 PM #7
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: Applet key event not recognized
I've tried looking for the right construction in the API but cannot find a component of the Applet class to work with.
Here is the bare-bones program that has the bug discussed in my posts.
If anyone can make this work properly, I would be very pleased.Java Code:/* * KeyTest.java * * KeyTest Applet application *<applet code="Main" width=300 height=100> *</applet> * Created on 02-10-2012 03:40 PM */ import java.awt.*; import java.applet.*; import java.awt.event.*; /* Chars typed should appear in the Status bar. They do ONLY after the mouse is clicked on the Viewer Window */ public class Main extends Applet implements KeyListener { char ch; String msg=""; public void init() { addKeyListener(this); } public void keyPressed(KeyEvent k) { } public void keyReleased(KeyEvent k) { } public void keyTyped(KeyEvent k){ ch=k.getKeyChar(); msg+=ch; showStatus(msg); } public void paint(Graphics g) { g.drawString("Welcome to Java!!", 50, 60 ); } }
gord cLast edited by Norm; 10-04-2012 at 10:45 PM. Reason: added code tags
- 10-02-2012, 10:29 PM #8
Re: Applet key event not recognized
What focus related methods from the Component class did you try?
Please edit your post and add code tags to the code. See: BB Code List - Java Programming ForumIf you don't understand my response, don't ignore it, ask a question.
- 10-03-2012, 01:37 AM #9
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: Applet key event not recognized
Please don't beat about the bush. You suggest what object.method. It's the object that has me baffled!
gord c
- 10-03-2012, 02:23 AM #10
Re: Applet key event not recognized
Use the this reference in the applet. What reference are you using with the following statement that calls the addKeyListener() method?It's the object that has me baffled!What class is that method defined in?Java Code:addKeyListener(this);
If you don't understand my response, don't ignore it, ask a question.
- 10-03-2012, 02:38 PM #11
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: Applet key event not recognized
I have added the following code. It compiles but does not solve the keystroke problem.
public class Main extends Applet implements KeyListener , FocusListener{
char ch;
String msg="";
public void init() {
addKeyListener(this);
addFocusListener(this);
this.requestFocus(); }
public void focusLost(FocusEvent f){
}
public void focusGained(FocusEvent f){
} ....... rest of code
I need a specific answer to this, no more vague suggestions please.
- 10-03-2012, 02:46 PM #12
Re: Applet key event not recognized
gord c, Norm has given you a link to the FAQ on how to use the code tags. If you persist in posting unformatted code, this thread will be locked and you may be banned for a period.
Kindly edit your posts and add the code tags.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-03-2012, 02:48 PM #13
Re: Applet key event not recognized
You don't seem to understand how a forum works. We can help you to learn enough to solve your own problems, but we won't do your coding for you.
Recommended reading: How to ask questions the smart way
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-03-2012, 03:11 PM #14
Re: Applet key event not recognized
List the methods in the Component class that deal with setting and requesting focus.I need a specific answer to this,
The posted code shows just one. There are more.If you don't understand my response, don't ignore it, ask a question.
-
- 10-04-2012, 10:39 PM #16
Member
- Join Date
- Sep 2012
- Posts
- 7
- Rep Power
- 0
Re: Applet key event not recognized
I apologize for not using the CODE tags, I don't know how to edit my previous listings. The original problem appears to be non-trivial. The failure of the applet window to gain focus (to accept keystrokes) automatically seems to be a topic that is seen in Google and other searches but no solution is available. I'm using IE8 with XP.
Thanks
gord c
- 10-04-2012, 10:44 PM #17
Similar Threads
-
javac not recognized
By kris972 in forum New To JavaReplies: 7Last Post: 09-21-2012, 07:11 AM -
applet event listener
By jobud9 in forum New To JavaReplies: 4Last Post: 01-29-2012, 06:15 AM -
.Jar Not Recognized
By Java Tyler in forum New To JavaReplies: 11Last Post: 08-12-2010, 03:36 PM -
Javac not recognized
By OscarLiu in forum New To JavaReplies: 8Last Post: 06-29-2010, 03:10 PM -
Javac is not recognized?
By Java idiot in forum New To JavaReplies: 4Last Post: 08-03-2009, 06:54 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks