How to Trap Each Keyboard Character in a PhoneGap Android App

I am creating a PhneGap App that will run on an Android device. Using Basic Script, I am trying to trap each character as it is being entered into a Text Box. I have tried using the standard event but none seem to work. The events like onKeyPressed only fires when the key is pressed or onBlur only fires when the focus leaves the field. Is there a way to actually trap each key as it is being entered via the Android virtual keyboard? Any help will be greatly appreciated.

This works for me when running as a web app, should work ok when compiled as a phonegap app:

Function window_onkeydown(e)
  If keycapture = "off" Then Exit Function 
    keyval = e.keyCode
    Call dosomething
End Function    

It doesn’t catch Function keys since most are intercepted by the browser, so I don’t use these keys in an app. This function gets all key presses regardless of form being displayed or whether an input box or text area are being used. So I filter the Call to dosomething by knowing what form or what input object is being used. I can toggle the capture by setting the variable keycapture. I use a single modal form to enter text with only one input object available in the entire app (a separate one is used for textarea input). By using this method I know exactly what data field the user is typing in the app.

A little tricky, but in this particular app I am reproducing a program written many years ago in DOS that used only the keyboard for navigation, no mouse or touch. Works quite well. But I’m not using a black background, well… maybe I should to make it more authentic.

John

Hi John,

Thanks for your quick response. I put the following code into my Application but regardless of what keyboard chacter I press, I the Print function always says the Key Val = 229. Any thoughts as to why?

Function window_onkeydown(e)
  Dim keyVal
  If gKeyCapture = "off" Then Exit Function 
    keyVal = e.keyCode
    Print ("Key Val = " & keyVal)
End Function

Regards,
Robert

I started a new project and put:
``
Function window_onkeydown(e)
Dim keyVal
keyVal = e.keyCode
Print ("Key Val = " & keyVal)
End Function

as the only code. F5 to Chrome on Windows PC and it works ok. Try this minimal app and see what happens. Are you using Chrome on a PC? Do not compile with phonegap. If an apk doesn't work I don't know what is going on.

John

I googled android key 229 and came up with quite a few related hits. Here’s a couple of results that may lead to a solution:

Kind regards,
Doug

I use window_onkeydown(e) on PC Windows/Chrome only so it works ok here. I use a different method if I want to capture keyboard input for Android devices that uses SetInterval:

TK = SetInterval(testtextvalue,100) ' to turn on testing
ClearInterval(TK) 'to turn off testing.

This method works best if you use a dedicated form for user text input.

In the function testtextvalue you can look at the input text value and see what was entered in the last 100 ms. Code this function depending on what you want to accomplish. You can see what was the last character(s) entered, or test the length of the data that was entered, or whatever.

John

Just a reminder: If you enclose your code in triple left ticks, it will be nicely formatted:

As Doug pointed out above, from Google searching… “Normal keypress event does not give keyCode in android devices.” So, to complete this topic and offer a final solution for others that might be inquiring about the same issue I offer the folowing code:

Function getKeyCode(str)
  return Asc(Right(str,1))
End Function

Function {Text_Box_ID}_onkeyup(e)
  Dim keyCode
  keyCode = e.which
  If keyCode = 0 Or keyCode = 229 Then keyCode = getKeyCode(this.value)
  NSB.Print(False) 
  Print ("Key Code = " & keyCode)
  {Do Something...}
End Function

Regards,
Robert

1 Like

Thanks for posting the follow up!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.