Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

java - Global Event Listeners with AWTEventListener & how to pull MouseEvent's from it

The following question is based on the following information. Scroll down to see the actual question - it refers to the console output specifically.

I have stripped out everything, and provided a simple program to reproduce the output below:

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;

import javax.swing.JFrame;

public class Main {
    static Toolkit tk = Toolkit.getDefaultToolkit();
    static long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK
           + AWTEvent.KEY_EVENT_MASK;

    public static void main(String[] args) {
        tk.addAWTEventListener(new AWTEventListener() {
            @Override
            public void eventDispatched(AWTEvent e) {
                System.out.println(e.getID() + ", " + e);
            }
        }, eventMask);

        JFrame test = new JFrame();
        test.setBounds(0, 0, 100, 100);
        test.setVisible(true);
    }
}

You will see that it gives the following output in the console:

500, java.awt.event.MouseEvent[MOUSE_CLICKED,(71,54),absolute(71,54),button=1,modifiers=Button1,clickCount=1] on frame0
501, java.awt.event.MouseEvent[MOUSE_PRESSED,(71,54),absolute(71,54),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on frame0
506, java.awt.event.MouseEvent[MOUSE_DRAGGED,(70,54),absolute(70,54),modifiers=Button1,extModifiers=Button1,clickCount=0] on frame0
502, java.awt.event.MouseEvent[MOUSE_RELEASED,(67,54),absolute(67,54),button=1,modifiers=Button1,clickCount=1] on frame0
503, java.awt.event.MouseEvent[MOUSE_MOVED,(67,55),absolute(67,55),clickCount=0] on frame0
503, java.awt.event.MouseEvent[MOUSE_MOVED,(65,91),absolute(65,91),clickCount=0] on frame0
505, java.awt.event.MouseEvent[MOUSE_EXITED,(65,92),absolute(65,92),button=0,clickCount=0] on frame0

My question is - how can I get access to in individual entities in this

[MOUSE_RELEASED,(67,54),absolute(67,54),button=1,modifiers=Button1,clickCount=1]

without parsing out the data?

I need global event listeners in my situation. I've never used them before so I'm sure it's something I'm overlooking. Related question (where this all spawned from), Java check mouse button state

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just check e instanceof MouseEvent and get all the parameters from the MouseEvent

public void eventDispatched(AWTEvent e) { 
  if (e instanceof MouseEvent) {
    MouseEvent  me=(MouseEvent)e;
  } 
}

long KEY_EVENTS = AWTEvent.KEY_EVENT_MASK;
long MOUSE_EVENTS = AWTEvent.MOUSE_EVENT_MASK;
long MOUSE_MOTION_EVENTS = AWTEvent.MOUSE_MOTION_EVENT_MASK;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...