I am writing a program in Java that requires cursor position
information from Windows (not from the program gui). I have heard that
Java Native Interface can
be used, but being a new programmer I have no idea how to start.
Could anyone please list the steps in an 'easy to understand' fashion
implementing a cursor related system call in the Windows OS.
Thanks |
Request for Question Clarification by
mathtalk-ga
on
14 Nov 2003 07:57 PST
Hi, mr_rufus-ga:
Please clarify what "cursor position" you are talking about. My first
thought was that you mean a screen cursor (relative) to some "window"
or frame displayed, but I'm far from sure this is what you are
interested in.
The word cursor is also in connection with file and database
operations, and it's entirely possible to make sense of "cursor
position" in these contexts as well.
regards, mathtalk-ga
|
Clarification of Question by
mr_rufus-ga
on
14 Nov 2003 09:29 PST
Sorry, when I say cursor position I mean the mouse cursor position. I
have written some code in Visual Basic that will do the job but I need
a JNI implementation.
Here is a snippet of the VB code - makes the form caption the current XY position
Dim FSys As New FileSystemObject
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex
As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
X As Long
Y As Long
End Type
'Get mouse position
Sub currentPos()
Dim pointerPos As POINTAPI
Dim getPos As Long
getPos = GetCursorPos(pointerPos)
frmMouse.Caption = pointerPos.X & " " & pointerPos.Y
End Sub
Thanks for the request.
|
Request for Question Clarification by
endo-ga
on
14 Nov 2003 11:16 PST
Hi,
When you say "cursor position information from Windows", do you mean
the position of the cursor relative to the screen rather than to the
Java component you are using?
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
14 Nov 2003 15:34 PST
Relative to the screen.
|
Request for Question Clarification by
endo-ga
on
14 Nov 2003 15:45 PST
Thank you for your clarification.
Does it necessarily have to be with JNI? Normal Java functions can
allow you to find this information. Would you be satisfied with a way
to do this in Java?
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
15 Nov 2003 06:40 PST
Hi endo
I see what your getting at but the cursor information has to come from
Windows not from Java. I want information from the cursor all the
time, no matter what application a user might be using. I believe Java
can only give information on cursor position from within its own
window using a listener.
My first version used Visual Basic to write to file and java to read,
but as you can imagine all the io work meant that the program wasn't
efficient or elegant.
Thanks
mr_rufus
|
Request for Question Clarification by
endo-ga
on
15 Nov 2003 06:53 PST
Hi,
Java can also give the position of the window within Windows, which is
obviously a Windows call. So if you add both coordinates, you get what
you want. I have tested this with a frame, moved it around and
clicked, and I do get the actual position of the mouse pointer
relative to the screen. I.e. when the top left of the frame is in the
bottom right of my screen, I get my screen resolution as coordinates.
If you're interested in this solution, I can post it as an answer.
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
15 Nov 2003 07:43 PST
Hi endo
Thanks for the quick reply.
I'm a little confused, I'm I right in thinking:
-Even if java is running in the background, i.e. no window or java gui
and the user points all over the desktop (or say photoshop), from left
corner, all around the screen I can get the cursor position?
-Your solution uses Windows API calls?
-It doesn't use the JNI?
Thanks
mr_rufus
|
Request for Question Clarification by
endo-ga
on
15 Nov 2003 10:28 PST
Hi,
Sorry I had misunderstood your request at first. This requires you to
click within the frame of the Java application. I think you are right
in assuming that what you want can't be done within pure Java.
Having a look at JNI it seems more complicated to use JNI with VB than
it is with C++. If the only reason you have for using VB is to get the
mouse position, then in C++ you only need to following to get the
mouse position:
POINT pointCursor;
GetCursorPos(&pointCursor);
If you have other reasons for using VB, here are a few links on using
JNI in general and JNI with VB. I hope this helps.
Enhance your Java application with Java Native Interface
http://www.javaworld.com/javaworld/jw-10-1999/jw-10-jni.html
Writing a Java Program with Native Methods
http://java.sun.com/docs/books/tutorial/native1.1/implementing/index.html
JNI Programming in C++
http://java.sun.com/docs/books/tutorial/native1.1/implementing/cpp.html
Sample JNI dll and Java test code
http://www.codeproject.com/useritems/jnisample.asp
Calling native functions which are written in C or C++ from Java using JNI
http://www.codeproject.com/useritems/jnibasics1.asp
How to call VB code from Java
http://milinddev.tripod.com/javavbvccall.htm
If you require any more help please let me know.
If at any point you are satisfied with my answer, please let know.
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
18 Nov 2003 04:08 PST
Hi, endo, hi Eadfrith
Thanks for your comments. I won't be able to check out the info for a
couple of days due to work commitments. I really apreciate the
feedback and will post a note soon.
Thanks
mr_rufus
|
Request for Question Clarification by
endo-ga
on
23 Nov 2003 18:56 PST
Hi,
Have you had any luck with your query?
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
24 Nov 2003 05:13 PST
Hi endo
Using Sun's guide on JNI I was finally able to create the simple Hello
World program using the simple C program:
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");
return;
}
I need to write a C class to show the cursor (so i can hide it) and to
get the cursor position. You have given me (POINT
pointCursor;GetCursorPos(&pointCursor);) but I don't know how to
modify my hello world c code to do this as I have no clue about c.
I would be willing to accept this code as your answer
Thanks for all your help
mr_rufus
|
Clarification of Question by
mr_rufus-ga
on
27 Nov 2003 06:31 PST
Hi endo
Thanks for your reply, the code you posted won't work with visual c++.
Seems to me my problem is just understanding C structure, so i'd
better get a book.
Thanks for all your effort.
mr_rufus
|
Request for Question Clarification by
mathtalk-ga
on
27 Nov 2003 06:57 PST
Hi, mr_rufus-ga:
If by "won't work with visual c++" you mean the VS 6.0 version, then I
think the problem in running endo-ga's code is pretty minor to fix
(nothing as fancy as the structs). Is that what you tried? Note that
endo-ga reports using VS .Net.
regards, mathtalk-ga
|
Request for Question Clarification by
endo-ga
on
27 Nov 2003 07:56 PST
Hi,
As mathtalk said, I'm sure it's a simple syntaxical difference between
the 2 versions, what errors do you get and where?
Thanks.
endo
|
Clarification of Question by
mr_rufus-ga
on
30 Nov 2003 11:53 PST
Hi all
I got the code working in C now, the problem was minor, so all I have
to do is return the results in an object, back to Java and that side
of the program should be finished. Endo, you must be getting ticked
off with this query, don't mind if you wanna get paid for your hard
work and i'll try and sort the rest.
Thanks
mr_rufus
|