I’ve got a button that sends a message to a handler on the root container. The results of the message handler is a query that searches for a part and sets a custom property, to which a table is bound.
This works fine.
I used this same message for pressing the Enter key. But, I do not get any query results. So, I bound a label to a custom property, and set this property via the root message handler, and it works both with the button click and Enter key events.
What is the key event handler attached to? Are you sure it’s firing? I would put a system.perspective.print() in there to verify it. If it’s firing but the message handler isn’t hearing the message, then perhaps it’s a scope issue. You aren’t providing one, and the default is page.
So the key event is attached to the button? That will only work when the button has focus. That will be true of whatever you attach it to. I would say to invoke self.focus() on the button in an onStartup event handler, but that is not guaranteed to work, since other parts of the view may steal focus when they get rendered. Perspective doesn’t have an onLoad handler that would fire after everything in the view is done rendering, something I have requested:
Ok, I tested a few more things, and it appears that there are two (2) components out of 8 or 9 that do not work with the query when I press the Enter key.
The two components are numeric text fields. I seem to recall asking this question a long time ago, now that I have come back to this project and have narrowed it down again...
No, the key event is held on a flex container, two levels up from the button.
Edit: So the issue is the numeric text fields. I will go back and look for my old thread on this and see what the end result was.
If the numeric text fields have focus when you press Enter, then they could be preventing the flex that has the key event handler from hearing it.
You could add the same handler to the inputs themselves, or you might want to try a Session Event Keystroke handler. I’ve never used it, but I would guess it isn’t tied to the focus of any particular page element.
Ah, I think I see what is happening. After entering a value, I must move the focus to another component first, then press Enter.
It will not work if I click somewhere in the container, i.e. not on an interactive component, but some other component must get the focus for the numeric field to, I assume, accept the input value and "transmit" to the bound property...
Right. So, moving the focus to another component before sending the message is what needs to happen. I put a short sleep statement in, but I seem to remember someone more knowledgeable stating that is not good practice. All I need is a short delay so the input value gets captured before running the rest of the script.
Edit: Never mind, don't include the parens on the function!
Edit 2: No dice on the query running. I added a line to change the background color of a button just below the message call, and I got nothing to happen.
So, this code is running on a numeric text field, I don't think I made that clear. The button gets the focus, good. IF I move the backgound color line to the top of the function, the color will change, but not in it's current position in the code.
I changed the message handler to display a message, as I did earlier, and it is not firing. For some reason, the sendMessage is not working in this context.