0

I am trying to use a KeyListener in my program. I have added this class to my main class:

class CustomKeyListener implements KeyListener{
    @Override  
    public void keyTyped(KeyEvent k) {}
    @Override
    public void keyPressed(KeyEvent k) {}
    @Override
    public void keyReleased(KeyEvent k) {}    
}

However I am not able to add the addKeyListener(new CustomKeyListener()) method anywhere in my main class. What am I missing?

Based on my previous question I understand it will only work with a graphic interface.Should I just add some arbitrary form and it will work?

Can anyone shed some light on this problem?

Community
  • 1
  • 1
  • 1
    What are you trying to add the KeyListener to? – apicellaj Mar 23 '16 at 08:53
  • 1
    “some arbitraty form” doesn’t sound right to me. If you want to go ahead this way, you need to build the graphical user interface you want your user to see and use. – Ole V.V. Mar 23 '16 at 08:55
  • @OleV.V. i am the only user. all i want is to add this keylistener so i would be able to catch any key pressing operation in order to stop some looping process. thats it. –  Mar 23 '16 at 09:00
  • @apicellaj i am trying to do `this.addKeyListener(new CustomKeyListener());` trying to add it to my main class i guess. –  Mar 23 '16 at 09:01
  • What class are trying to add the key listener to? – MadProgrammer Mar 23 '16 at 09:02
  • i am looking for a simple solution, but haven't been able to implement it yet. i am open to suggestions. –  Mar 23 '16 at 09:02
  • @MadProgrammer trying to add it to my main class. there is a some class, which has a main method. inside that method i am trying to add the keylistener using `this.addKeyListener(new CustomKeyListener());`. the `CustomKeyListener` is declared at the bottom of the main clas as stated in the question. –  Mar 23 '16 at 09:03
  • Which is a ... what? – MadProgrammer Mar 23 '16 at 09:04
  • @MadProgrammer i dont understand what are you asking me. what is the name of class? –  Mar 23 '16 at 09:17
  • 1
    If you're adding adding a KeyListener to "this" then the class must extend a JComponent (JFrame, JDialog, etc) or it's not going to work. – apicellaj Mar 23 '16 at 09:20
  • I think @MadProgrammer asks what type of class you're trying to add the Key Listener to. Does it extend JFrame, JWindow, Container...? – S.L. Barth is on codidact.com Mar 23 '16 at 09:23
  • As far as I can see, for this to work, your keypress needs to go into a window (other than your console window) represented in Java as a JFrame or your own subclass of JFrame. From there, Java will under certain circumstances pass it on to key listener/s attached to the JFrame and/or components inside the JFrame. – Ole V.V. Mar 23 '16 at 09:23
  • I have added `extends JComponent` to the main class declaration but still it does not work. does not show any options of `addKeyListener()` after the `this.` –  Mar 23 '16 at 09:28
  • the error that it show me is : `Cannot use this in a static context` –  Mar 23 '16 at 09:31
  • Ah! You're trying to do it in a static method. In a static method, the keyword `this` won't work. `this` refers to the current instance; a static method does not have an instance. – S.L. Barth is on codidact.com Mar 23 '16 at 09:32
  • ok. in order to solve this problem i should create some function that would add the keylistener? instead of the main function? –  Mar 23 '16 at 09:36
  • Yes, but you should understand what `static methods` and `instance methods` are... or you're just gonig to get more headaches. Give me a moment, I'll update the answer. – S.L. Barth is on codidact.com Mar 23 '16 at 09:37
  • ok, thank you @S.L.Barth. –  Mar 23 '16 at 09:39
  • You're welcome! I added a link to the official Java documentation in my answer. I think it will help you. Good luck and happy coding! – S.L. Barth is on codidact.com Mar 23 '16 at 09:49
  • 2
    I suspect you’re doing wrong in just trying to get past your compilation problem. You need to understand what you’re developing and what user interface element (as visible on your screen) you are adding your key listener to. – Ole V.V. Mar 23 '16 at 10:12

2 Answers2

0

Does your main class implement/extend a class that actually support the method iyou are trying to use?

Some context as to what your main-class is and what you want to achieve. Is it perhaps a scanner you need instead? If so then have a look over here to learn how to use it. http://www.tutorialspoint.com/java/util/java_util_scanner.htm

Also this might probably help as well: Java using scanner enter key pressed

Community
  • 1
  • 1
trappski
  • 1,066
  • 10
  • 22
  • i have already tried the `Scanner` method, did not worked out for me. should i add to my main class the `Implements KeyListener`as well? –  Mar 23 '16 at 09:20
0

Your class should have the addKeyListener method. This is the case if your class is derived from Component or JComponent. So you should derive the class from either of these classes, like this:

class MyClass extends JComponent

Note that the inheritance does not have to be direct; if your class is derived from (for example) a JFrame, it indirectly inherits the addKeyListener method, because JFrame is itself derived from Component.

Also, as is pointed out in the answers to this question, your class should be focusable for the Key Listener to work.

Update

You wrote in the comments that you were trying to use

this.addKeyListener( new CustomKeyListener( ) );

and got the error

Cannot use this in a static context

This happens because you called were making the call from a static method (the main method in this case).
this refers to the current instance of the class. In a static method, there is no instance - this is practically the definition of a static method.

What you should do, is create an instance:

MyClass newInstance = new MyClass( );

Now, you can add the key listener to that instance:

newInstance.addKeyListener( new CustomKeyListener( ) );

You could also do this in an instance method (any method that does not have the keyword static), and call that method from your instance.

The official Java Tutorials have some more explanation on this subject.

Community
  • 1
  • 1
  • almost. `No enclosing instance of type MyClass is accessible. Must qualify the allocation with an enclosing instance of type MyClass (e.g. x.new A() where x is an instance of MyClass ).` did everything you said, now i am getting this error. MyClass is my main class –  Mar 23 '16 at 09:48
  • For now, it's probably easiest to have `CustomKeyListener` outside the main class. Once you're a little more experienced, learn about inner classes. Or do as the error message suggests: `addKeyListener( MyClass.new CustomKeyListener( ) )`. – S.L. Barth is on codidact.com Mar 23 '16 at 09:51