Android Question KeyEvent inline java error

laomms

Member
Licensed User
I want use inline way to insert text to webview element :
B4X:
<input onkeypress="return event.charCode >= 48 && event.charCode <= 57" id="field1" type="tel" value="" pattern="[0-9]*">


B4X:
private Sub Process_HTML(Html As String)
    str=Regex.Split(" ","111 222 333 444")
    Dim jo As JavaObject = WebView1  
    For i=0 To str.length-1
        jo.RunMethod("KeyClick", Array(WebView1,str(i)))
    Next

End Sub

B4X:
#if JAVA

import android.view.KeyEvent ;
import android.webkit.WebViewClient;

public void KeyClick(WebView mWebView,String insertExt) {
     
      char[] arrayOfChar = insertExt.toCharArray();
     BA.Log(insertExt);
      for (int i = 0; i < arrayOfChar.length; i++)
      {
        if (i == 0)
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
        }
        if (arrayOfChar[i] == '0')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0));
        }
        if (arrayOfChar[i] == '1')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_1));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_1));
        }
        if (arrayOfChar[i] == '2')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_2));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_2));
        }
        if (arrayOfChar[i] == '3')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_3));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_3));
        }
        if (arrayOfChar[i] == '4')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_4));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_4));
        }
        if (arrayOfChar[i] == '5')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_5));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_5));
        }
        if (arrayOfChar[i] == '6')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_6));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_6));
        }
        if (arrayOfChar[i] == '7')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_7));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_7));
        }
        if (arrayOfChar[i] == '8')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_8));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_8));
        }
        if (arrayOfChar[i] == '9')
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_9));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_9));
        }
        if (i == -1 + arrayOfChar.length)
        {
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          mWebView.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
        }
      }

}

#end if

It's can not complite, need class WebView
 
Last edited:

laomms

Member
Licensed User
ok I got it success use NativeMe.RunMethod

B4X:
    For Each s As String In mStr
        NativeMe.RunMethod("KeyInsert", Array(s))
        Sleep(200)
    Next

B4X:
#if JAVA

import android.view.KeyEvent ;

public void KeyClick(String insertExt) {
    
      char[] arrayOfChar = insertExt.toCharArray();
      BA.Log(insertExt);
      for (int i = 0; i < arrayOfChar.length; i++)
      {
        if (i == 0)
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
        }
        if (arrayOfChar[i] == '0')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0));
        }
        if (arrayOfChar[i] == '1')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_1));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_1));
        }
        if (arrayOfChar[i] == '2')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_2));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_2));
        }
        if (arrayOfChar[i] == '3')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_3));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_3));
        }
        if (arrayOfChar[i] == '4')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_4));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_4));
        }
        if (arrayOfChar[i] == '5')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_5));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_5));
        }
        if (arrayOfChar[i] == '6')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_6));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_6));
        }
        if (arrayOfChar[i] == '7')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_7));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_7));
        }
        if (arrayOfChar[i] == '8')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_8));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_8));
        }
        if (arrayOfChar[i] == '9')
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_9));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_9));
        }
        if (i == -1 + arrayOfChar.length)
        {
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB));
          this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_TAB));
        }
      }

}

#end if
 
Last edited:
Upvote 0
Top