

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
1 message in net.java.dev.jna.usersHandling mouse events...| From | Sent On | Attachments |
|---|---|---|
| Dino Silva Sauro | Feb 7, 2008 10:58 am |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Handling mouse events... | Actions... |
|---|---|---|
| From: | Dino Silva Sauro (silv...@gmail.com) | |
| Date: | Feb 7, 2008 10:58:45 am | |
| List: | net.java.dev.jna.users | |
Hi there,
I've been studing some uses in JNA, and, also, made some modifications int the keyBoardHook code, to mouseHook to, now, I'm having a diferent trouble... I have a code in C++ and want to write it entirely in Java(with JNA). Can someone teach me?
Hear is the C code:
main.cpp [code] ////////////////////////////////////////////////////////////////////// // Defines the entry point
#include "EventSource.h" #include "MouseDownListener.h" #include "Jvm.h"
#include <process.h> #include <windows.h>
void __cdecl ThreadMain(void * pSource) { ((CEventSource *)pSource)->fireMouseDownEvent(50, 100); CJvm::DetachThread(); }
int main(int argc, char* argv[]) { CEventSource eventSource;
CMouseDownListener listener; eventSource.addMouseDownListener(&listener);
CJvm::CreateJVM(argc-1, &argv[1]); jobject jobj = CJvm::CreateJavaObject("events/MouseDownListener", (int)(IEventSource *)&eventSource);
_beginthread(ThreadMain, 0, &eventSource); Sleep(2000);
CJvm::ReleaseJObject(jobj); CJvm::DestroyJVM();
return 0; } [/code]
eventSource.cpp [code] ////////////////////////////////////////////////////////////////////// // implementation of the CEventSource class.
#include "EventSource.h"
////////////////////////////////////////////////////////////////////// // Construction/Destruction
CEventSource::CEventSource() {
}
CEventSource::~CEventSource() {
}
int CEventSource::addMouseDownListener(IMouseDownListener * listener) { if (NULL == listener) { return -1; }
m_setListeners.insert(listener); return 0; }
int CEventSource::removeMouseDownListener(IMouseDownListener * listener) { if (NULL == listener) { return -1; }
m_setListeners.erase(listener); return 0; }
void CEventSource::fireMouseDownEvent(int hPos, int vPos) { for (set<IMouseDownListener *>::iterator iter = m_setListeners.begin(); iter != m_setListeners.end(); iter++) { (*iter)->onMouseDown(hPos, vPos); }
return; } [/code]
MouseDownListener.cpp [code] ////////////////////////////////////////////////////////////////////// // Implementation of the CMouseDownListener class.
#include "MouseDownListener.h"
#include <iostream> using namespace std;
////////////////////////////////////////////////////////////////////// // Construction/Destruction
CMouseDownListener::CMouseDownListener() { }
CMouseDownListener::~CMouseDownListener() { }
void CMouseDownListener::onMouseDown(int hPos, int vPos) { cout << "In CMouseDownListener::onMouseDown" << endl; cout << " X = " << hPos << endl; cout << " Y = " << vPos << endl; } [/code]
events_EventSourceProxy.cpp [code] #include "common.h" #include "events_EventSourceProxy.h" #include "Jvm.h"
#include <iostream> using namespace std;
class JMouseDownListener : public IMouseDownListener { private: jobject m_jobj; jmethodID m_mid;
public: JMouseDownListener(JNIEnv * env, jobject jobj, jstring methodName, jclass javaClass); virtual ~JMouseDownListener();
void onMouseDown(int hPos, int vPos); };
JNIEXPORT jint JNICALL Java_events_EventSourceProxy_registerListener (JNIEnv * env, jobject jobj, jint handleEventSource, jstring methodName, jclass javaClass) { IEventSource * pEventSource = (IEventSource *) handleEventSource;
if (NULL == pEventSource) { return 0; }
JMouseDownListener * pListener = new JMouseDownListener(env, jobj, methodName, javaClass);
if (NULL == pListener) { return 0; }
pEventSource->addMouseDownListener(pListener);
return (jint)pListener; }
JNIEXPORT jint JNICALL Java_events_EventSourceProxy_unregisterListener (JNIEnv *, jobject, jint handleEventSource, jint handleNativeListener) { IEventSource * pEventSource = (IEventSource *) handleEventSource;
if (NULL == pEventSource) { return -1; }
JMouseDownListener * pListener = (JMouseDownListener *) handleNativeListener;
if (NULL == pListener) { return 0; }
pEventSource->removeMouseDownListener(pListener);
delete pListener;
return 0; }
#include <windows.h>
JMouseDownListener::JMouseDownListener(JNIEnv * env, jobject jobj, jstring methodName, jclass javaClass) : m_jobj(NULL), m_mid(NULL) { if ((NULL == methodName) || (NULL == jobj)) { return; }
m_jobj = env->NewGlobalRef(jobj);
if (NULL == m_jobj) { return; }
const char * szMethodName = env->GetStringUTFChars(methodName, 0); m_mid = env->GetMethodID(javaClass, szMethodName, "(II)V"); env->ReleaseStringUTFChars(methodName, szMethodName); }
JMouseDownListener::~JMouseDownListener() { if (NULL == m_jobj) { return; }
JNIEnv * env = CJvm::GetJNIEnv();
if (NULL == env) { return; }
env->DeleteGlobalRef(m_jobj); }
void JMouseDownListener::onMouseDown(int hPos, int vPos) { if ((NULL == m_jobj) || (NULL == m_mid)) { return; }
JNIEnv * env = CJvm::GetJNIEnv();
if (NULL == env) { return; }
env->CallVoidMethod(m_jobj, m_mid, (jint)hPos, (jint)vPos); } [/code]
Obviously, I don´t want that someone do all the job to me, mainly because I must learn, but if "translate" only events_EventSourceProxy.cpp i will apreciate!
Tks







