B4J Question [BANano] How to handle keypress events in a custom view?

Toky Olivier

Active Member
Licensed User
Longtime User
Hi,
In my customview for inputs or textareas, I though I can add keydown and keypress events by uncommenting event definitions below :
B4X:
'#Event: Keydown (event As BANanoEvent)
'#Event: KeyPress (event As BANanoEvent)
'#Event: KeyUp (event As BANanoEvent)

and add those lines in DesignerCreateView sub:
B4X:
mElement.HandleEvents("keypress", Me, "_keypress")
mElement.HandleEvents("keydown", Me, "_keydown")
mElement.HandleEvents("keyup", Me, "_keyup")

But It's not working. The input and textearea are blocking inputs.
 
Solution
That may be the clue! HandleEvents is using the Umbrella JS handle() wrapper and that one does automatically e.preventDefault() in their code. Looks like your AddEventListener solution if the way to go for you. HandleEvents is probably useful if you are not really wrapping some existing lib, but writing one from scratch.

Good catch!

alwaysbusy

Expert
Licensed User
Longtime User
You need to redirect it to the CallBack class and the method is called mEventName & "_keypress". If you use Me, then the events are redirected to this class and expect a method here.

B4X:
mElement.HandleEvents("keypress", mCallBack, mEventName & "_keypress")
mElement.HandleEvents("keydown", mCallBack, mEventName & "_keydown")
mElement.HandleEvents("keyup", mCallBack, mEventName & "_keyup")

in in your App for example it then becomes:
B4X:
Sub myTextBox_KeyPress(event as BANanoEvent) ' where mytextBox is the mEventName (comes from the Abstract Designer layout)
   ...
End Sub

If you want to handle them in the class itself e.g. to immediately pass the keyCode instead of the whole event, or to cancel it if for example enter is not allowed, you would do:
B4X:
#Event: KeyPress (keyCode as String)

mElement.HandleEvents("keypress", Me, "HandleKeypress")
...
Sub HandleKeypress(event as BANanoEvent)
      BANano.CallSub(mCallBack, mEventName & "_keypress", Array(event.keyCode))
end Sub

Alwaysbusy
 
Last edited:
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
My bad, yes I put like this as you said :
B4X:
mElement.HandleEvents("keypress", mCallBack, mEventName & "_keypress")
mElement.HandleEvents("keydown", mCallBack, mEventName & "_keydown")
mElement.HandleEvents("keyup", mCallBack, mEventName & "_keyup")
But it blocks the input, you cannot write anything.
 
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
I insure you no. As soon as you remove those 3 lines to handle key presses/downs, the input became editable.
 
Upvote 0

Toky Olivier

Active Member
Licensed User
Longtime User
Maybe the object itself has some special internal way to handle these events, and by activating them in your code, you are somehow overwriting their handling.
Yes, I'll do. But why this works (the input is not blocked and event is fired correctly):
B4X:
mElement.AddEventListener("keydown", BANano.CallBackMethod(mCallBack, mEventName & "_keydown"), True)

And not this (this blocks the input):
B4X:
mElement.HandleEvents("keydown", mCallBack, mEventName & "_keydown")
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
That may be the clue! HandleEvents is using the Umbrella JS handle() wrapper and that one does automatically e.preventDefault() in their code. Looks like your AddEventListener solution if the way to go for you. HandleEvents is probably useful if you are not really wrapping some existing lib, but writing one from scratch.

Good catch!
 
Upvote 0
Solution
Top