Respond to key stroke in command line programs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    #1

    Respond to key stroke in command line programs

    Hi,

    i'm a beginner/intermediate java programmer working on a client/server application as part of a lab assignment. This lab does not require a GUI but simple command line. All my features have been implemented except for one. One requirement is to have the server waiting for the client to press ^c (control + c) to end the connection between the server and the client. I know i need to write an action listener to capture this event but im not sure how to do it. I know that when using Swing, to add an action listener to a button we first create the button and then add the action listener to the button: buttonName.addA ctionListener(t his);
    Im not even sure how i would write my class to capture the "control" button being pressed plus the "c" key being pressed. It is not the case that i'm creating the "ctrl" + "c" button so how can i add a listener to this? I have googled my problem but +95% of the results return pages dealing with GUIs, and the rest just talk about action listeners in general.
    Any ideas/comments would be appreciated

    thanks
  • pbrockway2
    Recognized Expert New Member
    • Nov 2007
    • 151

    #2
    I know i need to write an action listener to capture this event
    How do you know this? In particular what would you attach the event listener to?

    Java's Consoleclass offers the ability to read lines without echoing but, beyond that,
    interaction with a character based console device is limited to what is provided by Reader and Writer: ie no events or classes to which you might attach event listeners. (And Console may very well not be instantiable.)

    You might want to consider OS specific approaches like JLine. The ConsoleReader class ethere has a addTriggeredAct ion() which seems to respond to characters. I haven't used it, however, and have no idea how it might be used in conjunction with the state of the control key.

    Or you could use a GUI. Your description counterposing a gui to a simple command line assumes that using a command line would be simpler. I'm not sure it is simpler. And I'm certain that, in Java, it's not even possible in a platform agnostic way.

    Comment

    • rotaryfreak
      New Member
      • Oct 2008
      • 74

      #3
      "How do you know this? In particular what would you attach the event listener to?"

      im assuming this is what i need to do because its what i would do if i had a GUI. I have no clue what i would attach a listener to, that is the reason i posted this topic; however, I will take your suggestion under consideration even though i've never heard of JLine, i'll look into it. Like i said, im a beginner/intermediate programmer and still learning :| Unfortunately, a GUI is out of the question because the requirements say i need command line only.

      thx

      Comment

      • pbrockway2
        Recognized Expert New Member
        • Nov 2007
        • 151

        #4
        OK. You might also ask whoever set the conditions what *they* would suggest: Java has never been good at commandline i/o and, I would have described it as the wrong tool for this job.

        Just my 2c of course. And if I'm being too sweeping someone can jump in and say so.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          I am not sure how you can achieve that. And i am also not sure whether we can achieve it or not.

          But some workaround i can suggest. Rather than waiting for ctrl+c of client in Server. Have a listener in client which notifies server that ctrl+c is pressed. So depending on this you can take appropriate steps on server side.

          Regards
          Dheeraj Joshi

          Comment

          • rotaryfreak
            New Member
            • Oct 2008
            • 74

            #6
            That sounds like a plan, but i still do not know how to accomplish this whether on the server side or the client side. As mentioned before, this is not a GUI based application so i can not add a listener to a button... i do not have any buttons. How to i monitor the keyboard for ctrl+c? Do i need to create a listener for it? and if i do, how can this be done? i'm completely in the dark about this.

            thanks for all the replies

            Comment

            • Ian Smith

              #7
              Um.. pressing control C exits a command line program already, with zero modifications. In Windows7 this works anyway, I could also go try it in linux.

              I just tried a simple program that stops and waits for user to press enter to continue with scrolling text. I press control C and the program quits completely. I'll go try this in linus as well.

              Comment

              • Ian Smith

                #8
                As I thought, control c also stops a cmd line program in Linux. I don't have a MAC to test on.

                Comment

                Working...