How to take input and verify in Java program
Respected Members,
I have 3 questions with slight changes, I would be thankful to you if you can help me solve this problem, I consulted some books and web sites but did not found the solution, the questions are as follows:
1) I want to make a Java console program which when executed will ask the user to enter some input from the key board on the command prompt,now suppose if the user enters Ctrl-F3 than a message would be printed that "you have entered the correct input", but if the user enters any other key or a combination of various key a message will be printed "you did not entered the correct key". Now my question is that how will I take input and after that on what basis will I cehck that a combination of paticular keys(Ctrl-F3) is pressed, I mean that what will be the basis of the IF ceck and what is the thing which I am going to check? If any one cal mail some sample code for this program I would really be thankful, or if you can give some oter important resources?
2) Now my second question is just an extension fo the first question with some slight variations.The thing which I was doing in previous question on a console based program I will now have to do it in GUI based program, I mean that now when I run my program a form(frame a JFrame or Frame) it will ask the user to enter some input from the key baord again if the user presses Ctrl-F3 than a message will be displayed taht you have entered correct input for any other key or key combinations tahnsome other message will be displayed, now here also my question is the same tat how will I take input,than how will I check this input and on what basis will I identify Ctrl-F3 key press.
3) The third question is same as the first two questions,but here my Java program is running like a Windows process I mean that when ever I start my computer this program starts nning at the back end in Windows(Win XP OS),and the program is continiously tracking all the key presses by the users if some user enters Ctrl-F3 a message will be displayed and if the user enters any other key or combination of keys than nothing will happen,how can I achive this and again I am asking that on what basis will I check the suer input,what will be the IF check,and how can I take input.
Please all reply soon because I consulted some books and web sites but did not found the solution,please guide me,I would be thankful to all of you people.
Thanking You,
Taqi Raza.
[2481 byte] By [
taqi10a] at [2007-11-27 11:16:47]

so what you actually want is a gui program which will have a JTextField. if a user types anything on that textfield it should display that the output is not correct may be in JLabel or another TextField. if ctrl+f3 is entered then the output should be displayed that "Your output is correct" am i right?
something like this. search for keypressed and keyreleased events in google. so your code should look something like this i am also new but you can try.
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode(); //gets the user input for the key ex 'A' or 'B' or...
//now you can use your if / else execution
if(keyCode==KeyEvent.VK_CONTROL && keyCode==KeyEvent.VK_F3) //VK_CONTROL means "ctrl" or VK_SHIFT means "shift key"
//display the message in the textField that "YOu are right"
else {
//display the output "You are wrong"
}
}
I am not sure if I have understood your questions right, but I will try to answer nr 2:
Question 2 is easier than Q1. The reason is you can use the KeyEvent interface or the KeyAdapter to check for keyboard events whithin a Component (JFrame in my example).
I have written a small example for you, friend:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class KeyExample2 extends JFrame{
private Infopanel pan = new Infopanel();
private String mess = "";//output
private JTextField tf = new JTextField(10); //input
private MyKeyListener lister = new MyKeyListener();
public static void main(String[] arg){
KeyExample2 fonster = new KeyExample2();
}
public KeyExample2(){
this.addKeyListener(lister);
tf. addKeyListener(lister);
add(tf, "North");
add(pan, "South");
pack();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.requestFocus();
}
//listener
public class MyKeyListener extends KeyAdapter {
public void keyPressed(KeyEvent evt){
// Check for key characters.
int iCode = evt.getKeyCode();
if (iCode == KeyEvent.VK_F3){ //F3 pressed
mess = "You wrote: " +tf.getText();
tf.setText("");
pan.repaint();
}
else if (iCode == KeyEvent.VK_CONTROL ){ //Crl pressed
mess = "Ctrl Key Pressed!";
pan.repaint();
}
}
}//end of key listener
//inner class showing messages
class Infopanel extends JPanel{
public Infopanel(){
setBackground(Color.black);
setForeground(Color.white);
setPreferredSize(new Dimension(600,100));
}
public void paintComponent(Graphics gr){
super.paintComponent(gr);
gr.drawString("Welcome to this simple example program! Write above and press F3!",10,20);
gr.drawString("" +mess,10,40);
}
}//end of inner class
}
This code you should be able to modify and maybe get what you want. You have to register the listener for both your JFrame and your JTextField since only the component on focus will throw an event to the listener.
Is this what you asked for? Good luck!
Respected Friends,
I am really thankful to all of you that you give me your precious time in replying my questions and guiding me, however I still have some confusions............
@lrngjava
In question # 2 the GUI program about which I am talking will be containing only and only a JFrame and no text feilds or any other components, my event would be registered to this JFrame. I saw youur code but can I use the same handler for a JFrame.
Ja_Lava_Java
Dear friend, I am thankful to you from the depth of my heart, this was the thing I was actually searching for, I think that by making some changes to the code I can use it for my purposes, but I have some questions regarding your code , can I bind my key board event only to a simple JFrame having no text feilds , buttons etc.
Also I am having one confusion, you said that only the components on focus will fire this event, can you please clarify what do you mean by "focus" here.... and suppose that some how I set this focus programatically than also will the event be fired?
Thanking You,
Taqi Raza.
> can I bind my key
> board event only to a simple JFrame having no text
> feilds , buttons etc.
(Why not trying? Just make the eventListener make some System.out.println so you could see if something happens)
Yes. The addKeyListener method is inherited from the Component class. So every JFrame (for example) could have a KeyListener, doesn't matter what the JFrame contains.
> can you please clarify what do you mean by "focus" here....
> and suppose that some how I set this focus
> programatically than also will the event be fired?
Ah. I think I see what you are trying - it may work.
A Component having focus means this Component (and no other) is listening to input. It is like you can open two note-pads at a time but only write in one...
I am not an expert in the subject, so I was wrong - my example program works even if I only calling addKeyListener for the TextField - apparently the textField gains focus automaticly.
Yes you can call requestFocus to try to get focus (not always possible). This method also is in the Component class. You can read more about it here:
http://java.sun.com/javase/6/docs/api/java/awt/Component.html#requestFocus()
As I said, im not an expert in this (focus and keyboards)...
Swing related questions should be posted in the Swing forum..
You "should not" be using a KeyListener to handle key events. You "should" be using Key Bindings for this purpose. Read the Swing tutorial on "How to Use Key Bindings":
http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html
And read the rest of the tutorial as well. It has examples of using all Swing components.
@camickr
Thanks for guiding me and pointing out my mistake, I will take care next time that not to post on the wrong forum, but since this discussion has been started on this forum so please allow me to continue it here.
@Ja_Lava_Java
Dear Friend,
One last question, Is it possible that suppose that I set my JFrame to setVisible(false) but still programatically set the focus on the same JFrame, now when my program is executed than definately the frame would not be visible on the screen but since the focus has been forcefully set from within the program than will the event get fired?
Thanking You,
Taqi Raza.
If the frame isn't visible then it doesn't receive any events.