LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Key Down event doesn't fire while control key is held down

Solved!
Go to solution

I'm trying to write an actor VI that will handle a few keyboard shortcuts for a strong control. For example, Ctrl-A should select all. I was hoping an event structure would allow me to detect that keyboard combination. But Key Down(?) doesn't work: Pressing and holding control fires off the event for Key Down(?), but then pressing any other keyboard character fails to fire the same event.

 

I can make it work using the Key Up event, but this doesn't select the text until after the keys are up, whereas the normal select-all behavior in most applications will do the text selection while the keys are still pressed.

 

Turns out that the need to detect control-key on a key down event exists elsewhere. For example, I have string filtering on arrow keys (do something when an arrow key is pressed while in a strong control), but this prevents the default ctrl-arrow key behavior. I need to detect whether the control key is pressed to keep the expected text-selection behaviors.

 

 

Any ideas?

 

_____________
Creator of the BundleMagic plugin for LabVIEW!
Message 1 of 18
(249 Views)

You can use the customized runtime menu.  It works even the menu bar is not visible.

 

George Zou
Message 2 of 18
(242 Views)

I note that the key-down event won't fire if a control-key shortcut in the menus is in use. However, in my running VI, there are no menus shown. If I make a sample VI, ctrl-a is grayed out (it's an edit-only feature of the LV environment). So seems to me it should work. 

 

Also, if this is normal text-input behavior, it should work out of the box!

_____________
Creator of the BundleMagic plugin for LabVIEW!
0 Kudos
Message 3 of 18
(242 Views)

The event data for the key down events has a "PlatMods" section that includes ctrl, so you'd look for 'A' with the ctl mod.

0 Kudos
Message 4 of 18
(236 Views)
Solution
Accepted by littlesphaeroid

ctrl-a is grayed out

You have to create a customized menu.  See attached VI.

 

George Zou
Message 5 of 18
(227 Views)

Thanks @avogadro5 but as I mentioned, this does not work on the Key Down event. Have you tried it?

 

@zou Thank you for the suggestion, that looks like it will work, though I'll need to look into creating this menu item programatically.

 
_____________
Creator of the BundleMagic plugin for LabVIEW!
0 Kudos
Message 6 of 18
(200 Views)

I'm not sure what is not working for you. On my computer I can run the attached VI and it will stop when I press ctl+a. Is it possible that the event handling for the ctl down is blocking when the a is pressed?

avogadro5_0-1733196018174.png

 

0 Kudos
Message 7 of 18
(194 Views)

> though I'll need to look into creating this menu item programmatically.

You can find this in LabVIEW Help and Example Finder:

image.pngimage.png

 

George Zou
0 Kudos
Message 8 of 18
(176 Views)
Solution
Accepted by littlesphaeroid

I came up with two solutions and attach my code in LV2019 format.

 

Custom User Menu (described above)

 

This solution is not entirely satisfactory, because it creates a visible user menus. I created a reentrant VI that can take one or more string references and which can be called multiple times. You have to check each registered reference for whether it is active when the keyboard shortcut fires.

 

Key Down? Event

 

This method requires first deleting any menu item that uses the ctrl-a shortcut (which in LabVIEW, even at runtime, includes the Select All editing menu item). This is restored automatically when the VI stops running. I prefer this method as it is more intuitive and doesn't create a new menu item. I went ahead and made this asynchronous call VI a malleable VI, so it can accept a single control ref or an array of refs.

 

The attached code has two test VIs demonstrating use of each method.

 

 

One thing I like about this technique for adding functionality to controls is that you can spawn these processes, and they stop themselves once the calling application stops, by polling if the control's owning VI is running. I'll  be curious to hear what others think.

_____________
Creator of the BundleMagic plugin for LabVIEW!
0 Kudos
Message 9 of 18
(135 Views)

Zou's example VI does work work (thank you!), but fails if you register for an event for a specific control, which is what I need to do. 

 

The documentation says:

 

Generated on a control that has keyboard focus. If a key press matches a keyboard shortcut in the VI menu, such as Ctrl-C or Ctrl-V, LabVIEW does not generate a Key Down event, regardless of whether the menu item is enabled.

 

I don't see how this behavior is consistent with the application Key Down event Zou shows, but here we are.

_____________
Creator of the BundleMagic plugin for LabVIEW!
0 Kudos
Message 10 of 18
(126 Views)