• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79973
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanna see my flashlight? How about this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic