Results 1 to 7 of 7
  1. #1
    Fecoooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default 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);  
        }  
          
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Thread supposed to block keyevents

    While the variable is true,
    Where is the variable's value changed?
    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.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Fecoooo is offline Member
    Join Date
    Mar 2012
    Posts
    5
    Rep Power
    0

    Default 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
    Java Code:
    if (System.currentTimeMillis() - lastKeyPress < 2000)  
      return;  
    lastKeyPress = System.currentTimeMillis();
    Thank you anyways

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,237
    Rep Power
    19

    Default 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?
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. How to stop block thread while inputstream reading?
    By briliasm in forum Networking
    Replies: 0
    Last Post: 02-29-2012, 08:24 AM
  2. JFrames and KeyEvents
    By CuddlyKittens11 in forum AWT / Swing
    Replies: 1
    Last Post: 05-05-2011, 12:58 AM
  3. Question on Graphics/JFrame/KeyEvents
    By loopsnhoops in forum New To Java
    Replies: 4
    Last Post: 02-10-2011, 11:22 PM
  4. Replies: 3
    Last Post: 02-02-2011, 10:53 AM
  5. Question about java keyevents?
    By Godsent in forum AWT / Swing
    Replies: 2
    Last Post: 10-26-2009, 09:38 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •