[WinUI3] How to disable input methods when using TextBox?

lei han 46 Reputation points
2022-06-23T13:14:34.38+00:00

214575-image.png
I want the TextBox to only be able to enter letters from the keyboard.And disable input methods.
I have tried the following methods:
~~~
ImmAssociateContext(main_window_hwnd, NULL);
~~~
but failed to disable successfully. Is the "main_window_hwnd" error? And I want to know how to get the right hwnd.

I also tried to intercept 'PreviewKeyDown'.It failed too.

I also know we can use “InputMethod” to Implement this in wpf,but what is the corresponding method in winui3?

Developer technologies Universal Windows Platform (UWP)
Windows development Windows App SDK
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,521 Reputation points
    2022-06-25T14:35:54.997+00:00

    I did some tests and this one worked, on Windows 10 21H1, not sure for other OS =>

    (in MainWindow constructor)

    IntPtr hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);  
    IntPtr pContext = ImmGetContext(hWnd);  
    bool bRet = ShowReadingWindow(pContext, false);  
    

    with declarations :

        [DllImport("Imm32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern IntPtr ImmGetContext(IntPtr unnamedParam1);  
    
        [DllImport("CHxReadingStringIME.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        public static extern bool ShowReadingWindow(IntPtr hwnd, bool bShow);
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,866 Reputation points
    2022-06-24T02:40:17.983+00:00

    Hello,
    Welcome to Microsoft Q&A!

    How to disable input methods when using TextBox?

    I think you could use PreviewKeyDown evnet to disable textbox input, PreviewKeyDown contains KeyRoutedEventArgs, you could set set the handler as true to disable keyboard input

    private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)  
    {  
        e.Handled = true;  
    }  
    

    Is the "main_window_hwnd" error? And I want to know how to get the right hwnd.

    In WinUI3 we have provide way to Retrieve a window handle, please refer to Retrieve a window handle (HWND) official document.

    The C# code below shows how to retrieve the window handle (HWND) for a WinUI 3 Window object.

    private async void myButton_Click(object sender, RoutedEventArgs e)  
    {  
        // Retrieve the window handle (HWND) of the current WinUI 3 window.  
        var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);  
    }  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Abhishek Jakabal 86 Reputation points
    2022-06-24T05:54:46.73+00:00

    How about disabling the textbox???
    For Ex: If the text box name is 'txt_name'. Then the to disable it just use the code = txt_name.isEnabled = false;
    and to enable it back = txt_name.isEnabled = true;


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.