Forums Register Login

How to crate KeyListener in different Thread?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
I was her plaything! And so was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6898 times.
Similar Threads
keyListener problems
KeyListerner in JPanel
Key Event listener question.
Key listener or what?
Button keyboard event
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:56:46.