Results 1 to 7 of 7
- 03-11-2012, 08:37 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Thread supposed to block keyevents
Hi There!
I wrote 2 class, the first is a canvas wich can listen to keyevents, the second is a thread wich sets a boolean variable to true, than 2 secs later to false. While the variable is true, one shouldnt supposed to change anything with a keypress, but one can. How can this be possible? Thanks.
The listeners code.
Java Code:@Override public void keyPressed(KeyEvent e) { if(!keyPressed){ KeyboardBlocker kBB = new KeyboardBlocker(keyPressed); kBB.start(); switch(e.getKeyCode()){ case KeyEvent.VK_UP: rotUpperClockwise(upperRow, CLOCKWISE, 'Z'); break; case KeyEvent.VK_DOWN: rotUpperClockwise(upperRow, COUNTERCLOCKWISE, 'Z'); break; case KeyEvent.VK_LEFT: break; case KeyEvent.VK_RIGHT: break; } } }
the threads code:
Java Code:package szakdoga_real; import java.util.logging.Level; import java.util.logging.Logger; public class KeyboardBlocker extends Thread{ private Boolean isKeyPressed; public KeyboardBlocker(Boolean isKeyPressed) { this.isKeyPressed = isKeyPressed; } @Override public void run() { isKeyPressed = true; System.out.println(isKeyPressed); try { Thread.sleep(2000); } catch (InterruptedException ex) { Logger.getLogger(KeyboardBlocker.class.getName()).log(Level.SEVERE, null, ex); } isKeyPressed = false; System.out.println(isKeyPressed); } }
- 03-11-2012, 01:20 PM #2
Re: Thread supposed to block keyevents
Where is the variable's value changed?While the variable is true,
Can you explain what happens when you execute the code? Add some printlns that show where the code is executing and what the values of the variables are as it executes.
- 03-11-2012, 01:55 PM #3
Re: Thread supposed to block keyevents
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem. The code snippets in isolation mean nothing.
For example, how is keyPressed in one class related to isKeyPressed in the other?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 02:33 PM #4
Member
- Join Date
- Mar 2012
- Posts
- 5
- Rep Power
- 0
Re: Thread supposed to block keyevents
It is supposed to start a new thread, wich changes a boolean value to true (i gave the varieble trough the constructor), wait 2 sec than change it back to false (its in run method). It changes the value (i printed the values out) but still i can press button and reach the switch statement. I resolved the problem tohugt
Thank you anywaysJava Code:if (System.currentTimeMillis() - lastKeyPress < 2000) return; lastKeyPress = System.currentTimeMillis();
- 03-11-2012, 02:37 PM #5
Re: Thread supposed to block keyevents
Can you make a SSCCE that shows the problem?
Without a fully functional program that shows all of what is happening, how can this problem be solved? There are too many ways to do it wrong.
- 03-11-2012, 03:49 PM #6
Re: Thread supposed to block keyevents
No SSCCE, no help. Not because we're stubborn old cusses (although that may be also true) but because we can't.
The ball's in your court.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-11-2012, 03:51 PM #7
Re: Thread supposed to block keyevents
On the other hand, since this has been cross posted, I'm no longer interested -- SSCCE or no.
Thread supposed to block keyevents (Threads forum at JavaRanch)
db
edit And Thread supposed to block keyevents - Java
Any more?Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
How to stop block thread while inputstream reading?
By briliasm in forum NetworkingReplies: 0Last Post: 02-29-2012, 08:24 AM -
JFrames and KeyEvents
By CuddlyKittens11 in forum AWT / SwingReplies: 1Last Post: 05-05-2011, 12:58 AM -
Question on Graphics/JFrame/KeyEvents
By loopsnhoops in forum New To JavaReplies: 4Last Post: 02-10-2011, 11:22 PM -
Getting keyEvents even though the program is not in focus
By Skurken in forum Advanced JavaReplies: 3Last Post: 02-02-2011, 10:53 AM -
Question about java keyevents?
By Godsent in forum AWT / SwingReplies: 2Last Post: 10-26-2009, 09:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks