Showing results for 
Search instead for 
Did you mean: 

determining which control has focus

Is there an easy way to find the name of the control that currently has key focus?  I've been struggling with this for a few hours now and I can't seem to find a straight forward way to do it.  Thanks for any suggestions in advance. 
0 Kudos
Message 1 of 7

Search for darrens nugget on TRefTraverse (or was it on the blog: labviewartisan). This allows you to get all controls of an FP. Then you need to check the KeyFocus property (you will need to poll), and with the same refrence obtain the lable.



0 Kudos
Message 2 of 7

I figured out a way to get the control name by using a VI Server ref call and accessing the FindCtrlWithKeyFocus method, but now I'm having some other issues. 


So I guess I need to go into what I am trying to accomplish.  I want to be able to execute a command (via a FP run button or a User Menu event), and base my execution logic on the contol that previously had focus. 


So my first problem is that as soon as I hit run or choose a menu selection the control I care about no longer has focus.  So I need a Find "Previous" Control With Focus, which doesn't seem to be available.  I can think of some ways to deal with this (like polling the current focus and keeping a rolling index), but none of them are easy and not really all that elegant. 


The second and probably more importnant issue I'm seeing is if the control is inside an array, the FindCtrlWithKeyFocus method fails to locate a valid control.  I want this to return the name of the array that had focus, but this doesn't seem to work.  


Any ideas?







Message 3 of 7

FindCtrlWithKeyFocus method,

Good work!Smiley Happy



 I want to be able to execute a command (via a FP run button or a User Menu event), and base my execution logic on the contol that previously had focus. 


Bad ideaSmiley Sad

Is it possible to base your command decision on a Vaule Change event?  Or on a Mouse Leave/up event?



Perhaps tracking the latest Mouse leave/up event may do the job (assuming your FP is maximized)


BUT, IMHO (as they say) I would look for a "state" to choose my command to run---- and by "state" I mean a distinguishable vector of values (not all values need be scalar) that are on Wires and execute the command defined by the vector.  (I love Case structures I guess)


This sounds a bit vague- even to me.  Can you provide more details at WHAT you want to elect to do and when the trigger point is?


I think we can offer solutions --- BUT you might wish to re-think your approach


"Should be" isn't "Is" -Jay
0 Kudos
Message 4 of 7

Is there a way to find which control had key focus lastly?

0 Kudos
Message 5 of 7

It should be possible, however in a multitasking OS/UI  the fokus will get lost or changed by accident...

So I would recommend a event stucture for keyboard/mouse events and keep that information in a (shifted) wire 😉

Greetings from Germany

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'

0 Kudos
Message 6 of 7

See this:

Also,  Here is everything that I know of that deals directly with focus:


"If you weren't supposed to push it, it wouldn't be a button."
Message 7 of 7