Hello,
I am currently attempting to programmatically clear the IME composition string from a Unity C# script using Windows API calls. However, I am encountering an issue where the ImmNotifyIME function returns False, and the IME composition string and candidate window remain visible on the screen.
Problem Details:
- Function Used: I am using
ImmNotifyIME to try to close the IME composition string and candidate window.
- Issue: The function returns
False, but when I retrieve the error code using Marshal.GetLastWin32Error(), it returns 0 (ERROR_SUCCESS), indicating that no specific error has occurred.
- Impact: Internally, it appears that the composition string is cleared, but visually, the composition string and candidate window remain displayed.
- Reproducibility: The same behavior occurs with both Microsoft IME and Google Japanese Input.
Error Messages and Logs:
var result = ImmNotifyIME(hIMC, NI_CLOSECANDIDATE, 0, 0);
if (!result)
{
int errorCode = Marshal.GetLastWin32Error();
Debug.LogError($"ImmNotifyIME failed with error code: {errorCode}");
}
System Information:
- Unity Version: 2022.3.28f1
- IME Used: Microsoft IME for Japanese Input
What I've Tried:
private const int NI_COMPOSITIONSTR = 0x15;
private const int NI_CLOSECANDIDATE = 0x0010;
private const int CPS_CANCEL = 0x4;
private const int CPS_COMPLETE = 0x1;
private const uint SCS_SETSTR = 0x0009;
IntPtr hIMC = ImmGetContext(windowHandle);
if (hIMC != IntPtr.Zero)
{
//The text in InputField does not disappear here.
ImmNotifyIME(hIMC, NI_CLOSECANDIDATE, 0, 0);
ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_COMPLETE, 0);
ImmSetCompositionString(hIMC, SCS_SETSTR, IntPtr.Zero, 0, IntPtr.Zero, 0);
ImmNotifyIME(hIMC, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
ImmReleaseContext(windowHandle, hIMC);
}
Questions:
- What could be the possible reasons for
ImmNotifyIME returning False with an error code of 0?
- Is there a reliable way to programmatically clear the IME composition string and candidate window?
- Are there other functions or approaches I should try?
I would greatly appreciate any guidance or suggestions.
Thank you.