Android Question [SOLVED] KeyPress Event Unmanageable

Discussion in 'Android Questions' started by Mike1970, May 28, 2019.

  1. Mike1970

    Mike1970 Active Member Licensed User

    Hi, everyone, i'm coding an app, and i wanted to do it graphically catchy, so i designed my own msgBox in the designer, and i display it using the custom B4XDialog and loading the layout i did.

    Now, the problem is that it doesn't aumatic handle the KeyPress Back event, so you have to do it manually.
    THe idea is this:

    If there is a dialog shown, and i click “back” it closes (cancel), but If there aren't dialog shown, and i hit “back” then i want to show a dialog saying “if you click Ok, you will leave the app”.
    The two cases conflict and the second dialog never will disayed (it open and closes instanly)

    Ps.

    I also put an "OK" button in the designer because i didn't know how to customdesign in that way the original buttons of the B4XDialog (But this is a secondary problem) (If know how to achieve that appearance keeping the native button also let me know)

    I attached the example, try to:
    - Click the "Show Dialog" button in main
    - Do "back" and it closes (if you want try the "ok" button as well"
    - Now, when there are no dialog shown, do "back" again (like you want to leave the app)
    - it should display another dialog, but it closes istantly :(
     

    Attached Files:

  2. Erel

    Erel Administrator Staff Member Licensed User

    Follow the example: https://www.b4x.com/android/forum/threads/b4x-input-dialogs-with-xui-views.101197/

    There is no such thing:
    Code:
    Wait For (dialog.Close(xui.DialogResponse_Positive)) Complete (Result As Boolean)
    You are actually calling dialog.Close and causing the dialog to close.

    You should also watch the resumable subs video tutorial. It is very important to understand how resumable subs work.

    Correct code:
    Code:
    Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
       If ((KeyCode = 269Or (KeyCode = KeyCodes.KEYCODE_BACK)) And dialog.Close(xui.DialogResponse_Cancel) Then
           
    Return True
       
    Else
           OpenEXITDialog
           
    Return True
       
    End If
    End Sub

    Sub OpenEXITDialog
       
    Wait For (showDialog("Exit?","Exit ExitExitExitExitExitExitExit","cocacola_logo_PNG5.png")) Complete (Result As Boolean)
       
    If Result Then Activity.Finish
    End Sub

    Sub btnOpenDialog_Click
       showDialog(
    "Title","Text Text Text Text Text Text Text Text Text Text ","cocacola_logo_PNG5.png")
    End Sub

    Sub showDialog(title As String, text As String, img As StringAs ResumableSub
       
    Dim p As B4XView = xui.CreatePanel("")
       p.SetLayoutAnimated(
    00100dip95%x95%y)
       p.LoadLayout(
    "customDialog")
       lblDialog.Text = text
       lblTitleDialog.Text = title
       imgDialog.Bitmap = 
    LoadBitmap(File.DirAssets, img)
      
       dialog.PutAtTop = 
    True 'put the dialog at the top of the screen
       dialog.BorderColor= Colors.Transparent
       dialog.BackgroundColor = 
    Colors.Transparent
       dialog.BlurBackground = 
    True
       pnlDialog.Tag = 
    True
      
       
    Wait For (dialog.ShowCustom(p, "","","")) Complete (Result As Int)
       
    If Result = xui.DialogResponse_Positive Then
           
    Return True
       
    End If
       
    Return False
    End Sub
     
    Johan Hormaza likes this.
  3. Mike1970

    Mike1970 Active Member Licensed User


    It doesn't work on my phone, the "ExitDIalog" appear and disappaer instantly (i tried with another phone but it works.., it's unstable)

    View attachment 80798
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Please upload the updated project.
     
  5. Mike1970

    Mike1970 Active Member Licensed User

    Ok, i have to do it in the example file (it's part of a massive code)
     
  6. Mike1970

    Mike1970 Active Member Licensed User

    Here you are
     

    Attached Files:

  7. Mike1970

    Mike1970 Active Member Licensed User

    Erel, i found the problem.. but i don't know how to solve, it's strange

    My Keycode for the back button it's not 4 (Keycodes.KEYCODE_BACK) but it's 269, so this is the reason why i put this in the IF condition

    Code:
    If ((KeyCode = 269Or (KeyCode = KeyCodes.KEYCODE_BACK)) And dialog.Close(xui.DialogResponse_Cancel) Then
    i tried to remove the keycode.key_back

    Code:
    If (KeyCode = 269And dialog.Close(xui.DialogResponse_Cancel) Then
    i find that keycode 269 is KEYCODE_DPAD_DOWN_LEFT
    But it still such a problem, no o_O?

    Edit:
    On the other phone the EXIT dialog is displayed when i go back, but if go back again to close the dialog it doesn't close anymore
     
    Last edited: May 28, 2019
  8. Erel

    Erel Administrator Staff Member Licensed User

    This code is completely wrong:
    Code:
    Sub btnOkDialog_Click As ResumableSub
       
    Wait For (dialog.Close(xui.DialogResponse_Positive)) Complete (Result As Boolean)
       
    Return True
    End Sub
    See my previous answer.
     
  9. Mike1970

    Mike1970 Active Member Licensed User

    Sorry i forget to delete that from the test project, in the real code i changed that Line in:

    Code:
    Sub btnOkDialog_Click
       dialog.Close(xui.DialogResponse_Positive)
    End Sub
    (Anyway i think that it's not the problem, because i don't use the "ok" when button the problem happends, it happend exclusively with the keyback (that on my phon is a differnte value :/)

    View attachment 80813
    Here you are another Gif explaining what happens, i enabled the ASSISTIVE TOUCH (the round white dot in the right corner) and you can see my finger tap.

    Tapping on the Assistvie touch triggers the Key BACK (real one, with the keycode 4), so the b4xDialog shows up, but then if i tap another time on the Assistive touch to close the dialog it doesn't want to close (p.s. at the end I tap on the "No" button, and it closes, so i don't think is that the problem)

    IMPORTANT
    Sorry for the long posts but it's hard to explain well...



    I """SOLVED"""" the problem, changing the if condition in:
    Code:
    If ((KeyCode = KeyCodes.KEYCODE_BACK)) And dialog.Close(xui.DialogResponse_Cancel) Then
    It works like a charm, if the keycode sended by the phisical button of the phone is 4, but in my case, I VERY DON'T KNOW WHY, the code is 269! o_O

    Well, at this point you can say "put an OR in the if condition to check also the code 269", yes it was the first first first thing i did, but this bring us to the initial problem...

    from my experience i noticed that even if the condition is:
    Code:
    If (KeyCode = 269And dialog.Close(xui.DialogResponse_Cancel)
    and i go back with the assistive touch, (so keycode = 4, NOT 269) the if goes True anyway!!! D: but evidently in a different way, im getting crazy
     
    Last edited: May 29, 2019
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please upload the correct project.
     
  11. Mike1970

    Mike1970 Active Member Licensed User

    Ok, now i change the line and reupload, meanwhile i changed the previous answere, take a look, i discovered something (maybe)
     
    Last edited: May 29, 2019
  12. Mike1970

    Mike1970 Active Member Licensed User

    Ok, i wrote some comments in the keypress event
     

    Attached Files:

  13. Erel

    Erel Administrator Staff Member Licensed User

    I ran your project. Whenever I click on the back key the dialog appears and then on the next click it disappears.
     
    Mike1970 likes this.
  14. Mike1970

    Mike1970 Active Member Licensed User

    Yes, beacuse you phone probably has the Keycode = 4
    But, if you had a meizu mc5 like me the things goes different, my keycode it's not 4 but 269, and i don't know why it triggers badly... i dont' know how to show you this.. it's a bug..


    i supposed something, i think that the keycode 269 trigger also the keycode 4... and then it remain in a buffer and passes two times in the keypress_event.
    There is a way to clear the commands buffer?
     
  15. Mike1970

    Mike1970 Active Member Licensed User

    Solved.
    My phone passes TWO times in a row in the Keypress event, one with Keycode 269 and then one with Keycode 4, so.. this is the correct code:
    (p.s. i Put also keycode 67 because the problem happends with the backspace of the softkeyboard too)


    Code:
    If ((KeyCode == KeyCodes.KEYCODE_BACK) And dialog.Close(xui.DialogResponse_Cancel)) Or (KeyCode == 67Then
            
    Return True
        
    Else If KeyCode == 269 Then
            
    Return False
        
    Else
            OpenEXITDialog
            
    Return True
        
    End If
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice