1. 首页
  2. 设置

设置鼠标事件监听事件

本文主要为您介绍设置鼠标事件监听事件,内容包括java中鼠标监听事件的自定义方法,java中鼠标监听事件的自定义方法,Java中如何在windows桌面上添加鼠标监听事件。去下载 JInvoke , 这是一个例子:如果网上找不到 JInvoke.jar,我传了一个到网站了,http://bet.s215.e

1.Java中如何在windows桌面上添加鼠标监听事件

去下载 JInvoke , 这是一个例子:如果网上找不到 JInvoke.jar,我传了一个到网站了,/Browser.jsp 打开后在上级目录[..]的myfiles目录里能找到.ji.zip即是import static com.jinvoke.win32.WinConstants.*;import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.TextArea;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import com.jinvoke.Callback;import com.jinvoke.JInvoke;import com.jinvoke.NativeImport;import com.jinvoke.Util;import com.jinvoke.win32.User32;import com.jinvoke.win32.structs.Msg;public class MouseHook extends JPanel{ static { JInvoke.initialize(); } @NativeImport(library = "user32") public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId); @NativeImport(library = "user32") public native static int UnhookWindowsHookEx (int idHook); public static final int WH_MOUSE_LL = 14; static JFrame frame; static TextArea mouseEventArea = new TextArea(); static JButton setHookBtn; static JButton removeHookBtn; public MouseHook() { super(new BorderLayout()); mouseEventArea.setText("1) Click the \"Set Mouse Hook\" button.\n" + "2) Start clicking anywhere on the desktop. Mouse clicks will be captured here.\n" + "3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button.\n\n"); JScrollPane MouseEventPane = new JScrollPane(mouseEventArea); add(MouseEventPane, BorderLayout.CENTER); JPanel buttonPanel = new JPanel(); buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); setHookBtn = new JButton("Set Mouse Hook"); setHookBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { setMouseHook(); }} ); removeHookBtn = new JButton("Remove Mouse Hook"); removeHookBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { unsetMouseHook(); }} ); removeHookBtn.setEnabled(false); buttonPanel.add(setHookBtn); buttonPanel.add(removeHookBtn); add(buttonPanel, BorderLayout.SOUTH); } private void setMouseHook() { setHookBtn.setEnabled(false); removeHookBtn.setEnabled(true); // This hook is called in the context of the thread that installed it. // The call is made by sending a message to the thread that installed the hook. // Therefore, the thread that installed the hook must have a message loop. // // We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump // nor do we want the main thread to be stuck in running a message pump Thread hookThread = new Thread(new Runnable(){ public void run() { if (MouseProc.hookHandle == 0) { int hInstance = User32.GetWindowLong(Util.getWindowHandle(frame), GWL_HINSTANCE); MouseProc.hookHandle = SetWindowsHookEx(WH_MOUSE_LL, new Callback(MouseProc.class, "lowLevelMouseProc"), hInstance, 0);// Standard message dispatch loop (message pump) Msg msg = new Msg(); while (User32.GetMessage(msg, 0, 0, 0)) { User32.TranslateMessage(msg); User32.DispatchMessage(msg); } } else { mouseEventArea.append("The Hook is already installed.\n"); } }}); hookThread.start(); } private void unsetMouseHook() { setHookBtn.setEnabled(true); removeHookBtn.setEnabled(false); UnhookWindowsHookEx(MouseProc.hookHandle); MouseProc.hookHandle = 0; }private static void createAndShowGUI() { //Create and set up the window. frame = new JFrame("Mouse Hook"); frame.(JFrame.EXIT_ON_CLOSE); MouseHook MouseEventsWindow = new MouseHook(); MouseEventsWindow.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); //Add content to the window. frame.add(MouseEventsWindow, BorderLayout.CENTER); //Display the window. frame.pack(); frame.setBounds(300, 200, 750, 600); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); }}class MouseProc { static int hookHandle; @NativeImport(library = "user32") public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam); static { JInvoke.initialize(); } public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) { if (nCode 评论。

2.flash里怎么调用鼠标监听事件,as2.0

用法举例:

myObj = new Object();

myObj.change = function(evt_obj) {

aaa = evt_obj.target.selectedItem.data;

_mc.loadMovie(aaa);//_mc是一个空影片剪辑,用来装载图片。

};

mylist.addEventListener("change", myObj);

//我这里将列表的实例名叫做 mylist。

/*注解:evt_obj.target.selectedItem.data

evt_obj 被称为“事件对象”。target 是它的属性。通过对象事件的target属性,来访问selectedItem的data属性。

*/

//==============欢迎再问。

3.如何在ArcMap中监听键盘鼠标事件

单就这个功能而言,可能不一定非得开发,或许直接在属性表里批量编辑也是个办法。

但如果实现了这个功能,还可以引申出一些其他的应用,比如在要素编辑的时候自动进行质量检查;比如在地图浏览中添加许多预定义的效果等。闲话不说,先介绍一下实现思路:ArcMap中本身是无法让你监听键盘事件的,显然需要通过开发。

开发的途径,目前主要是通过Add-In。在Add-In中,我们需要写一个工具,由工具来实现对鼠标和键盘事件的监听。

详细理一下步骤:1.开发一个Add-In插件;2.在Add-In插件中,写一个工具;3.将Add-In部署到ArcMap中;4.用户点击Add-In插件的时候,代码里初始化工具,将其设置为地图的当前活动工具;5.在工具类中,监听鼠标和键盘事件,执行所需的操作。想必大家看了这几个步骤之后,觉得也没什么难度,事实上也如此,唯一需要注意的就是工具需要实现的接口,以及如何在Add-In里面初始化和调用工具。

下面是示例代码。首先定义工具: class HotKeyTool:ITool,ICommand,ICommandItem注意该工具实现了ITool,ICommand,ICommandItem接口,其中ITool的OnKeyDown方法、OnMouseDown等方法,就是你的业务逻辑代码应该放的地方: //响应按键按下事件 public void OnKeyDown(int keyCode, int shift) { Keys key = (Keys)keyCode; switch (key) { case Keys.B: MessageBox.Show("S pressed"); break; case Keys.C: MessageBox.Show("D pressed"); break; case Keys.A: MessageBox.Show("A pressed"); break; default: MessageBox.Show(key + " pressed~"); break; } } 而ICommandItem的UID属性,则是非常关键的一个属性,需要生成一个UID值并返回。

//此属性会被反复多次调用 private UID uid = null; public ESRI.ArcGIS.esriSystem.UID ID { get { if (uid == null) { uid = new UIDClass(); uid.Value = "{6B76685B-31C5-4B69-8AC3-B6E9FE16D162}"; } return uid; } } 工具类其它代码没有特别需要说明的,接下来看下如何初始化和调用工具: protected override void OnClick() { ArcMap.Application.CurrentTool = null; IMxDocument mxDoc = ArcMap.Document; IActiveView activeView = mxDoc.ActiveView; IMap map = activeView.FocusMap; //初始化工具,并将其与应用程序绑定 HotKeyTool hotKeyTool = new HotKeyTool(); hotKeyTool.App = ArcMap.Application; try { ICommandItem cmdItem = hotKeyTool as ICommandItem; //设置当前工具,以便处理键盘事件 ArcMap.Application.CurrentTool = cmdItem; } catch (Exception ex) { } }代码很简单,思路也不复杂,如需完整代码,请在评论中留下邮箱。

4.JAVA 该怎么添加鼠标事件的监听器

这样子写比较好

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

public class MyPicture extends JFrame

{

JFrame frame;

JButton a1,a2,a3;

JPanel panel;

int mouseX,mouseY,sh=10,ss=290;

String msg="坐标:";

String setX,setY;

public MyPicture() //程序界面

{

frame=new JFrame("图形学实验");

a1=new JButton("直线");

a2=new JButton("弧");

a3=new JButton("裁剪");

panel=new JPanel();

panel.add(a1);

panel.add(a2);

panel.add(a3);

frame.add(panel);

frame.(JFrame.EXIT_ON_CLOSE);

frame.setSize(300,300);

frame.setVisible(true);

addMouseListener(new MouseMonitor());

}

public void paint(Graphics g) //显示鼠标坐标

{

g.drawString(msg,sh,ss);

}

class MouseMonitor implements MouseListener {

public void mouseEntered(MouseEvent me) //鼠标进入事件

{

mouseX=getX();

mouseY=getY();

setX=mouseX+"";

setY=mouseY+"";

msg=msg+setX+" "+setY;

repaint();

}

public void mousePressed(MouseEvent me){}

public void mouseReleased(MouseEvent me) {}

public void mouseExited(MouseEvent me) {}

public void mouseClicked(MouseEvent me){}

}

public static void main(String[] args)

{

MyPicture org=new MyPicture();

}

}

设置鼠标事件监听事件

本文来自投稿,不代表本站立场,如若转载,请注明出处。