LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Handling events in subvis - think this should work but it doesn't.

In part of a large project I need to handle key down events, but elsewhere I need to handle them differently and want to be able to dynamically register and unregister them in each location. I also want to be able to put one of the event handlers in a subvi. To quote the Help system, "Dynamically Registering Events: With dynamic registration, you can handle events in a subVI rather than only in the VI where the events are generated."
 
ViThatHandlesEvents.vi handles key down events. It uses dynamic event registration to demonstrate that it works (though in this simple example it isn't necessary).
ViContainingSubviThatHandlesEvents registers key down events and passes this to KeyDnEventHndlrSubVI. I thought it would work but it doesn't.
0 Kudos
Message 1 of 10
(3,212 Views)
I searched this forum and found explanations that said to pass the static reference to the calling vi down into the subvi, and do the event registration inside the subvi. That's not what I tried first, but I used that approach in the two further attachments here. It also doesn't work.
 
If anybody can tell me what I'm dong wrong, I'd sure appreciate it! Thanks!
0 Kudos
Message 2 of 10
(3,211 Views)
In your subVI the KeyDown event is linked to the subVI. You need to link it to the Dynamic: Controls[] as shown in attachment.

Also, you can use the "ThisVI" reference rather than use a static reference.

Download All
Message 3 of 10
(3,200 Views)
Smercurio, thanks! I tried to do what you suggested and the result is attached (for the version where the looping and registering happens inside the subvi). It runs without errors but does not seem to see any keydown events. Likewise the version where looping and registering happen outside the subvi in the main vi. Any idea why? Did I use your suggestion correctly?
Download All
0 Kudos
Message 4 of 10
(3,175 Views)
You main VI is calling a subVI that's not the one you posted.
Message 5 of 10
(3,159 Views)

Sheesh. No wonder it's hard to get things to work.

Here is another try. I think the main calls the correct sub, and it still doesn't work.

Thanks!

Download All
0 Kudos
Message 6 of 10
(3,146 Views)
It works just fine. Are you focused on a control when you press the key? Remember that you're registering the KeyDown for the controls, and not for the overall parent VI.
Message 7 of 10
(3,143 Views)

Oh! I didn't realize the distinction. Registering KeyDown for the controls is not what I mean to do, then. I want to register for all KeyDown events that occur when the overall parent VI has focus.

Here's a version with Panel > OwningVI > Key Down selected in the nodes that register events. It seems to do what I want. It acts on keystrokes that occur when the cursor isn't even over the parent's window, which is good in my situation. I made these choices by experimenting - is this a good approach, if I want to act on all the keystrokes that occur when the parent VI has focus?

 

Thanks! This distinction is important and it didn't occur to me this might be so, and don't think I've seen it anywhere. It's a big help!

Download All
0 Kudos
Message 8 of 10
(3,133 Views)
Say, I should have said this before, but you have to be pretty good to answer the question somebody should be asking, especially in brief forum postings.
0 Kudos
Message 9 of 10
(3,129 Views)

@cebailey wrote:

Here's a version with Panel > OwningVI > Key Down selected in the nodes that register events. It seems to do what I want. It acts on keystrokes that occur when the cursor isn't even over the parent's window, which is good in my situation. I made these choices by experimenting - is this a good approach, if I want to act on all the keystrokes that occur when the parent VI has focus?


As you noted, this will have the event structure respond to keypresses whenever the parent VI is active. You can simplify the subVI by simply wiring the "VI Refnum" terminal directly to the Register for Events node. Getting the Panel, and then the Owning VI simply gets you back to where you started. Smiley Wink

That's a valid way of doing it, and I'm assuming that this is intended to be a test case. As you noted you have a large project, so I don't know what your overall architecture is, or how you intend to use this technique, so I can't really say whether it's a "good" approach. It's an approach. Smiley Happy


Say, I should have said this before, but you have to be pretty good to answer the question somebody should be asking, especially in brief forum postings.
Thanks for the compliment. It's just a matter of seeing a lot of code and a lot of questions. I'm only a teenage pup compared to the real veterans on this board.
0 Kudos
Message 10 of 10
(3,117 Views)