KeyPress questions

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Under "Activity" in the help files, it says:

The KeyPress event occurs when the user has pressed on the key and assuming that no other view has consumed this event (like EditText).

Yet EditText does not show KeyPress as being one of its events. Does the EditText's TextChanged event consume the keypresses?

Is Activity the only thing that can use the KeyPress KeyCodes? For example, in EditText's TextChanged, to look for a particular character, it seems like you would use the literal character (such as "A" or "%") and not use KeyCodes.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
EditText internally consumes the key press event. Other views may also internally consume some or all of the key press events. For example in order to scroll the view or to select an item.

In most cases you will need to have an activity without any other views in order to handle KeyPress event.

The TextChanged event is different. It gives you the complete string not just the last character pressed (which may be in the middle of the string in some cases).
 
Upvote 0

silverbyte

Member
Licensed User
Longtime User
Ok so how do I capture the keys being pressed then?

All I would like to do is when i press the letter A i can do something
when i press the letter B do something else

i want to capture each keypress so i can trigger an function

** NOTE: Im using softkeyboard **

The following code in my Activity keypress or keyup does not work, and does not capture any characters, symbols or numbers
s = KeyCode
If KeyCode >= KeyCodes.KEYCODE_0 AND KeyCode <= KeyCodes.KEYCODE_9 Then
s = Chr(KeyCode + 41)
Else If KeyCode >= KeyCodes.KEYCODE_A AND KeyCode <= KeyCodes.KEYCODE_Z Then
s = Chr(KeyCode + 36)
End If

ToastMessageShow(s,False)
 
Last edited:
Upvote 0

silverbyte

Member
Licensed User
Longtime User
Focus is on edittext because that is how I popup my keyboard. Then I have the following code

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
If Keycode = KeyCodes.KEYCODE_BACK Then
Return False
Else
Return False
End If
End Sub

(I know its redundant because it always returns false, i was just playing with it) So the answer to your question is Activity_KeyPress states NOT to consume the keypress.

Now I understand that focus is on edittext and therefore it ?MAY? consumes the keypress? although how do you stop this from happening. Is there an alternative to accomplishing my goal.

Something that replicates EditText1_KeyPress(keycode as string).
Im aware OF EditText1_TextChanged although this is a GREAT function, it is not what i need.
 
Last edited:
Upvote 0

silverbyte

Member
Licensed User
Longtime User
Clever trick! I think that would work better than what im doing now. What I was doing is grabbing the position of the cursor and always reading 1 character position on textchanged

Sub EditText1_TextChanged (Old As String, New As String)
Dim str As String
str = New

If EditText1.SelectionStart > 0 Then
TTS1.Speak(str.SubString2(EditText1.SelectionStart - 1,EditText1.SelectionStart), True)
End If

End Sub

This works extremely well except when i backspace it reads the last character... not to bad but not what i want.

Im just creating an app for my 7month old boy so thay when he taps the letters of the alphabet on the keyboard the TTS reads out the character he types in. Im hoping this will let him learn how the letters of the alphabet.
 
Upvote 0
Top