Listbox keydown keyup msgbox enter

  1. last week
    Edited last week

    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

    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):

    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

    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 !

  2. Roger C

    Dec 3 Lewis Center, OH

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

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

  4. Emile S

    Dec 3 Europe (France, Strasbourg)

    Add Return True in the line after the MsgBox statement.

  5. Edited last week

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

  6. Emile S

    Dec 3 Europe (France, Strasbourg)
    Edited last week

    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.

    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

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

  7. Emile S

    Dec 3 Europe (France, Strasbourg)

    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…

  8. @Emile S
    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.

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

  10. Emile S

    Dec 3 Answer Europe (France, Strasbourg)

    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):

    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

    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 !

  11. 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

  12. Emile S

    Dec 4 Europe (France, Strasbourg)

    @Julien Cegrave;s 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…

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

  14. Emile S

    Dec 4 Europe (France, Strasbourg)

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

  15. Done in feedback

  16. Emile S

    Dec 4 Europe (France, Strasbourg)

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

  17. Alberto D

    Dec 4 Pre-Release Testers, Xojo Pro

    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

or Sign Up to reply!