A Funny Key Event Prank in Java

Let me play a prank on the Key pressed and Key released events. The prank is nothing if you understand. Consider three keys that are kept pressed and only one of them is released keeping other two keys pressed, then an event has to occur. I think, you got my point. You might be in thought of how to do this and might already have reached it. If you didn't go through the code.





import javax.swing.*;
import java.awt.event.*;
class KeyTrick extends JFrame
{
JButton b;
boolean ctrlPressed,altPressed,XPressed;
    public KeyTrick()
    {
    setTitle("A Prank on the KeyEvent");
    setSize(400,400);   
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    addKeyListener(new KeyAdapter(){
        public void keyPressed(KeyEvent ke)
        {
            if(ke.getKeyCode()==KeyEvent.VK_CONTROL)
            {
            ctrlPressed=true;
            System.out.println("Control pressed");
            }
            else if(ke.getKeyCode()==KeyEvent.VK_ALT)
            {
            altPressed=true;
            System.out.println("Alt pressed");
            }
            else if(ke.getKeyCode()==KeyEvent.VK_X)
            {
            XPressed=true;
            System.out.println("X pressed");
            }
        }
        public void keyReleased(KeyEvent ke)
        {
            if(ke.getKeyCode()==KeyEvent.VK_CONTROL)
            {
            ctrlPressed=false;
            System.out.println("Control released");
            }
            else if(ke.getKeyCode()==KeyEvent.VK_ALT)
            {
            altPressed=false;
            System.out.println("Alt released");
            }
            else if(ke.getKeyCode()==KeyEvent.VK_X)
            {
                if((ctrlPressed==altPressed==true))
                {
                System.out.println("X Released!");
                System.exit(0);
                }
            }
        }
    });
    }
    public static void main(String args[])   
    {
    new KeyTrick();
    }
}

Output


Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Control pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
Alt pressed
X pressed
X Released!

Program Analysis

Flags have been declared in order to work out the program. According to the program,

When control is pressed, ctrlPressed will be true. In a similar way for alt and x keys.
When control is released, ctrlPressed will be false. In a similar way for alt key.
When the key x is released, a condition is needs to be checked i.e. both the control and the alt keys should be pressed (should be in the pressed state and should not be released).
So when x is released keeping alt and control keys in the pressed state, the event occurs (here exiting the program). See the program output for a better understand.

I called this as a prank because the user might not know how the event occurs. He presses all the keys at once/or one by one and then releases all at once. He has to know actually that the other keys should be pressed releasing the third key. Is my title justified? Drop a comment.

No comments: