Google Answers Logo
View Question
 
Q: Using Java JNI to get cursor information from Windows ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: Using Java JNI to get cursor information from Windows
Category: Computers > Programming
Asked by: mr_rufus-ga
List Price: $30.00
Posted: 13 Nov 2003 13:08 PST
Expires: 13 Dec 2003 13:08 PST
Question ID: 275551
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
Answer  
Subject: Re: Using Java JNI to get cursor information from Windows
Answered By: endo-ga on 30 Nov 2003 12:03 PST
Rated:4 out of 5 stars
 
Hi again mr_rufus!

It's nice to hear from you again and I'm glad to see you were able to
achieve some progress. I thank you for your concern, but I have no
problem with helping clients as much as I can, even if it can take a
certain amount of time. :)

Your question was very interesting and I'm happy to have taken the challenge.

If you need any more help or require clarifications please do not hesitate to ask.

Thanks
endo

Search Strategy

jni tutorial
://www.google.com/search?q=jni%20tutorial

java cursor position
://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&safe=off&q=java+cursor+position

c cursor position
://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&safe=off&q=c+cursor+position

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
mr_rufus-ga rated this answer:4 out of 5 stars
Endo is extremely dedicated to finding the answer. He asked the right
questions and compiled a really useful collection of data from which I
was able to gather everything I needed to solve this fairly
complicated, cross language, programming problem.

Comments  
Subject: Re: Using Java JNI to get cursor information from Windows
From: eadfrith-ga on 15 Nov 2003 13:17 PST
 
You can achieve what you're trying to do with the SWT package. It has
a Display class with a method getCursorLocation method, which returns
the position of the cursor relative to the top left hand corner of the
screen. The difference between SWT and AWT is that you don't need to
create a window and you can call it from your own thread, ie. no
display threads are created in the background, as they are with AWT.
The following code shows a possible usage, whereby the current cursor
location is printed to the console every 200ms or so:

import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Display;

public class CursotLocationDemo
{
  public static void main(String[] args)
  {
    Display d = new Display();
    while(true)
    {
      Point p = d.getCursorLocation();
      System.out.println("Cursor location is: " + p);
      try{ Thread.sleep(200); } catch (InterruptedException e) {}
    }
  }
}

You can download SWT from here:

http://download2.eclipse.org/downloads/drops/R-2.1.2-200311030802/index.php

Scroll to the bottom of the screen to the section titled "SWT Binary
and Source". Include the swt.jar file in your classpath and place the
dll somewhere in your windows path and you're good to go.

SWT is covered by the Common Public License, which is one of the more
liberal open source licenses, so if you really want to then you strip
out the classes you need (Display, and superclasses, and Point) and
build your own, smaller jar file. You'll still need the dll though. As
you'd expect SWT simply makes a JNI call to windows, so if you're
feeling really adventurous, or wish to learn some JNI, then you could
take a look at the c source code to see how the windows system call is
actually made. If you'd like some pointers on this let me know.

Cheers,

Eadfrith
Subject: Re: Using Java JNI to get cursor information from Windows
From: endo-ga on 24 Nov 2003 11:15 PST
 
Hi,

To be honest my C/C++ skills aren't very good.
But I've tried the following and it works and compiles in Visual Studio .NET

#include <iostream>
#include <Windows.h>


void main()
{
  {
   tagPOINT pointCursor;
   int i = 0;
   while(true){
    if(i==0){
        GetCursorPos(&pointCursor);
        std::cout<<pointCursor.x<<" "<<pointCursor.y<<"\n";
   }
   i++;
   if(i==100000000)
     i=0;
    }
  }
}

This will loop around outputting the coordinates of the mouse cursor
until you press control+c on the prompt. Depending on your compiler
the syntax might be slightly different. But adapting your helloWorld
program would consist of removing the printf line and replacing it
with the contents of the above main function and adapting it to your
needs, while not forgetting to include Windows.h.

I hope this helps, please let me know how it goes.

Thanks.
endo

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy