1# Using the Input Method in a Custom Edit Box (C/C++) 2<!--Kit: IME Kit--> 3<!--Subsystem: MiscServices--> 4<!--Owner: @illybyy--> 5<!--Designer: @andeszhang--> 6<!--Tester: @murphy1984--> 7<!--Adviser: @zhang_yixin13--> 8 9## When to Use 10 11IME Kit allows you to use input method in the custom edit box to interact with input method applications, including displaying and hiding input methods and receiving text editing notifications from input method applications. This document describes how to use C/C++ to develop this function. 12 13## APIs 14 15For details about the APIs, see [API Reference](../reference/apis-ime-kit/capi-inputmethod.md). 16 17## Adding Dynamic Link Libraries 18 19Add the following library to **CMakeLists.txt**. 20 21```txt 22libohinputmethod.so 23``` 24 25## Including Header Files 26 27```c 28#include <inputmethod/inputmethod_controller_capi.h> 29``` 30 31 32## Binding an Input Method 33 34When the text box is focused, you can call the [OH_InputMethodController_Attach](../reference/apis-ime-kit/capi-inputmethod-controller-capi-h.md#oh_inputmethodcontroller_attach) API to bind the input method. After the binding is successful, you can use the input method to enter text. 35 361. Create an **InputMethod_TextEditorProxy** instance. The sample code is as follows: 37 38 ```c 39 // Create an InputMethod_TextEditorProxy instance. 40 InputMethod_TextEditorProxy *textEditorProxy = OH_TextEditorProxy_Create(); 41 ``` 42 433. Create an **InputMethod_AttachOptions** instance and set the options for binding the input method. The sample code is as follows: 44 45 ```c 46 // Create an InputMethod_AttachOptions instance. showKeyboard specifies whether to display the keyboard after the binding is successful. The following uses displaying the target keyboard as an example. 47 bool showKeyboard = true; 48 InputMethod_AttachOptions *options = OH_AttachOptions_Create(showKeyboard); 49 ``` 50 514. Call **OH_InputMethodController_Attach** to bind the input method service. After the call is successful, you can obtain **InputMethod_InputMethodProxy** used to interact with the input method. The sample code is as follows: 52 53 ```c 54 InputMethod_InputMethodProxy *inputMethodProxy = nullptr; 55 // Send a binding request. 56 if (OH_InputMethodController_Attach(textEditorProxy, options, &inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 57 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Attach failed!"); 58 } 59 ``` 60 61## Displaying or Hiding the Panel 62 63After the binding is successful, you can use the obtained [InputMethod_InputMethodProxy](../reference/apis-ime-kit/capi-inputmethod-inputmethod-inputmethodproxy.md) object to send a message to the input method. The sample code is as follows: 64 65```c 66// Display the keyboard. 67if (OH_InputMethodProxy_ShowKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 68 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "ShowKeyboard failed!"); 69} 70// Hide the keyboard. 71if (OH_InputMethodProxy_HideKeyboard(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 72 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "HideKeyboard failed!"); 73} 74// Notify the change of the input box configuration. 75if (OH_InputMethodProxy_NotifyConfigurationChange(inputMethodProxy, InputMethod_EnterKeyType::IME_ENTER_KEY_GO, InputMethod_TextInputType::IME_TEXT_INPUT_TYPE_TEXT) != InputMethod_ErrorCode::IME_ERR_OK) { 76 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "NotifyConfigurationChange failed!"); 77} 78``` 79 80## Listening to Requests/Notifications from the Input Method Application 81 821. Implement the response processing function for the request or notification sent by the input method application. The sample code is as follows: 83 84 ```c 85 // Implement the input method application event response function in InputMethod_TextEditorProxy. 86 void GetTextConfig(InputMethod_TextEditorProxy *textEditorProxy, InputMethod_TextConfig *config) 87 { 88 // Process the request sent by the input method for obtaining the input box configuration. 89 } 90 void InsertText(InputMethod_TextEditorProxy *textEditorProxy, const char16_t *text, size_t length) 91 { 92 // Process the text insertion request sent by the input method. 93 } 94 void DeleteForward(InputMethod_TextEditorProxy *textEditorProxy, int32_t length) 95 { 96 // Process the text deletion request sent by the input method. 97 } 98 // ...... 99 ``` 100 1012. Set the implemented response function to [InputMethod_TextEditorProxy](../reference/apis-ime-kit/capi-inputmethod-inputmethod-texteditorproxy.md), and then set the response function to the input method framework using [OH_InputMethodController_Attach](../reference/apis-ime-kit/capi-inputmethod-controller-capi-h.md#oh_inputmethodcontroller_attach) called when the input method is bound to complete listener registration. The sample code is as follows: 102 103 ```c 104 // Set the implemented response processing function to InputMethod_TextEditorProxy. 105 OH_TextEditorProxy_SetGetTextConfigFunc(textEditorProxy, GetTextConfig); 106 OH_TextEditorProxy_SetInsertTextFunc(textEditorProxy, InsertText); 107 OH_TextEditorProxy_SetDeleteForwardFunc(textEditorProxy, DeleteForward); 108 ``` 109 110## Unbinding an Input Method 111 112When the edit box is out of focus, you need to stop using the input method and unbind the input method framework by calling [OH_InputMethodController_Detach](../reference/apis-ime-kit/capi-inputmethod-controller-capi-h.md#oh_inputmethodcontroller_detach). 113 114```c 115// Send an unbinding request. 116if (OH_InputMethodController_Detach(inputMethodProxy) != InputMethod_ErrorCode::IME_ERR_OK) { 117 OH_LOG_Print(LOG_APP, LOG_ERROR, 0, "testTag", "Detach failed!"); 118} 119inputMethodProxy = nullptr; 120OH_TextEditorProxy_Destroy(textEditorProxy); 121textEditorProxy = nullptr; 122OH_AttachOptions_Destroy(options); 123options = nullptr; 124``` 125