Swing now allows for an abstract mapping between KeyStroke's and Action's by setting up the InputMap and ActionMap for the JComponent in question. One problem I faced recently goes something like this. Suppose we mapped several KeyStrokes to the same ActionMap key like so...
...how does the Action Object associated with 'myActionKey' determine exactly which KeyStroke triggered it. As obvious from the code, I was toying with a Calculator model. Initially I tried pulling it out of the ActionCommand of the ActionEvent, but that doesn't form a sufficiently unique pattern, for example the two mappings above generate an ActionCommand of "+", but I intended to create a generic Action to handle the contract for all functions in the calculator (=, -, *, /, memory, etc.) with actual functionality abstracted into implementations of a Function interface. I managed a rather bulky workaround, but had to redesign significantly. My original design was to maintain a KeyStroke to Function factory map that could be used to manufacture Function instances corresponding to KeyStrokes generated. So, getting back to the point, do any of u guys have any idea as to how we can derive the KeyStroke the triggered a particular Action?
OK, first of all, I said i wanted only one entry in the ActionMap. Something like...
But, that's OK since you used the same instace for all 4 mappings.
What I have ral reservations about is wiring into the KeyboardFocusManager's KeyEventDispatch'er just to capture the character typed. I could have done that by registering keystrokes using
I didn't want to use this factory method since I want to provide other Functions the scope to register KeyStrokes with modifiers. Besides, my 'bulky' workaround turned out to be better than this and I also managed to find the more appropriate solution to the problem. I'll tell you more about that in the next post. Gotta run now!
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad: