473,465 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Respond to key stroke in command line programs

74 New Member
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.addActionListener(this);
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
Oct 3 '10 #1
7 5285
pbrockway2
151 Recognized Expert New Member
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 addTriggeredAction() 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.
Oct 3 '10 #2
rotaryfreak
74 New Member
"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
Oct 4 '10 #3
pbrockway2
151 Recognized Expert New Member
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.
Oct 4 '10 #4
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
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
Oct 4 '10 #5
rotaryfreak
74 New Member
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
Oct 4 '10 #6
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.
Oct 5 '10 #7
As I thought, control c also stops a cmd line program in Linux. I don't have a MAC to test on.
Oct 5 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: rkoida | last post by:
Hello evryone I am a newbie to python. I have a makefile which i can compile in UNIX/LINUX, But i I am planning to write a python script which actually does what my MAKEFILE does. The make file...
7
by: Ken Innes | last post by:
I am trying to write a little program that can be run as a Windows program or as a command line program. I'm really not sure the best way to do this, so what I came up with so far was to check for...
6
by: Yevgeniy (Eugene) Medynskiy | last post by:
Hi all, This is probably a very newbie question, but after searching google and docs @ python.org I can't find an answer, so maybe someone would be able to help? I'd like to call command-line...
5
by: randyelliott | last post by:
Good Day, I have a MS Access (Access 2000 now upgraded to 2003) database that tracks customer information. One function of this database is to create an encrypted license file for our software,...
1
by: Steve Bishop | last post by:
I was wondering since ASP.Net and C# work server side, is it possible to run command line programs from a Web page? I was wanting to active an exe like program on the server from a hyperlink. Help...
3
by: guy levi | last post by:
Running command line programs from vb.net and capturing the output how do i do this?
8
by: matt | last post by:
hello, i work on a private enterprise intranet. we now have a business need for opening other desktop applications, from my web app's click events. (my application is not entirely complete, so...
2
by: Jeroen | last post by:
Can anyone recommend a book or tutorial about programming a command line tool? I need to build a .NET C# tool that starts some batch processes, so some info on how to build a nice syntax for the...
0
by: chandan agarwal | last post by:
hi I have been given the task of building vc++ programs at the command line using make software. i have installed gnu make-3.81 version. i was able to build and run makefiles for turbo c++. but...
2
by: Brad | last post by:
Hi... we do a lot of stuff in C++. Mostly back end, command line programs. Recently, we've been asked to write a GUI front end to a few of these apps. In the past, we have used QT and WX for GUIs...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.