-
June 1st, 2009, 04:55 PM
#1
Java Native Interface Question
I'm tasked with creating a GUI for a multi-threaded C++ network server program that has it's main function running with a while loop that checks for keystrokes from the user. The C++ code for the loop is basically:
Code:
while(i!='q')
{
if(kbhit())
{
i=getch();
processkey(i);
}
}
And the processkey function performs different actions based on the key that was pressed.
What I'm wondering is, with the JNI is it possible to direct input from the click of a button in a JAVA GUI to function as though I've just entered a keystroke in the C++ program?
-
June 1st, 2009, 05:20 PM
#2
Re: Java Native Interface Question
Originally Posted by knobody
What I'm wondering is, with the JNI is it possible to direct input from the click of a button in a JAVA GUI to function as though I've just entered a keystroke in the C++ program?
I'm not sure you even need JNI. There's a Java class called Robot. Check for it here,
http://java.sun.com/javase/6/docs/api/
which allows you to send key-presses from Java code to the computer's keyboard.
You should be able to read the users wishes from the Java GUI and then control the C++ program via the keyboard using the Robot class.
-
June 1st, 2009, 05:58 PM
#3
Re: Java Native Interface Question
Thanks nuzzle, I'll check it out
-
June 2nd, 2009, 04:51 AM
#4
Re: Java Native Interface Question
Originally Posted by nuzzle
You should be able to read the users wishes from the Java GUI and then control the C++ program via the keyboard using the Robot class.
How would that work? Doesn't the Java app have keyboard focus when Robot simulates the key press?
Everything should be made as simple as possible, but not simpler...
A. Einstein
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 5th, 2009, 12:40 AM
#5
Re: Java Native Interface Question
Originally Posted by dlorde
How would that work? Doesn't the Java app have keyboard focus when Robot simulates the key press?
You can control a lot from the keyboard by typing different keyboard combinations, including switching applications. If you're running Windows try pressing LEFT_ALT TAB for example.
My suggestion maybe isn't the perfect solution but its simple and straightforward and doesn't require JNI. Effectively the Robot class will allow you to execute keyboard macroes from Java code. All you need is a little creativity in putting together those macroes.
-
June 5th, 2009, 06:12 AM
#6
Re: Java Native Interface Question
Originally Posted by nuzzle
You can control a lot from the keyboard by typing different keyboard combinations, including switching applications.
OK - it's not something I've tried, but it sounds like could be useful.
... All you need is a little creativity in putting together those macroes.
That, I suspect, is the tricky part
The outcome of any serious research can only be to make two questions grow where only one grew before...
T. Veblen
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 5th, 2009, 09:30 AM
#7
Re: Java Native Interface Question
Originally Posted by dlorde
That, I suspect, is the tricky part
Well, then why don't you present your own much better solution, instead of just calling mine into question all the time?
-
June 5th, 2009, 10:34 AM
#8
Re: Java Native Interface Question
Hey, I've been trying to make a small test case application using the Robot class before trying to implement it in my larger project.
I created a program with a JFrame with two buttons, one starts a simple C++ program that loops, waiting for keypresses and which will close when 'q' is pressed. The other button creates a Robot instance and tells that robot to send a key press of 'q', which should terminate the C++ program.
It isn't working though. Could it be because when the program is started from my Java app, it runs in the background rather than opening in a new command prompt window?
-
June 5th, 2009, 11:22 AM
#9
Re: Java Native Interface Question
Originally Posted by nuzzle
If you're running Windows try pressing LEFT_ALT TAB for example.
This may work (I've never tried it so can't be sure) but it does assume that the next window that will gain focus is the one you want to communicate with. If you have multiple applications open, the order in which they open becomes critical to the success/failure of this solution.
This may be a neat trick but it is not a robust solution.
Well, then why don't you present your own much better solution, instead of just calling mine into question all the time?
Questioning your suggestion is not a personal attack. If you post a solution to someones problem then you have to be prepared for people to discuss it and maybe even criticise it if they believe it has weaknesses.
If we didn't point out the possible drawbacks then the OP might get the wrong impression about how safe it was to use your suggestion. On the other hand the OP might decide that in their particular circumstances the drawbacks are not relevant, but at least they can make an informed descision.
-
June 5th, 2009, 11:30 AM
#10
Re: Java Native Interface Question
Originally Posted by knobody
It isn't working though. Could it be because when the program is started from my Java app, it runs in the background rather than opening in a new command prompt window?
Yes.
nuzzle's suggestion relies on the C++ application having a window to move the focus to.
-
June 5th, 2009, 12:54 PM
#11
Re: Java Native Interface Question
Originally Posted by keang
Yes.
nuzzle's suggestion relies on the C++ application having a window to move the focus to.
Dang, so maybe I should go back to trying JNI.
-
June 5th, 2009, 06:04 PM
#12
Re: Java Native Interface Question
Originally Posted by nuzzle
Well, then why don't you present your own much better solution, instead of just calling mine into question all the time?
Smiley missed error in #6. I don't have a better solution, I've never really looked at this kind of requirement. You're being a little over-sensitive - I'm not calling your solution into question at all, just saying I've never tried it and I imagine that putting the macros together is the tricky part of implementing it. If that annoys you, you're going to go through life being annoyed a lot of the time
That language is an instrument of human reason, and not merely a medium for the expression of thought, is a truth generally admitted...
G. Boole
Last edited by dlorde; June 5th, 2009 at 06:08 PM.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
June 8th, 2009, 05:40 PM
#13
Re: Java Native Interface Question
Originally Posted by keang
nuzzle's suggestion relies on the C++ application having a window to move the focus to.
Well of course.
The whole idea is to establish a line of communication with the C++ programs. And to do that you need to know how these programs take their input.
-
June 8th, 2009, 05:44 PM
#14
Re: Java Native Interface Question
Originally Posted by dlorde
I don't have a better solution, I've never really looked at this kind of requirement. You're being a little over-sensitive - I'm not calling your solution into question at all, just saying I've never tried it and I imagine that putting the macros together is the tricky part of implementing it. If that annoys you, you're going to go through life being annoyed a lot of the time
Well, I'm sorry if I over-reacted. I just feel the bulk of people are trying to get credit from opposing other people's ideas rather than suggesting something themselves.
-
June 9th, 2009, 04:59 AM
#15
Re: Java Native Interface Question
Originally Posted by nuzzle
Well, I'm sorry if I over-reacted.
No problem, we get a lot worse...
I just feel the bulk of people are trying to get credit from opposing other people's ideas rather than suggesting something themselves.
Agreed there are a lot of them online, although I don't think it's the majority - the assholes tend to be louder and stick in the mind more. There's also the problem with text-only communication - without any other cues it's very easy to misconstrue someone's attitude, and smileys don't really bridge the gap. It's also easy to be a dick when you're anonymous - some people can only feel they're somebody if they can stir up some emotion. I try not to give them the pleasure, and generally give them the 'patient, understanding adult' response, which they hate more than anything
They may forget what you said, but they will never forget how you made them feel...
C. Buchner
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|