Bonk sound while using arrow keys in ListBox

  1. 2 months ago

    Scott C

    Jul 8 Pre-Release Testers, Xojo Pro Vancouver, Canada

    In a Desktop app I'm working on (for both macOS & Windows), I have a Listbox that I've built as a Hierarchical TreeView.

    Naturally, because most TreeViews in applications or operating system file managers allow navigating a TreeView using your keyboard arrow keys, I coded some handling for the folders in the tree structure, where the Left arrow key will "collapse" the currently highlighted folder and the Right arrow key will "expand" the folder. Of course the Up & Down arrows move up and down the items in the tree.

    I have the folders collapsing and expanding properly in my code, but my problem is that when you use the left or right arrow keys in a Listbox on macOS, it makes an alert sound (bonk). The sound does not occur when going up or down in the Listbox, because I think this sort of navigation is a standard feature of the Listbox. But going left or right is not?

    -image-

    The following is my KeyDown event of my Hierarchical Listbox.

    Select Case AscB(Key)
    Case 28
      Self.LastArrowKey = ArrowKey.Left
      If Me.ListIndex >= 0 And Me.RowTag(Me.ListIndex) = ItemType.Folder And Me.Expanded(Me.ListIndex) Then
        Me.Expanded(Me.ListIndex) = False
      End If
    Case 29
      Self.LastArrowKey = ArrowKey.Right
      If Me.ListIndex >= 0 And Me.RowTag(Me.ListIndex) = ItemType.Folder And Not Me.Expanded(Me.ListIndex) Then
        Me.Expanded(Me.ListIndex) = True
      End If
    Case 30
      Self.LastArrowKey = ArrowKey.Up
    Case 31
      Self.LastArrowKey = ArrowKey.Down
    Else
      Self.LastArrowKey = ArrowKey.None
    End Select
    
    Return False

    Note: This sound appears to happen on macOS whether the Listbox is hierarchical or not. And when testing my app in Windows 10, there is no sound made when using the left or right arrow keys.

    If anyone has a tip or suggestion on how to prevent this sound, I would appreciate it. Thanks.

    Xojo Version 2019r1.1
    Mac version: Mojave 10.14.5

    don't return FALSE.... return TRUE

    TRUE is if YOU handled the key.. which you are.
    FALSE is if you want to pass the "key stroke" to other parts of you app, or to the OS itself

    The "bonk" is because you passed it on, but nobody else knew what to do with it

  2. Mark C

    Jul 8 Pre-Release Testers Spain 03170

    Hi Scott,

    Unless you want the OS to do something with the key press then I think you should return TRUE rather than FALSE so the OS never sees the event.

    Also the LR says that the KEYUP event will also fire whatever you return here, so maybe you need to add the KEYUP and just put RETURN TRUE in it to stop the OS seeing anything from that event?

    Mark

  3. Dave S

    Jul 8 Answer San Diego, California USA

    don't return FALSE.... return TRUE

    TRUE is if YOU handled the key.. which you are.
    FALSE is if you want to pass the "key stroke" to other parts of you app, or to the OS itself

    The "bonk" is because you passed it on, but nobody else knew what to do with it

  4. Scott C

    Jul 8 Pre-Release Testers, Xojo Pro Vancouver, Canada
    Edited 2 months ago

    Thank you both @Mark C and @Dave S for the Return True part and your insights. You both answered my question, although it appears I can only mark one of you as "Answered" (sorry).

    I had tried Return True before, but it stopped me from moving up & down the tree, until I realized just now that I needed to add a bit more logic. And because I was stuck on not being to move up & down - I didn't realize that Return True was still allowing the Folders to collapse and expand - without the bonk!. D'oh!

    So in my KeyDown event, when hitting the Up arrow, I added:

    If Me.ListIndex > 0 Then
      Me.ListIndex = Me.ListIndex - 1
    End If

    And when hitting the Down arrow, I added:

    If Me.ListIndex < Me.ListCount - 1 Then
      Me.ListIndex = Me.ListIndex + 1
    End If

    The newly changed event code is the following, and it works without a bonk now.

    Select Case AscB(Key)
    Case 28
      Self.LastArrowKey = ArrowKey.Left
      If Me.ListIndex >= 0 And Me.RowTag(Me.ListIndex) = ItemType.Folder And Me.Expanded(Me.ListIndex) Then
        Me.Expanded(Me.ListIndex) = False
      End If
    Case 29
      Self.LastArrowKey = ArrowKey.Right
      If Me.ListIndex >= 0 And Me.RowTag(Me.ListIndex) = ItemType.Folder And Not Me.Expanded(Me.ListIndex) Then
        Me.Expanded(Me.ListIndex) = True
      End If
    Case 30
      If Me.ListIndex > 0 Then
        Me.ListIndex = Me.ListIndex - 1
      End If
      Self.LastArrowKey = ArrowKey.Up
    Case 31
      If Me.ListIndex < Me.ListCount - 1 Then
        Me.ListIndex = Me.ListIndex + 1
      End If
      Self.LastArrowKey = ArrowKey.Down
    Else
      Self.LastArrowKey = ArrowKey.None
    End Select
    
    Return True

    Thank you again, for expanding my understanding. I've been a db & web developer for too long, so I'm really enjoying this Desktop stuff.

or Sign Up to reply!