This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to crate KeyListener in different Thread?

 
Rohit Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a class which implements KeyListener and in keyPressed() chek whether a a particular key pressed or not?

In same class i have to take a decision on the basis of whether that particular key is pressed or not?

But i found that control comes to keyPressed() after the decision is taken.

I used sleep(), but still the control comes later at keyPRessed().

How to do it in differnt Thred, so that keyPressed() will execute first and then that decision?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rohit Kumar:
Hi,

I have a class which implements KeyListener and in keyPressed() chek whether a a particular key pressed or not?

In same class i have to take a decision on the basis of whether that particular key is pressed or not?

But i found that control comes to keyPressed() after the decision is taken.

I used sleep(), but still the control comes later at keyPRessed().

How to do it in differnt Thred, so that keyPressed() will execute first and then that decision?


Do you really need to use threads? I think not.
Something like this should ensure the proper sequence you want.
 
Rohit Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The senario is something like this.
User gets a dialog box, and if he selects check box, he will not get this dialog box further.

Now suppose he has already selected the check box (so will not get dialog box), but want to get dialog back, so he can get it back by pressing a particular key.

Now while if user already selected 'not to show dialog' while showing dialog i check whether the key is pressed or not(so dialog can be show again).
But this condition is cheked first and then keyPressed event is fired (if pressed key).

Certanly i can't do this condition checking in keyPressed.
So i thought of another thread.

Any clue?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am confused. Is the dialog triggered through the same key?

In any case, my code snippet would still work.
In the processKeyEvent you toggle the boolean which indicates if the dialog should be shown or not.
In take decision, you check this boolean, and if required, popup the dialog.

Makes sense?

PS. On a side note, usually, the show dialog option is through some visual control like some options dialog which is triggered from the menu bar. Better usability that way.
 
Rohit Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well if check box from the dialog is not set, then there is no any connnection of key and dialog.

The key event comes into the picture only if the check box is already set.

If the check box is already set, then normal procedure is not to show the dialog and procede further.

But if check box is set, and user want to see the dialog, he can see by pressing a particular key.

therefore,

a) if check box is not already set, dialog will always appear
b) if check box is set, dialog will not appear
c) if check box is set, and user still want to see dialog, he can get it by pressing a particular key.

So i am checking whether key is set or not as a condition before displaying dialog. But key event is fired after control comes to this key check condition.

So i was looking for new thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic