Trap Return&Enter keys only if Combobox popup is open

I’ve scoured the forums and Googled around, can’t seem to pin this one down.

I have a form where a user can enter data in several fields by tabbing through them, including a combobox. Pressing the Return/Enter key in any field natively triggers an ‘OK’ pushbutton with the Default property set to true to commit changes to the entire data set.

This works fine, except the combobox is tripping me up. When using the keyboard up/down arrow keys, the list of items pops up, but pressing Return/Enter accepts the selection AND triggers the OK button at the same time. What I want to happen is have the Return/Enter key accept the selected list item if the list is visible, but not trigger the OK button. Then, only if if the list is closed and the user presses Return/Enter, allow the OK button to be triggered as usual.

I can trap the Return/Enter keys but it’s all or nothing. I can’t find a Xojo combobox property that gives me a boolean value whether the list is visible or not. That way you could selectively trap the keys if the list was open but allow the keys through otherwise.

I realize if you use the mouse to select a list item, the OK button will not trigger. But keyboard use is important in this app.

I’m on OS X 10.10.3. I’m not sure if this behavior is different on Windows or Linux, could be an OS X- specific issue (but assuming it’s cross-platform).

Any thoughts?

Sounds like the menu or combobox should trap the event and not pass it up the responder chain… maybe a bug report is in order?

One workaround that comes to mind (not a pretty one, but if you just need it to work…)

  • add a timer to the window (mode=0 period=1), and a property - lastIndex as integer. and in the changed event of the combobox if me.listindex<>lastIndex then timer1.mode=1 and lastIndex=me.listindex. And finally, in the action of the defaultButton, if timer1.mode=1 then return

[quote=188538:@Steffan Andrews]I’ve scoured the forums and Googled around, can’t seem to pin this one down.

I have a form where a user can enter data in several fields by tabbing through them, including a combobox. Pressing the Return/Enter key in any field natively triggers an ‘OK’ pushbutton with the Default property set to true to commit changes to the entire data set.

This works fine, except the combobox is tripping me up. When using the keyboard up/down arrow keys, the list of items pops up, but pressing Return/Enter accepts the selection AND triggers the OK button at the same time. What I want to happen is have the Return/Enter key accept the selected list item if the list is visible, but not trigger the OK button. Then, only if if the list is closed and the user presses Return/Enter, allow the OK button to be triggered as usual.

I can trap the Return/Enter keys but it’s all or nothing. I can’t find a Xojo combobox property that gives me a boolean value whether the list is visible or not. That way you could selectively trap the keys if the list was open but allow the keys through otherwise.

I realize if you use the mouse to select a list item, the OK button will not trigger. But keyboard use is important in this app.

I’m on OS X 10.10.3. I’m not sure if this behavior is different on Windows or Linux, could be an OS X- specific issue (but assuming it’s cross-platform).

Any thoughts?[/quote]

Return validates the selection on Windows too. It is the normal behavior for that control.

Here is what I believe does what you want.

  • Add a Selected Boolean property with default false to the window/module/subclass (whichever applies)
  • In the Keydown event :

Function KeyDown(Key As String) As Boolean if key = chr(13) and not Selected then selected = True return true elseIf key = chr(13) and selected then //do nothing so chr(13) works again system.debuglog "return" selected = false end if End Function

Upon return when the menu is displayed, it simply selects. Then, once the menu is hidden, each Return validates.

Maybe that can help you get started.