Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyPress KeyListener etc.

 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK it is time for me to get semi-serious with this now and actually do something.

If you will recall a few months ago I made an inquiry about getting hot keys to activate icons
on a page, instead of clicking on the icons.

I was given general help on investigating the wonderful world of keyTyped, keyPressed, keyReleased, key listeners etc. etc. etc.
Given that I don't have the foggiest knowledge of Java that wasn't really much help for me.

I just want to open up code in Netbeans (doing that now) and add hot keys to events that are normally activated by mouse clicks.
How difficult can that be?

To give you an example of how skilled I am, I opened the code, navigated to an interesting spot and noticed two
keys "=" and " " were being used to activate code. I wanted to see what would happen if I replaced the " " with another key.
I substituted "q" for the " ", rebuilt the code and tried it out.

Low and behold, the "q" key now worked in place of the " " key!

Well I would like to do a lot more than that. I would like to be able to assign a key to any event that I choose.

Here is one that I was just looking at:
com.openbravo.pos.panels
JPanelCloseMoney.java
private void m_jCloseCashActionPerformed (java.awt.event.ActionEvent evt) {

So I know a specific event is listed above and what follows that.
Can someone please give me guidance on how to add a key to trigger this particular event?

If I can be shown how to do one event, I am sure I can figure out how to do many more.

Thank you.



 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't mess around with keypresses and that sort of thing, not unless you really have to. And you probably don't. Swing has features which mean you don't have to deal with actual physical keyboards. Have a look at this tutorial: How to Use Actions. I've used Actions in my code to do things like when I press Control-D in a text field it inserts the ° symbol. Which of course isn't on my keyboard. So I don't have to fuss about with operating-system components to get that character.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Henstridge wrote:To give you an example of how skilled I am, I opened the code, navigated to an interesting spot and noticed two
keys "=" and " " were being used to activate code. I wanted to see what would happen if I replaced the " " with another key.
I substituted "q" for the " ", rebuilt the code and tried it out.

Low and behold, the "q" key now worked in place of the " " key!


Don't put yourself down too much; that's basically what I do when I'm trying to learn some Java feature which I haven't used before. I grab some code from a tutorial and start fiddling with it until it does what I want. Granted you didn't do much fiddling, but you know how they say every journey begins with a single step, right?
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another tutorial which you should read too: Key Bindings. If I don't post this one then Rob is going to come around and post it anyway.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul I'll have a look at those links you posted and see what I can learn from them.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK let me try it this way as I didn't seem to have much luck with the above suggestions.

In many sections of the code I see :



where it is allowing the user to press certain keys to activate code that follows.
In the above case the keys are the Spacebar and the Equals sign.
This is actually the section of code I mentioned earlier where I was able to substitute
a key for the Spacebar.

No KeyPress or KeyListeners appear to be used. Something is allowing just the press of the designated keys to
activate code.

(cTrans == is the common code used throughout.
But there must be more to it than that.

So what is going on here exactly that allows these keys to activate code?

TIA
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Henstridge wrote:So what is going on here exactly that allows these keys to activate code?


In that code fragment you posted? Nothing is going on there that allows any keys to activate code. I imagine we're looking at some code which is activated somehow, but if you want us to explain the controller code which set up the activation, well, what you posted isn't that code.

If I were you I would persevere with producing code which doesn't use key listeners. Actions are a much more flexible architecture.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
David Henstridge wrote:So what is going on here exactly that allows these keys to activate code?


In that code fragment you posted? Nothing is going on there that allows any keys to activate code. I imagine we're looking at some code which is activated somehow, but if you want us to explain the controller code which set up the activation, well, what you posted isn't that code.

If I were you I would persevere with producing code which doesn't use key listeners. Actions are a much more flexible architecture.


OK digging around a bit more I believe I found the origin to all the following cTrans statements in the code.
A private void statement:



Imports at the very top of the code that may be pertinent?
java.awt.event.ActionEvent;
javax.swing.Action;

It seems that actions are being used in this code rather than listeners.
Although one author of the code declared in the middle somewhere:

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Henstridge wrote:


Okay... so now look at what's calling that method.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay... so now look at what's calling that method.


Did a bit of code hunting/searching.

This is what I found:



So if the method is being called by these, will it help me assign my own hot keys to trigger events?
Not just yet.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those are just declarations. They tell you nothing about how the methods can be used or what they are for.

You are still looking for code which assigns values to those characters and calls those methods.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to be such a PITA with this.

There are two situations I am trying to work with.
In one, there is the code I quoted above, with specific keys coded to trigger events.
I am trying to figure out why they work and how I can copy the method to many different areas
of the program.

In another, there are no keys coded to trigger an event, but I would like to add one.
In this situation, a mouse is clicked to open/close a menu.

The relevant code follows:




What I did with diligent research, was find a method that could possibly work.
I will copy it below.
I was absolutely thrilled when I didn't see any red dots on the left side and the code compiled!



This code is indented in to the right of the above code. I'm not sure why Netbeans put it there, but it did.

The long and short of it, despite compiling OK, the F12 key is not opening/closing the menu as I hoped it would.





 
Rob Camick
Ranch Hand
Posts: 2703
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly, this posting is going nowhere. Every now and then you post a few random lines of code. We have no context of how the code is used.

When you ask a question post a proper SSCCE that demonstrates the question.

So in your case you want to do something when a key is pressed. So create a JFrame. Add the key binding to the frame. The whole program will probable be about 20 lines of code.

Then test the code it will either work or it won't.

If it doesn't work then you have a small program to post and we can see exactly what you are doing.

If a question can't be solved in 2 or 3 replies, then the question just doesn't make sense.

If you still need more help then create your SSCCE and start a new posting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!