如何通过java获取系统鼠标位置?
如何通过java获取系统鼠标位置?
最近玩游戏点击鼠标,按键觉得太辛苦了,想写辅助程序帮忙操作。java中没有找到获取系统鼠标的方法,就借助dll来实现。
先建立java文件MouseInfo.java
publicclassMouseInfo{
static{
System.loadLibrary("MouseInfo");
}
publicnativestaticintgetMousePosX();
publicnativestaticintgetMousePosY();
publicstaticvoidmain(String[]args)throwsException{
//TODOAuto-generatedmethodstub
MouseInfomouseInfo=newMouseInfo();
System.out.println("x:"+mouseInfo.getMousePosX()+"y:"+mouseInfo.getMousePosY());
}
}
static{
System.loadLibrary("MouseInfo");
}
publicnativestaticintgetMousePosX();
publicnativestaticintgetMousePosY();
publicstaticvoidmain(String[]args)throwsException{
//TODOAuto-generatedmethodstub
MouseInfomouseInfo=newMouseInfo();
System.out.println("x:"+mouseInfo.getMousePosX()+"y:"+mouseInfo.getMousePosY());
}
}
javac MouseInfo.java
javah MouseInfo
生成MouseInfo.h
/*DONOTEDITTHISFILE-itismachinegenerated*/
#include<jni.h>
/*HeaderforclassMouseInfo*/
#ifndef_Included_MouseInfo
#define_Included_MouseInfo
#ifdef__cplusplus
extern"C"{
#endif
/*
*Class:MouseInfo
*Method:getMousePosX
*Signature:()I
*/
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosX
(JNIEnv*,jclass);
/*
*Class:MouseInfo
*Method:getMousePosY
*Signature:()I
*/
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosY
(JNIEnv*,jclass);
#ifdef__cplusplus
}
#endif
#endif
#include<jni.h>
/*HeaderforclassMouseInfo*/
#ifndef_Included_MouseInfo
#define_Included_MouseInfo
#ifdef__cplusplus
extern"C"{
#endif
/*
*Class:MouseInfo
*Method:getMousePosX
*Signature:()I
*/
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosX
(JNIEnv*,jclass);
/*
*Class:MouseInfo
*Method:getMousePosY
*Signature:()I
*/
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosY
(JNIEnv*,jclass);
#ifdef__cplusplus
}
#endif
#endif
用vc建一个win32动态dll工程
建立StdAfx.h
//stdafx.h:includefileforstandardsystemincludefiles,
//orprojectspecificincludefilesthatareusedfrequently,but
//arechangedinfrequently
//
#if!defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
#defineAFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#defineVC_EXTRALEAN//Excluderarely-usedstufffromWindowsheaders
#include<afxwin.h>//MFCcoreandstandardcomponents
#include<afxext.h>//MFCextensions
#ifndef_AFX_NO_OLE_SUPPORT
#include<afxole.h>//MFCOLEclasses
#include<afxodlgs.h>//MFCOLEdialogclasses
#include<afxdisp.h>//MFCAutomationclasses
#endif//_AFX_NO_OLE_SUPPORT
#ifndef_AFX_NO_DB_SUPPORT
#include<afxdb.h>//MFCODBCdatabaseclasses
#endif//_AFX_NO_DB_SUPPORT
#ifndef_AFX_NO_DAO_SUPPORT
#include<afxdao.h>//MFCDAOdatabaseclasses
#endif//_AFX_NO_DAO_SUPPORT
#include<afxdtctl.h>//MFCsupportforInternetExplorer4CommonControls
#ifndef_AFX_NO_AFXCMN_SUPPORT
#include<afxcmn.h>//MFCsupportforWindowsCommonControls
#endif//_AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
//orprojectspecificincludefilesthatareusedfrequently,but
//arechangedinfrequently
//
#if!defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
#defineAFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_
#if_MSC_VER>1000
#pragmaonce
#endif//_MSC_VER>1000
#defineVC_EXTRALEAN//Excluderarely-usedstufffromWindowsheaders
#include<afxwin.h>//MFCcoreandstandardcomponents
#include<afxext.h>//MFCextensions
#ifndef_AFX_NO_OLE_SUPPORT
#include<afxole.h>//MFCOLEclasses
#include<afxodlgs.h>//MFCOLEdialogclasses
#include<afxdisp.h>//MFCAutomationclasses
#endif//_AFX_NO_OLE_SUPPORT
#ifndef_AFX_NO_DB_SUPPORT
#include<afxdb.h>//MFCODBCdatabaseclasses
#endif//_AFX_NO_DB_SUPPORT
#ifndef_AFX_NO_DAO_SUPPORT
#include<afxdao.h>//MFCDAOdatabaseclasses
#endif//_AFX_NO_DAO_SUPPORT
#include<afxdtctl.h>//MFCsupportforInternetExplorer4CommonControls
#ifndef_AFX_NO_AFXCMN_SUPPORT
#include<afxcmn.h>//MFCsupportforWindowsCommonControls
#endif//_AFX_NO_AFXCMN_SUPPORT
//{{AFX_INSERT_LOCATION}}
//MicrosoftVisualC++willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
#endif//!defined(AFX_STDAFX_H__9CE33D16_5EAB_479B_A1CE_AEC7E4B243B6__INCLUDED_)
建立MouseInfo.cpp
#include"MouseInfo.h"
#include"StdAfx.h"
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosX
(JNIEnv*,jclass)
{
//获取当前系统鼠标的x坐标,返回int型
intxPos=100;
POINTpt;
GetCursorPos(&pt);
xPos=pt.x;
returnxPos;
}
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosY
(JNIEnv*,jclass)
{
//获取当前系统鼠标的y坐标,返回int型
intyPos=100;
POINTpt;
GetCursorPos(&pt);
yPos=pt.y;
returnyPos;
}
#include"StdAfx.h"
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosX
(JNIEnv*,jclass)
{
//获取当前系统鼠标的x坐标,返回int型
intxPos=100;
POINTpt;
GetCursorPos(&pt);
xPos=pt.x;
returnxPos;
}
JNIEXPORTjintJNICALLJava_MouseInfo_getMousePosY
(JNIEnv*,jclass)
{
//获取当前系统鼠标的y坐标,返回int型
intyPos=100;
POINTpt;
GetCursorPos(&pt);
yPos=pt.y;
returnyPos;
}
将生成的MouseInfo.dll放入java项目中运行。
这样在java中就可以获取到鼠标的系统位置。
然后应用java中的Robot类来方便的处理各种模拟事件。也许这是一种多此一举的做法,因为习惯了java编程,所以这个思路就这么形成了。呵呵。