> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XGameUiTextEntryOpen

> XGameUiTextEntryOpen

# XGameUiTextEntryOpen

打开一个虚拟键盘，由游戏负责渲染文本。目前在桌面平台上未实现。

## 语法

```cpp theme={null}
HRESULT XGameUiTextEntryOpen(  
         const XGameUiTextEntryOption* options,
         uint32_t maxLength,
         const char* initialText,
         uint32_t initialCursorIndex,
         XGameUiTextEntryHandle* handle 
)  
```

### 参数

*options*   \_In\_\
类型：[XGameUiTextEntryOptions](/reference/system/xgameui/structs/xgameuitextentryoptions)

指示打开键盘时应用于键盘的初始选项集，例如位置和键盘配置。

*maxLength*   \_In\_\
类型：uint32\_t

用户可以在虚拟键盘中输入的最大字符数。此数字不得大于 32K。

*initialText*   \_In\_\_opt\_z\_\
类型：const char\*

在虚拟键盘中初始填充的以 null 结尾的字符串。

*initialCursorIndex*   \_In\_\
类型：uint32\_t

光标相对于起始字符的初始字节索引。

*handle*   \_Out\_\
类型：XGameUiTextEntryHandle

新打开的虚拟键盘的句柄。

### 返回值

类型：HRESULT

HRESULT 成功或错误代码。有关错误代码列表，请参阅[错误代码](/reference/errorcodes)。

| 返回代码            | 说明          |
| --------------- | ----------- |
| S\_OK           | 操作成功。       |
| E\_ACCESSDENIED | 已经在显示一个对话框。 |

## 备注

此函数将在能够获取句柄后立即返回，即使虚拟键盘尚未可见。句柄返回后，可以立即安全地调用所有其他 **XGameUiTextEntry** 函数。

以下示例演示了如何使用 **XGameUiTextEntry** 函数的基础知识。

```cpp theme={null}
// Somewhere in the main game loop
// ...
bool someSignal;
bool showingKeyboard;
uint32_t KEYBOARD_MAX_CHARACTERS = 256; 
uint32_t KEYBOARD_BUFFER_SIZE = KEYBOARD_MAX_CHARACTERS * 4;

XGameUiTextEntryHandle keyboardInstance;

if (someSignal)
{
    XGameUiTextEntryOptions options;
    options.inputScope = XGameUiTextEntryInputScope::Default;
    options.positionHint = XGameUiTextEntryPositionHint::Bottom;
    options.flags = XGameUiTextEntryVisibilityFlags::Default;

    if (SUCCEEDED(XGameUiTextEntryOpen(&options, 
                                       nullptr, 
                                       0, 
                                       KEYBOARD_MAX_CHARACTERS, 
                                       &keyboardInstance)))
    {
        showingKeyboard = true;
    }
}

if (showingKeyboard)
{
    char* buffer[KEYBOARD_BUFFER_SIZE];
    uint32_t cursorPosition;
    XGameUiTextEntryChangeTypeFlags changeType;

    if (SUCCEEDED(XGameUiTextEntryGetState(
        keyboardInstance,
        &changeType,
        &cursorPosition,
        nullptr,
        nullptr,
        KEYBOARD_BUFFER_SIZE,
        buffer)))
    {
        if (changeType & XGameUiTextEntryChangeTypeFlags::TextChanged)
        {
            // Copy text for in-game rendering
            RenderTextInGame(buffer, cursorPosition);

            // Assuming the game doesn't want multi-line input
            if (strstr(buffer, "\n")
            {
                XGameUiTextEntryClose(keyboardInstance);
                showingKeyboard = false;
            }
        }

        if (showingKeyboard && (changeType & XGameUiTextEntryChangeTypeFlags::KeyboardDismissed))
        {
            XGameUiTextEntryClose(keyboardInstance);
            showingKeyboard = false;
        }    
    }
}
```

## 要求

**标头：** XGameUI.h

**库：** xgameruntime.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 请参阅

[XGameUI](/reference/system/xgameui/xgameui_members)\
[虚拟键盘支持](/build/core-features/common/input/overviews/virtualkeyboardsupport)


## Related topics

- [XGameUiTextEntryOptions](/zh-CN/reference/system/xgameui/structs/xgameuitextentryoptions.md)
- [XGameUiTextEntryClose](/zh-CN/reference/system/xgameui/functions/xgameuitextentryclose.md)
- [XGameUiTextEntryGetState](/zh-CN/reference/system/xgameui/functions/xgameuitextentrygetstate.md)
- [XGameUiTextEntryInputScope](/zh-CN/reference/system/xgameui/enums/xgameuitextentryinputscope.md)
- [虚拟键盘支持](/zh-CN/build/core-features/common/input/overviews/virtualkeyboardsupport.md)
