Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

c# - Update input language of on screen keyboard

I have an Windows Forms application written in C# .NET 5 with a webview2 control which hosts a kiosk based application which will be run full screen on a touchscreen.

The application will use the on screen keyboard for user input. The application is hosted on our own machines, which will only be used for our application so changing the system input language is exactly what we want to achieve.

I need to allow the users to pick their own input language. In 4.7.2 I can achieve this by setting InputLanguage.CurrentInputLanguage, which dynamically updates the on screen keyboard input language, you also see the system input language update in the task bar (which in production will not be visible to users).

In .NET 5, setting the same value does not have the desired effect, the on screen keyboard does not reflect the changed input language, not does the input language update in the taskbar.

I notice the libraries behind the scenes have changed which is clearly why I am having this issue.

I have tried the following without success in a simple dummy app:

        private void btnEnglish_Click(object sender, EventArgs e)
        {
            var language = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-GB"));
            if (InputLanguage.InstalledInputLanguages.IndexOf(language) >= 0)
                InputLanguage.CurrentInputLanguage = language;

            ChangeSystemInputLanguage(language);
        }
        
        private static void ChangeSystemInputLanguage(InputLanguage language)
        {
            Application.OleRequired();
            
            IntPtr handleOld = ActivateKeyboardLayout(new HandleRef(language, language.Handle), 0);
            if (handleOld == IntPtr.Zero)
            {
                throw new ArgumentException("ErrorBadInputLanguage", nameof(language));
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr ActivateKeyboardLayout(HandleRef hkl, int uFlags);

Short of downgrading to 4.7.2 or re-writting in WPF (and such using the InputLanguageManager), can anyone make any recommendations how I might achieve the desired effect in .NET 5.0?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For anyone having this issue, the answer was incredibly simple...

before updating the CurrentInputLanguage, set focus on a control on in your form!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...