1

I have looked at a bunch of answers to a previous question I had (it was answered), but something that I kept seeing were methods like:

public void keyPressed(KeyEvent e)

but none ever showed where these methods were used, so I never figured out what to do with the argument.

Example:

public void keyPressed(KeyEvent e, Robot r) {
    int key = e.getKeyCode();
    if (key == KeyEvent.VK_W) {
        r.keyPress(KeyEvent.VK_R);
        r.mousePress(InputEvent.BUTTON1_MASK);
        try { Thread.sleep(100); } catch (Exception s) {}
        r.mouseRelease(InputEvent.BUTTON1_MASK);
    }
}

public static void autoCliker() throws AWTException, InterruptedException
{
    Robot r = new Robot();
    while(KeyPressed(not sure what to do here, r)//this is what my question is about
{
        Thread.sleep(10);
        r.keyPress(KeyEvent.VK_R);
        r.mousePress(InputEvent.BUTTON1_MASK);
        try { Thread.sleep(100); } catch (Exception e) {}
        r.mouseRelease(InputEvent.BUTTON1_MASK);

    }
}

It's more about how to use an Event in an argument within a method than about the KeyEvent, I am just using one of my programs as an example.

  • 1
    You deleted your other question to quickly. Newbies are welcome to ask questions here; but as EJP tried to explain to you on your other question that got downvoted: form matters. When you ask about a coding problem, be sure to have a **real** [mcve] in there. Don't waste your time expressing that you are a newbie with no reputation. That doesn't matter. This is a Q&A community - the only thing that matters is the quality of your input. And the question you deleted this morning was simply not in the category of questions that fully match the scope of this community. As said, that problem could – GhostCat Apr 10 '17 at 06:17
  • 1
    have had many different reasons, many of them **not** programming problems. In that sense: A) you can ask on forums such as quora.com - but make no mistake; you probably dont get downvotes there that easily, but when you put up a "bad" question, you will simply not receive answers. B) simply study how other (more successful) newbies write their questions. Study what the [help] has to tell you. All of that. Repeatedly. And, yes, I acknowledge that: people are sometimes picky here. One bad newbie question gets downvoted and closed and deleted within 10 minutes; others get upvotes and ... – GhostCat Apr 10 '17 at 06:21
  • 1
    keep attracting c..p answers for days or weeks. And yes, the "java" tag is one where people are more picky. But well, that is how things are over here. And as said: try to be focused when writing questions. Try to think how a total stranger would react to your input ... and understand: people don't think you are stupid when they downvote. This is **not** personal. It means: you should improve your input to us! – GhostCat Apr 10 '17 at 06:23
  • 1
    @GhostCat I know I still have a lot to learn, but if my question is not clear, downvoting it won't help anyone. I just don't understand why no one just gets straight to the point and asks me for specifics that I might not have known about. Just asking me about some things I missed might even point me in the direction that I need in order for me to fix my error. I also know now that it's good to add an new block for the fixed code instead of having people search through the comments for answers. (at least that's one conclusion I have come to) – HarrietTubmanFan69 Apr 24 '17 at 19:34
  • 1
    The point is: there are zillions of really bad newbie questions each day. Downvotes are an important mean to help dealing with that. They express quickly that a large portion of the readers reject that input. You see, there is a lot of excellent documentation for newbies in the help center. We can't spend an hour on each newbie explaining to him repeatedly "please study those help documents". I know it is hard for newbies, but down+close vote to then move on is a very accepted policy here. But I am glad you are willing to learn. You would be surprised to see how many people are – GhostCat Apr 24 '17 at 20:47
  • 1
    absolutely unwilling to accept how things work here. They WANT a place where they can drop whatever crap input they have and others please do all the work quickly. So: I am glad you are still here. May the future have good questions and answers for you. – GhostCat Apr 24 '17 at 20:49
  • 1
    Thank you for your support, and I already knew when I came here that I would need to change in some way, and you helped me with that a lot. Also, why would those kinds of people be here if they are unwilling to learn new things. – HarrietTubmanFan69 Apr 24 '17 at 22:44
  • 1
    Yeah, that is one good question. People come here and trust us to help with their programming problems, but when they receive input on the policies of the community they totally think that they know better. But just for the record: it also happens that the feedback given is too harsh. I am guilty of that, too. Learning never ends... – GhostCat Apr 25 '17 at 02:28
  • 2
    This is just a tip that works for me when I get too harsh. All I do is remember that we were all once kids who didn't know anything and didn't worry about anything, and I just view everyone including myself as a child and it opens my mind and allows me to reply calmly without being too harsh. It is weird, but it works for me. – HarrietTubmanFan69 Apr 25 '17 at 03:43

1 Answers1

1

This method, along with others, shows up when your class implements KeyListener.

public class Test implements KeyListener {

This method senses key's pressed on the keyboard. If you want to detect a certain key like w. Do this:

if(e.getKeyCode.equals(KeyEvent.VK_W);

Hope this helps.

  • This does help, but it still isn't exactly what I was looking for. – HarrietTubmanFan69 Apr 06 '17 at 05:33
  • I will update the question and give an example of what I mean. – HarrietTubmanFan69 Apr 06 '17 at 05:34
  • Where you asked the question, all you need is to put the parameters, not sure what the problem in that is. If you don't need to make your own keyPressed method(overcomplicates), just implement KeyListener and use the default one and add your code in that method. That way you don't need to call the method as it is automatically called. – Krishnanshu Gupta Apr 06 '17 at 23:39
  • Sorry for replying so late, but I was a bit unclear with my question before. I wasn't trying to create a new KeyPressed method. To my understanding, I thought you could use it as a boolean expression in a while loop to see if a key was being pressed. In pseudocode: `while(KeyPressed(W key)){do something}` – HarrietTubmanFan69 Apr 24 '17 at 19:38
  • No, but if you could, that would make things much easier wouldn't it. – Krishnanshu Gupta Apr 28 '17 at 03:06
  • I don't know why I never got around to asking this, but why does it say that I need my class to be abstract in order to implement KeyListener? (I don't know what an abstract class really is) – HarrietTubmanFan69 Apr 29 '17 at 17:17
  • An abstract class is basically just an idea. All the interfaces you implement are abstract classes, i.e.: KeyListener, MouseListener, MouseMotionListener, KeyAdapter, etc. An abstract class is basically just an idea for the user to implement and therefore, use to their benefit. Could you show me your whole code in an answer, so I can help you better??? – Krishnanshu Gupta Apr 30 '17 at 00:47