im trying to get the cursor to move to a textfield when a specific key is pressed like when you press tab. im trying to do this instead of just using tab because i want to implement other actions at the same time, how do i do this?
here is the key event for keypressed so far
Fname.addKeyListener(new KeyListener(){
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode() == KeyEvent.VK_TAB){
Sname.setFocusable(true);
Sname.getFocusAccelerator();
if(Sname.hasFocus()){
Sname.setText("");
}
}
//System.out.print(e + "keyRelease: ");
}
any help would be greatly appreciated thank you for your time
i want to implement other actions at the same time
Don't use a KeyListener. Swing was designed to use Key Bindings.
See Key Bindings for more information and a link to the Swing tutorial on the same topic.
Related
I am trying to have a window pop up when key 1 is pressed and a separate window when key 2 is pressed.
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == KeyEvent.VK_1)
{
TicTacToeDriver tic = new TicTacToeDriver();
PointCounter();
}
else if(e.getKeyCode() == KeyEvent.VK_2)
{
HangmanDriver hang = new HangmanDriver();
PointCounter();
}
}
public void keyReleased(KeyEvent e)
{
//do nothing
}
public void keyTyped(KeyEvent e)
{
//do nothing
}
The tic tac toe and hangman games were created by two separate people and the programmers created their own driver.
Solution:
I am assuming that the programmers are familiar with java oriented programming...
Thus, You would just create a new object of one of the games.
Hangman h = new Hangman();
or
Tick h = new Tick();
Tick.start() //depending on their code.
If you are running a Jframe...
you would need to
(insert object name).setVisible(true);
Since you mentioned that they have drivers I assume that they use main methods instead of constructors even though the code you provided creates objects of the classes.
Thus, when the button is clicked or key is pressed.. Just call the main method of the driver class.
hangman.main(null); //this is a terrible way to do it btw.
I also recommend using KeyBindings API instead of keylistener as keybindings does not require focus... which could also be problem.
Another problem would be..
this.addKeyListener(this);
You have to add the keylistener to the component.
But, this is where problems arise as you are using keylistener.. Your JComponent that you added the KeyListener to might not have focus. Thus, the action wont fire till the component has focus and an action is triggered.
Hi all here i am trying to detect the button clicked and based upon that i want to determine some actions
Scenario is i must be able to do some action if a user clicks on up arrow or "A" button in the keyboard.. it is throwing an error as off now and is quite obvious
enter code here
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
switch (e.getKeyCode()) {
case KeyEvent.VK_UP || e.getKeyChar("a"):
break;
default:
break;
}
}
How can i achieve it ?
Thanks in advance
Don't use a KeyListner.
Swing was designed to be used with Key Bindings.
So you create an Action for each KeyStroke you want to handle and then you bind the KeyStroke to the Action. The above tutorial will explain in more detail and there are plenty of example on the forum.
You can also check out Motion Using the Keyboard which compares KeyEvent and KeyBinds with working examples of both.
My mate and I did a project in which we needed this, and the code we used looks like :
public final synchronized void keyPressed(KeyEvent e){
int key = e.getKeyCode();
//stuff
}
This method is called automatically as soon as a key gets pressed.
Right now, when I add a KeyListener to a JTextField, I get an event, then the text updates. But what I need is for the KeyListener to respond after the text is updated. How would I go about doing that? Right now, I am setting a 10-millisecond delay to the response of the KeyListener in another thread, which is sufficient for the text to update and the user to not notice.
Don't use a KeyListener. Swing has newer and better API's than AWT.
Instead you should be adding a DocumentListener to the Document of the JTextfield
A DocumentEvent is generated whenever the Document is updated.
Read the section from the Swing tutorial on How to Write a DocumentListener for more information and examples.
So.... ummmm i know it's kind'a late X)
I kind'a gone around that by using the keyReleased method I noticed that the text gets updated before the key event it should give you something like this
JTextField jtf = new JTextField();
jtf.addKeyaddKeyListener(new KeyListener() {
#Override
public void keyTyped(KeyEvent e) {
// not here
}
#Override
public void keyReleased(KeyEvent e) {
// not here
}
#Override
public void keyPressed(KeyEvent e) {
//do the stuff here
}
});
note that I am not sure why it works but I would think that it has something to do with the typing speed or something, also I am not an expert but i wanted to help (that problem drove me crazy for a couple days) if i am saying any stupid stuff pleas let me know !
Short Version:
How do I determine when a KeyBind key has been released in similar functionality to a KeyListener keyReleased() event?
Long Version:
I am experimenting a bit with making a very simple game, and I was using several KeyListeners to track my keyboard input. However, as I added more complicated features, I began to encounter issues with the keyboard input not receiving proper focus and thus no keyboard input was being received.
Then I read about KeyBinds. While the KeyBinds functionality fixed my focus issue, for my game I was wanting to change a value based off whether a key was pressed or not. I can get the value to change through pressing the key, but I don't know how to detect when the key is released. The KeyListener had a separate KeyPressed and KeyReleased method, but that won't work correctly due to component focusing issues.
Relevant Code:
I don't have a lot of code to share because the input was simply needed to call one of two methods which are set up in another class (and I was testing KeyBinds). But here is the relevant KeyBind code anyway:
Action myAction = new AbstractAction()
{
#Override
public void actionPerformed(ActionEvent e)
{
System.out.println("testing action output");
}
};
actionMap = getActionMap();
inputMap = getInputMap(condition);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "leftArrow");
actionMap.put("leftArrow", myAction);
And here's the code I was using with KeyListener (again, very little since I just call a method)
addKeyListener(new KeyListener()
{
#Override
public void keyPressed(KeyEvent e)
{
racquetClass.keyPressed(e);
}
#Override
public void keyReleased(KeyEvent e)
{
racquetClass.keyReleased(e);
}
#Override
public void keyTyped(KeyEvent e)
{
}
});
Thanks in advance to any and all help that can be provided.
but I don't know how to detect when the key is released.
You need to create a separate Key Binding:
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0, true), "leftArrowReleased");
actionMap.put("leftArrowReleased", myReleasedAction);
I want to be able to receive input from the keyboard by the user but I've added everything I thought would allow my program to do this and still it does not work. What am I doing wrong?
class KeyInput implements KeyListener {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped");
}
}
public GameView() {
this.addKeyListener(new KeyInput());
}
The constructor works fine and KeyInput is an inner class of the GameView object.
When running the game, if I press a key nothing gets printed to the system output.
Am I missing something? Thanks!
KeyListener is fickle mistress, it wants a lot of attention all the time. Basically, it will only raise key events if the component it is registered to has focus AND is focusable.
Generally, you want to avoid using it and use key bindings API instead, How to Use Key Bindings, but this will depend on whether you MUST use pure AWT APIs or not....