Android Question [SOLVED] KeyPress Event Unmanageable

Mike1970

Well-Known Member
Licensed User
Longtime 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 :(
 

Attachments

  • keypress.zip
    21.4 KB · Views: 314

Erel

B4X founder
Staff member
Licensed User
Longtime User
Follow the example: https://www.b4x.com/android/forum/threads/b4x-input-dialogs-with-xui-views.101197/

There is no such thing:
B4X:
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:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If ((KeyCode = 269) Or (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 String) As ResumableSub
   Dim p As B4XView = xui.CreatePanel("")
   p.SetLayoutAnimated(0, 0, 100dip, 95%x, 95%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
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Follow the example: https://www.b4x.com/android/forum/threads/b4x-input-dialogs-with-xui-views.101197/

There is no such thing:
B4X:
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:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If ((KeyCode = 269) Or (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 String) As ResumableSub
   Dim p As B4XView = xui.CreatePanel("")
   p.SetLayoutAnimated(0, 0, 100dip, 95%x, 95%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


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
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Please upload the updated project.

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

B4X:
If ((KeyCode = 269) Or (KeyCode = KeyCodes.KEYCODE_BACK)) And dialog.Close(xui.DialogResponse_Cancel) Then

i tried to remove the keycode.key_back

B4X:
If (KeyCode = 269) And 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:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
This code is completely wrong:
B4X:
Sub btnOkDialog_Click As ResumableSub
   Wait For (dialog.Close(xui.DialogResponse_Positive)) Complete (Result As Boolean)
   Return True
End Sub
See my previous answer.

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

B4X:
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:
B4X:
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:
B4X:
If (KeyCode = 269) And 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:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I ran your project. Whenever I click on the back key the dialog appears and then on the next click it disappears.
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?
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime 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)


B4X:
    If ((KeyCode == KeyCodes.KEYCODE_BACK) And dialog.Close(xui.DialogResponse_Cancel)) Or (KeyCode == 67) Then
        Return True
    Else If KeyCode == 269 Then
        Return False
    Else
        OpenEXITDialog
        Return True
    End If
 
Upvote 0
Top