如何在java中基于聊天的屏幕锁定应用程序中禁用密钥

时间:2012-12-07 12:52:11

标签: java windows

我已编写此代码以使计算机实验室应用程序监控学生。这里屏幕锁定正在工作,但禁用键无效我在这里尝试使用机器人

如何运行循环以连续收听消息,并检查Screenlocker的状态以使用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);
    }   
}

0 个答案:

没有答案