Changing the event of Action Listener and/or doing it out of focus?
Joined: Sep 17, 2011
I'm absolutely new to java [just been doing a tutorials from the new boston on youtube] and I decided to try a few things out.
After a few simple programs that seemed to work fine, I wanted to create a program that would launch a certain method when the user would type a specific keystroke or key. If possible, as I would want to combine this with other programs that don't allow me to focus on the java System.in or on a different window; it would be out of focus (running constantly in the background for example, a bit like key-logger but not with the same functions ).
So I was wondering if:
-instead of using <enter> as an event in an ActionListener / ActionPerformed, it was possible to modify this / use another event and if so, how?
-if ActionListener could be used out of focus/in the background?
-if doing either of these was possible and/or understandable for a newbie like me?
I'm sorry if this is not exactly what is supposed to be posted or if I'm doing this wrong in anyway but I have searched for this and failed to find any answer.
I do have a program ready to be used with these new "functions" added to it but I believe this to be irrelevant as they do not affect the problem at hand.
Thank you very much for your help.
Joined: Oct 13, 2005
There is a great big section in the Java Tutorials about Listeners. I don't agree with all of it, but you can find out about different sorts of Listeners.
If you look at ActionListener, you find that listens for an ActionEvent. That is fired, for example when a button has the focus and is clicked (or space pressed), or the enter key is pressed on a JTextField (I think, not absolutely certain).
I don't know whether you can change that; it would confuse your users if the button responded to, say "ESC" rather than space. Or would you want a button to fire its action when it doesn't have focus? So I think such changes would be undesirable. But you can look for other Listeners, maybe one like this, which can be programmed for different actions for different keys, and even to respond to shift alt(meta) and ctrl.
I think this discussion would sit better on our GUIs forum, so I shall move it.