aspose file tools
The moose likes Swing / AWT / SWT and the fly likes Changing the event of Action Listener and/or doing it out of focus? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Changing the event of Action Listener and/or doing it out of focus?" Watch "Changing the event of Action Listener and/or doing it out of focus?" New topic
Author

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

Pierre Lataillade
Greenhorn

Joined: Sep 17, 2011
Posts: 1
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

Joined: Oct 13, 2005
Posts: 32712
    
    4
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

Joined: May 03, 2008
Posts: 4167
    
    3

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Changing the event of Action Listener and/or doing it out of focus?
 
Similar Threads
Replacing GUI with new GUI
Programming Portfolio guidance and suggestions
Communication Between Java Programs
JTextField with ActionListeners?
Key Event listener question.