Capture backspace key

artiechappie

Member
Licensed User
Longtime User
Hi Guys,

I'm a relative newbie here but I have a question relating to updating DrawText on a Canvas.

I input the text in an EditText box and this displays fine on the canvas using DrawText. However, if I want to delete some text then I want the text on the canvas to recognise when the backspace key has been pressed so that it deletes the characters/text on DrawText on the canvas.

I've done some searching on the forums but can't find anything to point me in the right direction. I've tried various methods but just can't get it to work.

Thanks in advance.
 

stevel05

Expert
Licensed User
Longtime User
Have a look at this thread, there are a few options there.
 
Upvote 0

artiechappie

Member
Licensed User
Longtime User
Hi Guys,

Thanks for the pointers but the only KeyCode I could find was KEYCODES_BACK which references the Android back key and not the keyboard Backspace key which is what I was looking for.

This:

Sub Activity_KeyPress (KeyCode As Int) As Boolean

If KeyCode = KeyCodes.KEYCODE_BACK Then

Msgbox("Backspace key pressed (not)","Result")

End If

End Sub

... brings up the MsgBox only when I press the back key.

Any tips?

Thanks
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try logging the keycode (Log("KeyCode "&Keycode)) received in Activity_KeyPress when you press the backspace key and see what you get.
 
Upvote 0

artiechappie

Member
Licensed User
Longtime User
Thanks Steve05

Good tip

Put this in:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the eventKey

Log("KeyCode Pressed = "&KeyCode)

End Sub

... and got this in the log:

KeyCode Pressed = 4

so tried the process again with:

If KeyCode = KeyCodes.KEYCODE_4 Then

   Msgbox("Backspace key pressed (not)","Result")

End If
Still no joy.

Thanks for any help you can offer
 
Last edited by a moderator:
Upvote 0

artiechappie

Member
Licensed User
Longtime User
Duh.....

Sorry guys, Just realised that I was getting "Keycode Pressed = 4" in the log because I was actually pressing the "Back" key to exit the application.

Anyone know what the keycode is for the backspace key?
 
Upvote 0

artiechappie

Member
Licensed User
Longtime User
.... all I want to do is update the text on the Canvas if the backspace key is pressed to correct an entry - maybe there's another way of doing this?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Not all keys generate a keycode, I don't think that the backspace key is a 'standard' android key, It certainly doesn't appear on my phone.

It may be supported on the newer tablet versions Android 3+ possibly.

You could designate a different key, or add a button to your layout.
 
Upvote 0

artiechappie

Member
Licensed User
Longtime User
Maybe post your project file

Sent from my DROIDX

Here's my code snippet as a Canvas test. What I want to do is make the text that appears on the canvas editable - for example, if the user presses the backspace key on the Android phone's keyboard then this is reflected on the text that appears on the canvas.

I want to be able to create signs, nameplates etc on the canvas area and then save the canvas as a graphic which can be sent to the manufacturer.

Thanks for any help.

Allen
 

Attachments

  • CanvasTest.zip
    6.9 KB · Views: 231
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are:
B4X:
Sub TextBox1_TextChanged (Old As String, New As String)
    c.DrawBitmap(B, Null, brect)    
    c.DrawText(New,25,35,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
    
    i.Invalidate
    
End Sub
You need to redraw the background bitmap and then draw the text.

Best regards.
 
Upvote 0
Top