How do I implement a mouse event outside the Java frame?

590 Views Asked by At

I am trying to make a return program which will press the enter key when the left mouse key is clicked...

with courtesy of http://www.java-tips.org/java-se-tips/java.awt/how-to-use-robot-class-in-java.html (for void typing method) and "thenewboston" I have gotten so far...

I am trying to make it so that it will function in other platforms, for example: Word, Note Pad and not just on a JFrame

This is what I have up till now...

import java.awt.event.MouseEvent;
import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;

import java.awt.Robot;
import java.awt.event.KeyEvent;


public class MW3Tool

{

public static void main (String[] args)

{

Robot enter = new Robot();
int num;
return count = 0;

num = count * 3;


Control c = new Control();

for (int k = 1; k <= num; k++)


{System.out.println("H");}


/*  try {
Robot robot = new Robot();                    // Going to be used to electronically hit the enter key later.

  robot.delay(5000);
  robot.setSpeed(10);
  for (int k = 1; k<= num; k ++)
    robot.keyPress(KeyEvent.VK_ENTER);

 }           
  catch (AWTException e) {
   e.printStackTrace();

         }   }    */

}

private class Control implements MouseListener


{
int count;
int useless;
int useless2;
   public void mouseClicked(MouseEvent event) 
{
   count++;
}

public void mousePressed(MouseEvent event) 
{
   useless++;
}


public void mouseExited(MouseEvent event) 
{
   useless2++;
}

}

}

My errors:

 ----jGRASP exec: javac -g MW3Tool.java

MW3Tool.java:20: cannot return a value from method whose result type is void
return count = 0;
             ^
MW3Tool.java:22: cannot find symbol
symbol  : variable count
location: class MW3Tool
num = count * 3;
      ^
MW3Tool.java:35: non-static variable this cannot be referenced from a static context
Control c = new Control();
            ^
MW3Tool.java:60: MW3Tool.Control is not abstract and does not override abstract method mouseEntered(java.awt.event.MouseEvent) in java.awt.event.MouseListener
private class Control implements MouseListener
        ^
4 errors

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

Sorry for my inefficient methods (newly affiliated with Java)

Any help will be appreciated... thank you...

1

There are 1 best solutions below

0
On BEST ANSWER

First error: Are you trying to initialize an Integer? Wrong syntax. Use

int count = 0;

Second error: Solving the first error will solve this error.
Third error: Instead of saying

private class Control implements MouseListener { ... }

Say

private static class Control implements MouseListener { ... }

Last error: See the MouseListener Javadocs: Method Summary
void mouseClicked(MouseEvent e) Invoked when the mouse button has been clicked (pressed and released) on a component.
void mouseEntered(MouseEvent e) Invoked when the mouse enters a component.
void mouseExited(MouseEvent e) Invoked when the mouse exits a component.
void mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component.
void mouseReleased(MouseEvent e) Invoked when a mouse button has been released on a component.
You MUST override all of these methods in the Control class for your program to work.
Hope this helps!