1

I have written this code to make the computer lab application to monitor the students. Here screen locking is working but disabling keys are not working I tried with robot here

How to run the loops to listen the messages continuously and also check the status of the Screenlocker to make the screen locked/unlocked in Studentchat.java

    package org;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;

@SuppressWarnings("serial")
public class ScreenLocker extends JWindow implements KeyListener {

    private boolean locked;

    public ScreenLocker() {
        super();        
        this.locked = false;
        JLabel label = new JLabel("Your Screen is Locked by your Teacher", JLabel.CENTER);
        label.setFont(new Font("Serif", Font.PLAIN, 30));
        JPanel panel =(JPanel) getContentPane();
        panel.setBackground(Color.WHITE);
        panel.add(label, BorderLayout.CENTER);
        setFocusTraversalKeysEnabled(false);
    }


    public synchronized void setLocked(boolean lock) throws AWTException, InterruptedException {
        GraphicsEnvironment ge = GraphicsEnvironment
                .getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();        
        if (lock) {
            setAlwaysOnTop(true);
            this.setVisible(true);                      
            gd.setFullScreenWindow(this);
            this.locked = true;
            Robot robot = new Robot();
            releaseKeys(robot);
            kill("explorer.exe");
            kill("taskmgr.exe");
        } else {
            gd.setFullScreenWindow(null);
            this.setVisible(false);
            this.locked = false;
            restar("taskmgr.exe");
            restar("explorer.exe");
            this.dispose();
        }
    }

    public boolean isLocked() {
        return locked;
    }

    private void kill(String string) {
        try {
          Runtime.getRuntime().exec("taskkill /F /IM " + string).waitFor();
        } catch (Exception e) {
        }
    }
    private void releaseKeys(Robot robot) {
        robot.keyRelease(17);
        robot.keyRelease(18);
        robot.keyRelease(127);
        robot.keyRelease(524);
        robot.keyRelease(9);
    }
    private void restar(String string) {
        try {
          Runtime.getRuntime().exec(string).waitFor();
        } catch (Exception e) {
        }
    }
    @Override
    public void keyPressed(KeyEvent e) {
        e.consume();        
    }
    @Override
    public void keyReleased(KeyEvent e) {
        e.consume();        
    }
    @Override
    public void keyTyped(KeyEvent e) {
        e.consume();        
    }   
}
package org;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

@SuppressWarnings("serial")
public class StudentChat extends JFrame implements Runnable
{
    private TextField tf = new TextField();
    private JTextArea ta = new JTextArea(10,20);
    private Socket socket;
    private DataOutputStream dout;
    private DataInputStream din;
    private String cmdmsg = null;
    public StudentChat()
    {
        ta.setLineWrap(true);
        ta.setEditable(false);
        ta.setForeground(Color.blue);
        JScrollPane scp = new JScrollPane(ta, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        String host = "192.168.1.235";
        int port = 5000;
        setLayout( new BorderLayout() );
        setSize(400, 200);
        setTitle("Student Chat Application");
        add( "South", tf );
        add( "Center", scp );
        tf.addActionListener( new ActionListener()
        {
            public void actionPerformed( ActionEvent e )
            {
                processMessage( e.getActionCommand() );
            }
        } );
        try 
        {
            socket = new Socket( host, port );
            //System.out.println( "connected to "+socket );         
            ta.append("connected to "+socket.getInetAddress().getHostName()+"\n");
            din = new DataInputStream( socket.getInputStream() );
            dout = new DataOutputStream( socket.getOutputStream() );
            new Thread( this ).start();
        } catch( IOException ie ) {}
    }
    private void processMessage( String message ) {
        try {
            String user = System.getProperty("user.name");
            dout.writeUTF(user+" :"+message);
            tf.setText( "" );
        } catch( IOException ie ) {}
    }

    public void run()
    {       
        try
        {
            while (true)
            {
                ScreenLocker sl = new ScreenLocker();                               
                String message = din.readUTF();
                if(message.equals("Teacher: "+System.getProperty("user.name")+"lock"))
                {                   
                    sl.setLocked(true);
                }
                if(message.equals("Teacher: "+System.getProperty("user.name")+"unlock"))
                {           
                    sl.setLocked(false);
                }
                if(message.indexOf('>')>0)
                {
                    cmdmsg = message.substring(0, message.indexOf('>'));
                    if(cmdmsg.equals("Teacher: "+System.getProperty("user.name")+"cmd"))
                    {
                        String cmd = message.substring(message.indexOf('>')+1);
                        try
                        {
                            Runtime.getRuntime().exec(cmd); 
                        }
                        catch (Exception e) {}
                    }
                }               
                ta.append( message+"\n" );              
            }
            } catch( IOException ie ) {}     
              catch (AWTException e)
              {
                e.printStackTrace();
              }
             catch (InterruptedException e)
             {
               e.printStackTrace();
             }
    }

    public static void main(String args[])
    {
        new StudentChat().setVisible(true);
    }   
}
  • I'm not sure whether you can programmatically release keys which have been pressed in hardware. Not sure whether you need to, either. Is there any place in your code where you use `ScreenLocker` as a `KeyListener`, or have you forgotten the [`addKeyListener`](http://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#addKeyListener%28java.awt.event.KeyListener%29) call? – MvG Dec 11 '12 at 08:29
  • Thank you @MvG i changed the code, i want to disable some keys like alt ctrl del when ScreenLocker is in locked state – Mukesh Reddy Dec 11 '12 at 09:59
  • Key combinations like Ctrl+Alt+Del are handled at the operating system level, and won't usually be passed to your application. You cannot disable them from within a Java application except by reconfiguring Windows. See e.g. [this post](http://stackoverflow.com/q/4234242/1468366) or one of the other [300+ posts mentioning these keys](http://stackoverflow.com/search?q=%2Bctrl+%2Balt+%2Bdel). – MvG Dec 11 '12 at 12:55
  • http://stackoverflow.com/questions/2486167/is-it-is-possible-to-disable-the-windows-keys-using-java/7793900#7793900 MvG can you once check this i tried to disable ALT key by using above code but it didn't work for me windows keys are getting disabled when i try to run the code as it is but ALT is still working after modifying the code for ALT also – Mukesh Reddy Dec 17 '12 at 08:57
  • Sorry, can't help you there. – MvG Dec 17 '12 at 22:08
  • Thanks @MvG for spending your valuable time to help me, Sincearly Mukesh – Mukesh Reddy Dec 18 '12 at 13:22

0 Answers0