• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing the event of Action Listener and/or doing it out of focus?  RSS feed

 
Pierre Lataillade
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone!
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.
Ryujin.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

And welcome to the Ranch
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Java application can't receive input events (jey / mouse inputs) when it isn't active. So you can forget about using Java to write a program that's
a bit like key-logger


To react to keystrokes in a foreground, active Java GUI, learn How to Use Key Bindings.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!