Results 1 to 3 of 3
  1. #1
    thricehex is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default keyListener sending keystrokes over network

    Hi, I'm trying to write a program that records keystrokes and sends them over the network to another program to be displayed.
    The problem is that it only works for a single character and afterwards I either get a broken pipe error in the console or nothing at all. How would I get the logging end to keep sending keystrokes and the receiving end to keep getting and displaying them. Loops don't help, at least the ones iv tried but my experience in Java is still at low level and I cant seem to figure this out.

    Key logging code:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.net.*;
    
    public class KeyLog extends JFrame implements KeyListener {
    	
    	//private File keys;
    	//private FileWriter writer;
    	//private BufferedWriter buffer;
    	private int key;
    	//private int kn = 0;
    	//private int[] keyarray;
    	private Socket socket;
    	//private ObjectInputStream input;
    	private ObjectOutputStream output;
    
    public static void main(String[] args) { 
    	new KeyLog(); 
    	
    }
    
    
    
    public KeyLog() { 
    
    	this.addKeyListener(this);
    
    
    this.setSize(0,0); this.setVisible(true); 
    
    
    try {
    socket = new Socket("192.168.0.13", 25564);
    JOptionPane.showMessageDialog(null, "Connected to Server");
    output = new ObjectOutputStream(socket.getOutputStream());
    output.flush();
    
    
    }catch(UnknownHostException e) {
    	e.printStackTrace();
    }catch(IOException e) {
    	e.printStackTrace();
    }
    
    
    
    }
    
    public void keyPressed(KeyEvent e) { 
    	
    	System.out.println(e.getKeyCode()); 
    	key = e.getKeyCode();
    	
    	KeySend((char)key);
    }
    
    public void keyReleased(KeyEvent e) { 
    	
    }
    
    public void keyTyped(KeyEvent e) { 
    	
    	}
    	public void KeySend(char capd_key) {
    		
    
    		try {
    		output.writeObject(capd_key); 
    		output.flush();
    		JOptionPane.showMessageDialog(null, "Char sent");
    		
    	
    		}catch(IOException e) {
    			e.printStackTrace();
    			}
    		}
    	}
    Key display code:

    Java Code:
    
    import java.net.*;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class KeyRec {
    	
    	private static ObjectInputStream input;
    	//private static ObjectOutputStream output;
    	private static ServerSocket server;
    	private static Socket socket;
    	private static Object charset;
    	//private static int chn = 0;
    	private static String charstring;
    	
    	public static void main(String[] args) {
    		
    		try {
    		server = new ServerSocket(25564, 2);
    		socket = server.accept();
    		JOptionPane.showMessageDialog(null, "Connection to client established.");
    		input = new ObjectInputStream(socket.getInputStream());
    		
    		charset = input.readObject();
    		charstring = charset.toString();
    		JOptionPane.showMessageDialog(null, charstring);
    		}catch(IOException e) {
    			e.printStackTrace();
    		}catch(ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    		}
    	}
    Last edited by thricehex; 08-08-2012 at 05:48 PM. Reason: had to edit code

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: keyListener sending keystrokes over network

    You may have a Swing threading issue as you appear to be doing all on the same thread. Consider doing the socket business in a background thread.

    Also, to help others better understand your code, please read up on and abide by Java naming conventions: class names start with capital letter, variable and method names with a lower-case letter. Also please improve on your code indentation as it is all over the place making it very hard to follow your logic. Consider indenting 3 spaces and having your indentations be uniform and consistent.

  3. #3
    thricehex is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: keyListener sending keystrokes over network

    Quote Originally Posted by Fubarable View Post
    You may have a Swing threading issue as you appear to be doing all on the same thread. Consider doing the socket business in a background thread.

    Also, to help others better understand your code, please read up on and abide by Java naming conventions: class names start with capital letter, variable and method names with a lower-case letter. Also please improve on your code indentation as it is all over the place making it very hard to follow your logic. Consider indenting 3 spaces and having your indentations be uniform and consistent.
    Ok, thanks.

Similar Threads

  1. Sending files over network
    By TerraViper-5 in forum Networking
    Replies: 0
    Last Post: 04-15-2011, 08:28 PM
  2. sending object through network/socket
    By skandalouz in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:34 AM
  3. Sending a key over network
    By cst in forum Networking
    Replies: 0
    Last Post: 11-04-2009, 01:19 PM
  4. sending file over network
    By qwerty in forum Networking
    Replies: 6
    Last Post: 04-25-2009, 01:55 AM
  5. Sending .GIF files over the network
    By nadia in forum Networking
    Replies: 8
    Last Post: 12-30-2008, 11:57 AM

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
  •