Listbox keydown keyup msgbox enter

Hi,

I added to the keydown / keyup event of the listbox
keydown event

return true

keyup event

SELECT CASE key
case chrB(3)
  //Enter
  if me.ListIndex > -1 then
    msgbox "ok"
  end if
case chrB(13)
  //Enter
  if me.ListIndex > -1 then
    msgbox "ok"
  end if
end select

but If I press “Enter” on the msgbox, the code is executed again.

Is it a bug or do I need to add something to prevent that?

I could set listindex to -1 before showing the msgbox but it’s a bit ugly

Julien

You have your key events backwards. Put Return True in the keydown event and your code in the key up event

I mixed things writing the post but I have the return True in the keydown and the code in the keyup event already.

Add Return True in the line after the MsgBox statement.

@Emile Schwarz I can’t return True in the KeyUp event as they are no return type defined.

Sorry. I do not checked before I sent my previous answer.

So I reformulate; the code below do what you ask (?).

NO !: I do not have the MsgBox at all with 2018r3 (and 2015r1) / El Capitan.

[code]Sub KeyUp(Key As String)
Select Case Key

case ChrB(3)
// Enter
If Me.ListIndex > -1 Then
MsgBox “OK”
Return
End If

Case ChrB(13)
// Return
If Me.ListIndex > -1 Then
MsgBox “OK”
Return
End If

End Select
End Sub[/code]

What OS / Xojo are you using ?
Also, how are you able to press a key in a Listbox ?

I finally get what you wrote because I have a default PushButton in the same window.

When I get that, usually I remove the Default Boolean for the button.

Usually, I called that a bug, but nowadays…

@Emile Schwarz
I am on MACOS Mojave runing xojo2018r3

I added navigation with keys (left/right) in the listbox to expand rows and to edit selected item using spacebar or enter.

I will try to remove the default boolean for the button.

I do not have any default button on the window containing the listbox with the event.

Dorry for the delay.

What are these Select Case for ?

I understand the test part, but after your descripton, I do not understand. I have some things to do (right now), and I will make other testings in a brand new project (instead of a code addition to an existing project).

ExpandRow: I would use these keys too (Enter AND Return).

You do not have a PushButton that have the Default behavior ? I do not understand… how that can come.

What the Return True in MouseDown is meant to do ?

I used this code (and nothing in KeyDown):

[code]Sub KeyUp(Key As String)
Select Case Key

case ChrB(3)
// Enter
If Me.ListIndex > -1 Then
MsgBox “OK”
Return
End If

Case ChrB(13)
// Return
If Me.ListIndex > -1 Then
MsgBox “OK”
Return
End If

Case ChrB(28)
Me.Expanded(Me.ListIndex) = False
Return

Case ChrB(29)
Me.Expanded(Me.ListIndex) = True
Return

End Select
End Sub[/code]

The window have only the Listbox, and the two arrow keys works fine.

I confirm what you said: pressing Return close the MsgBox, but the Listbox also trap that Return and display another MsgBox as nauseam.

This one follows the LR instructions (Listbox.KeyUp):

case ChrB(3) // Enter If Me.ListIndex > -1 Then MsgBox "OK" ClearFocus End If

And I do not get a second MsgBox !

Thank you, adding clearfocus solved the issue

keydown return true was to handle the keyup, thought that if we return false in the keydown event, the keyup never happens

That is the .MouseDown syndrom.

Commenting that line (and run) proved that I was wrong and leads me to read more carefully the documentation to discover this specific use of ClearFocus.

You may have to set the focus back in CellLostFocus…

Yeah I will do that, but I think it’s more like a workaround. Can it be considered as a bug?

Xojo have so many things to do… You can make a bug report with Feedback.

Done in feedback

If this is a bug, they have a reminder; else you will know later…

I was testing this and found that this doesn’t work:

case ChrB(3) // Enter If Me.ListIndex > -1 Then ClearFocus MsgBox "OK" End If
it looks like after MsgBox is executed (maybe because is another window), when it is removed and the previous window re-activated, then the focus is activated again.

Using ClearFocus but getting the focus back:

case ChrB(3) // Enter If Me.ListIndex > -1 Then MsgBox "OK" ClearFocus Xojo.Core.Timer.CallLater (100, AddressOf Me.setfocus) End If

Tested on macOS 10.12.6