2

I want to type special characters (such as 'ä', 'é', '£', etc.) in an Android webview. These characters are not included into the virtual KCM (key characters map). So I can't retrieve the keycode associated to the character and create a KeyEvent.

I tried different methods:

String t = "testàaâäù"; char[] charArray = t.toCharArray(); if (charArray.length > 0) { KeyCharacterMap keyCharacterMap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); KeyEvent[] events = keyCharacterMap.getEvents(charArray); } // events is null, because some characters aren't include in KCM // it never works in any case 

With instrumentation methods:

instrumentation.sendStringSync("a"); // Display 'a' instrumentation.sendStringSync("àâä"); // Display nothing instrumentation.sendCharacterSync(KeyEvent.KEYCODE_H); // Display 'h' // Instrumentation seems to use KCM for data entry 

With ACTION_MULTIPLE KeyEvent:

KeyEvent event = new KeyEvent(SystemClock.uptimeMillis(), String.valueOf("aàâä"), 0, 0); focusedView.dispatchKeyEvent(event); // Doesn't work with WebView, but works with other components such as EditText. // I get this error: Unimplemented WebView method onKeyMultiple called from: android.webkit.WebView.onKeyMultiple 

Can I implement the method onKeyMultiple (WebView) to handle data entry? Is there a solution for inputting special characters in the webview? I need a solution that doesn't require root or the creation of a keyboard layout (user hasn't to select the keyboard).

1 Answer 1

2

I had the same problem when writing with the virtual keyboard on an input field inside a WebView. It would not recognize special characters like "á". What I did:

if (event.getAction() == KeyEvent.ACTION_MULTIPLE) { loadUrl("javascript:var prevText = document.activeElement.value;" + "var afterText = document.activeElement.value = prevText + \"" + event.getCharacters() + "\";"); return false; } 

When the action is ACTION_MULTIPLE with Javascript I get the input field and I append the special character.

Here is the whole class:

public class CustomWebView extends WebView { public CustomWebView (Context context, AttributeSet attrs) { super(context, attrs); } @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { ExtenderInputConnection connection = new ExtenderInputConnection(this, false); outAttrs.imeOptions = EditorInfo.IME_ACTION_SEARCH; return connection; } @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_MULTIPLE) { loadUrl("javascript:var prevText = document.activeElement.value;" + "var afterText = document.activeElement.value = prevText + \"" + event.getCharacters() + "\";"); return false; } return super.dispatchKeyEvent(event); } public class ExtenderInputConnection extends BaseInputConnection implements InputConnection { public ExtenderInputConnection(View targetView, boolean fullEditor) { super(targetView, fullEditor); } @Override public boolean deleteSurroundingText(int beforeLength, int afterLength) { if (beforeLength == 1 && afterLength == 0) { // backspace return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL)); } return super.deleteSurroundingText(beforeLength, afterLength); } } } 

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.