您的位置:首页 > 编程语言 > Java开发

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编程,所以这个思路就这么形成了。呵呵。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: