CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2009
    Posts
    9

    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?

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: Java Native Interface Question

    Quote Originally Posted by knobody View Post
    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.

  3. #3
    Join Date
    Feb 2009
    Posts
    9

    Re: Java Native Interface Question

    Thanks nuzzle, I'll check it out

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Native Interface Question

    Quote Originally Posted by nuzzle View Post
    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.

  5. #5
    Join Date
    May 2009
    Posts
    2,413

    Re: Java Native Interface Question

    Quote Originally Posted by dlorde View Post
    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.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Native Interface Question

    Quote Originally Posted by nuzzle View Post
    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.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Java Native Interface Question

    Quote Originally Posted by dlorde View Post
    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?

  8. #8
    Join Date
    Feb 2009
    Posts
    9

    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?

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Native Interface Question

    Quote 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.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Java Native Interface Question

    Quote 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.

  11. #11
    Join Date
    Feb 2009
    Posts
    9

    Re: Java Native Interface Question

    Quote Originally Posted by keang View Post
    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.

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Native Interface Question

    Quote Originally Posted by nuzzle View Post
    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.

  13. #13
    Join Date
    May 2009
    Posts
    2,413

    Re: Java Native Interface Question

    Quote Originally Posted by keang View Post
    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.

  14. #14
    Join Date
    May 2009
    Posts
    2,413

    Re: Java Native Interface Question

    Quote Originally Posted by dlorde View Post
    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.

  15. #15
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java Native Interface Question

    Quote Originally Posted by nuzzle View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured