Listen for mouse clicks on the whole system (not on JFrame, etc.)

I want to write a little code that responds to a mouse click. But it seems the only way is to listen for clicks on Java components. The direct listener for all clicks will be great.

Is it possible to implement this in Java?

Thanks in advance!

Update:

It turned out that JNI would need some hook with some C encoding.

Additional information on http://www.jotschi.de/?p=90

Regards, fnst

+4
source share
4 answers

I donโ€™t think because of the sandbox in which you are running.

+1
source

It is quite possible if you want to use a third-party library - JNativeHook

It provides these functions using JNI, which is not possible in pure java applications.

+1
source

some food for thought: use Point in Java to determine where the click occurs. http://download.oracle.com/javase/1.4.2/docs/api/java/awt/Point.html

In truth, ive only used it once to detect rows in a table, something like this, but it seems to be closest to ive finding the answer to your qn .:

public void mouseClicked(MouseEvent e) { JTable target = (JTable)e.getSource(); //get the coordinates of the mouse click Point p = e.getPoint(); //get the row index that contains that coordinate row= target.rowAtPoint(p); } 

I'm sorry if this is not what you are looking for, but otherwise clicking on the components is the only way. what's the alternative click on containers? well, that just doesn't make any sense?

0
source

As far as I know, there is no easy way to accomplish what you want. But what you want can be done. Well, for a modern way to accomplish this task, I suggest you study the java.awt.Dialog show method.

 package mouseclickevent; import java.awt.AWTEvent; import java.awt.Component; import java.awt.EventQueue; import java.awt.MenuComponent; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.lang.reflect.Method; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; public class FrmEvent extends JFrame { public FrmEvent(){ JPanel panel = new JPanel(); getContentPane().add(panel); JButton btn = new JButton("Test"); panel.add(btn); panel.add(new JTextField("Test")); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { FrmEvent frm = new FrmEvent(); frm.setBounds(300,300, 200, 200); frm.show(); } }); } private void consumeEvents() { try { if (Thread.currentThread().getClass().getName().endsWith( "EventDispatchThread")) { EventQueue eq = null; eq = Toolkit.getDefaultToolkit().getSystemEventQueue(); if (eq == null) { return; } while (isVisible() || isShowing()) { AWTEvent event = eq.getNextEvent(); Object src = event.getSource(); Class kActiveEvent= Class.forName("java.awt.ActiveEvent"); if (kActiveEvent != null) { if (kActiveEvent.isInstance(event)) { Method m; Class types[] = {}; Object args[] = {}; m = kActiveEvent.getMethod("dispatch", types); if (m != null) { m.invoke(event, args); continue; } } } dispatchEvent(src, event); } } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } } private void dispatchEvent(Object src, AWTEvent event) { if (event.getID()== MouseEvent.MOUSE_CLICKED) { System.out.println("mouseClicked"); } if (src instanceof Component) { ( (Component) src).dispatchEvent(event); } else if (src instanceof MenuComponent) { ( (MenuComponent) src).dispatchEvent(event); } } public void show(){ super.show(); consumeEvents(); } public static void main(String[] args) { FrmEvent frm = new FrmEvent(); frm.setBounds(300,300, 200, 200); frm.show(); } } 
0
source

Source: https://habr.com/ru/post/1334636/


All Articles