WebAPI/KeboardIME: Difference between revisions

Line 191: Line 191:
== Examples ==
== Examples ==


  var conn = navigator.mozInputMethodConnection;
  var im = navigator.inputMethod;


  // Called when the user starts or finishes editing an input field
  // Called when the user starts editing an input field or switches to current IME
  conn.oninputmethodstatechange= function(event) {
  im.onstart = function(event) {
    if (conn.inputStarted) {
  // Text input should start
        // Text input should start
};
    } else {
 
        // Text input should end
// Called when the user finishes editing an input field or switches to another IME.
    }
im.onfinish = function(event) {
  // Text input should end
  };
  };


  // Insert a string at the current cursor position
  // Insert a string at the current cursor position
  conn.replaceSurroundingText('Hello world');
  im.commitText('Hello world');


  // Clear delete 5 characters before the cursor position.
  // Clear delete 5 characters before the cursor position.
  conn.replaceSurroundingText('', 5);
  im.deleteSurroundingText(-5, 5);
 
// Get the selected text
conn.getText(conn.selectionStart, conn.selectionEnd, function callback(text) {
    var selectedText = text;
});


  // Get notified when the text content has changed.
  // Get notified when the text content has changed.
  conn.ontextchange = function() {
  im.onsurroundingtextchange = function(beforeText, afterText) {
   conn.getText(0, conn.textLength, function(text) {
   console.log(beforeText + afterText);
    console.log(text);
  }
  }
  }


  // Move the cursor position
  // Move the cursor position
  var position = 10;
  var position = 10;
  conn.setSelectionRange(position, position);
  im.setSelectionRange(position, position);


  // Hide the keyboard
  // Hide the keyboard
  conn.removeFocus();
  im.removeFocus();
 
// Switch the focus onto the next input field if possible
if (conn.canAdvanceFocus) {
    conn.advanceFocus();
}


== Related ==
== Related ==
Confirmed users
58

edits