Solved

Applet : press any key to continue

Posted on 2003-10-22
6
348 Views
Last Modified: 2010-03-31
Hi experts,
I have written an applet that processes some datas, however, at one point, I need the user to press any key (or the enter key) to continue.
This is keyevent is not linked to any object like a button. I tried something like :
this.addKeyListener(new KeyAdapter() {
  public void KeyPressed(KeyEvent e) {
  }
  });
but this produce an extra file$1.class that bothers me (it needs to become a signed jar).
I tried playing with something like : if ( ev.id == Event.KEY_PRESS ) with no success.
I'm sure there's a simple way to write in an applet something similar to : while ((ch = (char)System.in.read()) !=...).
Thanks experts for your answer !

0
Comment
Question by:everfly
6 Comments
 
LVL 3

Accepted Solution

by:
savalou earned 20 total points
ID: 9599148
Why can't you include the file$1.class in your signed jar?

The file$1.class is generated because you're using an anonymous subclass of KeyAdaptor (new KeyAdaptor() {//this is an anonymous class...}).  To get around this you can write your own class like MyKeyAdaptor.java in which you extend KeyAdaptor.
0
 

Author Comment

by:everfly
ID: 9599535
Thanks savalou. Right, I included the file$1.class in my signed jar but adding "this.addKeyListener(new KeyAdapter() {public void KeyPressed(KeyEvent e) {}});" doesn't wait for me to press any key.
And also, isn't there a easier way to perfom this ?
Thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 10194156
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept savalou's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

objects
EE Cleanup Volunteer
0
 

Expert Comment

by:Bobby_Hirst
ID: 10220786
How about this:

this.addKeyListener(new KeyAdapter() {
    public void KeyPressed(KeyEvent e) {
        continue=true;
    }
});"

//add this bit where you want execution to stop:
boolean continue=false;
while (!continue) {}  //this will loop, until the KeyEvent allows the
                              //execution to continue.

I think that should solve it... The thing that you hadn't taken into account is that KeyListener runs in parallel with your code listening for Events, and if no event is assigned to it, nothing will happen to your main code's execution.  As for the extra class file, I think it's unavoidable but should cause any problems when you create a signed jar
0
 

Expert Comment

by:Bobby_Hirst
ID: 10220797
Sorry, read 'should not' in the last sentence
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question