I want to know if I have completed an input word on a wpf textbox, but TextChanged
event will fire even if I am typing in IME input. Therefore, I try to distinguish WM_IME_CHAR
from WM_CHAR
message (like I did in c++ windows form). I can get window message for a WPF window using this method, but how can I get the message specifically for a textbox inside WPF window?
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSource hwndSource = PresentationSource.FromVisual(myTextbox) as HwndSource;
if (hwndSource != null)
{
hwndSource.AddHook(WndProc);
}
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_CHAR = 0x0102;
const int WM_IME_CHAR = 0x0286;
switch(msg)
{
case WM_CHAR
Console.WriteLine("WM_CHAR:" + wParam);
//this is for testing
case WM_IME_CHAR
Console.WriteLine("WM_IME_CHAR" + wParam);
//do something if myTextbox receive an IME char...
break;
}
return IntPtr.Zero;
}
My above code cannot get any message for textbox. Is anything I am doing wrong?
EDIT:
I'm using VS2015 on windows 10.
If I focus on Window
and type something in English, I can get WM_CHAR
message and the correct wParam
.
If I click on TextBox
, I cannot get any WM_CHAR
message.
If I focus on either Window
or TextBox
and type something in Chinese Input Method, I cannot get any message.