java获取系统鼠标位置(jni+dll应用)
2006-10-18 09:52
489 查看
最近玩游戏点击鼠标,按键觉得太辛苦了,想写辅助程序帮忙操作。java中没有找到获取系统鼠标的方法,就借助dll来实现。
先建立java文件MouseInfo.java
public class MouseInfo ...{
static...{
System.loadLibrary("MouseInfo");
}
public native static int getMousePosX();
public native static int getMousePosY();
public static void main(String[] args)throws Exception ...{
// TODO Auto-generated method stub
MouseInfo mouseInfo = new MouseInfo();
System.out.println("x:"+mouseInfo.getMousePosX()+"y:"+mouseInfo.getMousePosY());
}
}
javac MouseInfo.java
javah MouseInfo
生成MouseInfo.h
/**//* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/**//* Header for class MouseInfo */
#ifndef _Included_MouseInfo
#define _Included_MouseInfo
#ifdef __cplusplus
extern "C" ...{
#endif
/**//*
* Class: MouseInfo
* Method: getMousePosX
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosX
(JNIEnv *, jclass);
/**//*
* Class: MouseInfo
* Method: getMousePosY
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosY
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
用vc建一个win32动态dll工程
建立StdAfx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
#define AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h> // MFC OLE classes
#include <afxodlgs.h> // MFC OLE dialog classes
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h> // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT
#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h> // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
建立MouseInfo.cpp
#include "MouseInfo.h"
#include "StdAfx.h"
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosX
(JNIEnv *, jclass)
...{
//获取当前系统鼠标的x坐标,返回int型
int xPos = 100;
POINT pt;
GetCursorPos(&pt);
xPos = pt.x;
return xPos;
}
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosY
(JNIEnv *, jclass)
...{
//获取当前系统鼠标的y坐标,返回int型
int yPos = 100;
POINT pt;
GetCursorPos(&pt);
yPos = pt.y;
return yPos;
}
将生成的MouseInfo.dll放入java项目中运行。
这样在java中就可以获取到鼠标的系统位置。
然后应用java中的Robot类来方便的处理各种模拟事件。也许这是一种多此一举的做法,因为习惯了java编程,所以这个思路就这么形成了。呵呵。
先建立java文件MouseInfo.java
public class MouseInfo ...{
static...{
System.loadLibrary("MouseInfo");
}
public native static int getMousePosX();
public native static int getMousePosY();
public static void main(String[] args)throws Exception ...{
// TODO Auto-generated method stub
MouseInfo mouseInfo = new MouseInfo();
System.out.println("x:"+mouseInfo.getMousePosX()+"y:"+mouseInfo.getMousePosY());
}
}
javac MouseInfo.java
javah MouseInfo
生成MouseInfo.h
/**//* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/**//* Header for class MouseInfo */
#ifndef _Included_MouseInfo
#define _Included_MouseInfo
#ifdef __cplusplus
extern "C" ...{
#endif
/**//*
* Class: MouseInfo
* Method: getMousePosX
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosX
(JNIEnv *, jclass);
/**//*
* Class: MouseInfo
* Method: getMousePosY
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosY
(JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
用vc建一个win32动态dll工程
建立StdAfx.h
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
#define AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h> // MFC OLE classes
#include <afxodlgs.h> // MFC OLE dialog classes
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h> // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT
#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h> // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
建立MouseInfo.cpp
#include "MouseInfo.h"
#include "StdAfx.h"
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosX
(JNIEnv *, jclass)
...{
//获取当前系统鼠标的x坐标,返回int型
int xPos = 100;
POINT pt;
GetCursorPos(&pt);
xPos = pt.x;
return xPos;
}
JNIEXPORT jint JNICALL Java_MouseInfo_getMousePosY
(JNIEnv *, jclass)
...{
//获取当前系统鼠标的y坐标,返回int型
int yPos = 100;
POINT pt;
GetCursorPos(&pt);
yPos = pt.y;
return yPos;
}
将生成的MouseInfo.dll放入java项目中运行。
这样在java中就可以获取到鼠标的系统位置。
然后应用java中的Robot类来方便的处理各种模拟事件。也许这是一种多此一举的做法,因为习惯了java编程,所以这个思路就这么形成了。呵呵。
相关文章推荐
- Java通过MouseInfo获取鼠标位置
- Java中获取鼠标位置
- java 动态获取当前系统桌面位置
- (java)获取鼠标位置坐标
- 超简单的java获取鼠标点击位置坐标的实例(鼠标在Jframe上的坐标)
- Java应用过程中获取系统属性
- JAVA获取文件本身所在的磁盘位置
- 分享java程序设计中一个小程序:获取当前系统日期和时间在小程序中显示出来
- Java获取当前系统事件System.currentTimeMillis()方法
- 在火狐浏览器中获取鼠标位置
- JAVA WEB获取系统运行时间
- JAVA中获取当前系统时间
- JAVA远程获取linux系统的信息
- js获取鼠标位置的各种方法
- 在PV3D中获取鼠标点击位置的空间坐标信息
- 我的Java学习路一:系统变量获取
- JAVA中获取当前系统时间
- js获取鼠标位置
- HTML页面上获取鼠标的位置(备忘)
- WPF中获取鼠标相对于桌面位置