Problems with KeyCode 66 (Enter)

Status
Not open for further replies.

Flosch

Member
Licensed User
Longtime User
I wrote an App using the 'Activity_KeyPress' Event to react on input from a Bluetooth-Keyboard. So if you press x for example it will start a function.
Everything works fine – except if you press the Enter Key (KeyCode 66) !
By pressing the enter key, a button on the screen will be pressed automatically.
You can see how the button is pressed and the function of the button is executed. The function of the EnterKey is ignored completly.

Is there something special with the Enter KeyCode?

Ok I just figured out that the 'Activity_KeyPress' Event isn't triggered if you are pressing the Enter-Key short - it's only triggered, if you are pressing it down a little bit longer (1 sec). By pressing the key longer, the Button on the screen will be pressed AND the function of the Enter Key will be executed.
If you are pressing the Enter-Key and Holding it down the Keyboard will send the Enter Key several times. So it seams the first hit on the Enter-Key is catched by the button the second one will trigger the 'Activity_KeyPress' Event.


Heres the code:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
 
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        t_play.Enabled = False
        If MB.MediaIsPlaying Then MB.MediaStop
    End If
 
    If Layout = "Player" Then
 
        Dim vol As Int 
     
        If KeyCode = KeyCodes.KEYCODE_X Then
              vol = ph.GetVolume(ph.VOLUME_MUSIC) + 1
              ph.SetVolume(ph.VOLUME_MUSIC, vol, True)
        End If
        If KeyCode = KeyCodes.KEYCODE_ENTER Then
              vol = ph.GetVolume(ph.VOLUME_MUSIC) + 1
             ph.SetVolume(ph.VOLUME_MUSIC, vol, True)
        End If
 
        Return True 
 
    End If
 
    Return False
 
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
You have to return false in case of that you handle the keystroke...

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    If keycode = KeyCodes.KEYCODE_BACK Then
        Return True
    Else
        Return False
    End If
End Sub

In your case it should be

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    dim result = false as boolean    
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        t_play.Enabled = False
        If MB.MediaIsPlaying Then MB.MediaStop
        Result = True
    End If
    If Layout = "Player" Then
        Dim vol As Int
  
        If KeyCode = KeyCodes.KEYCODE_X Then
              vol = ph.GetVolume(ph.VOLUME_MUSIC) + 1
              ph.SetVolume(ph.VOLUME_MUSIC, vol, True)
              Result = True
        End If
        If KeyCode = KeyCodes.KEYCODE_ENTER Then
              vol = ph.GetVolume(ph.VOLUME_MUSIC) + 1
            ph.SetVolume(ph.VOLUME_MUSIC, vol, True)
           Result = True
        End If
    End If
    return result
End Sub

Hope it helps!?
 
Upvote 0

Flosch

Member
Licensed User
Longtime User
So if I can't handle it with KeyPress - is there another way to handle it? I need the Enter Key because I will use a Bluetooth Remote Control and it will send Keycode 66 which is the Enter Key.
 
Upvote 0

Flosch

Member
Licensed User
Longtime User
When I disable the button everything will work.
But I need both the button has a different function as the key on the remote conrtol.
Is there a way that buttons doesn't react on Keycodes?
 
Upvote 0
Status
Not open for further replies.
Top