How to determine the current input language?

╄→гoц情女王★ 提交于 2019-12-06 03:59:54

问题


I am designing an on screen keyboard,

I need to determine which language been set by the user and which language he is using now in the other threads,

i.e. I need to know the language selected in the taskbar language switcher:

P.S. current culture returns the language used in the on screen keyboard application, which is not the case I am looking for..


回答1:


The solution was to get the Keyboard Layout for the foreground window, and then apply it to the on screen keyboard, and check for the language in the usual ways..

            IntPtr fore = GetForegroundWindow();
            uint tpid = GetWindowThreadProcessId(fore, IntPtr.Zero);
            IntPtr hKL = GetKeyboardLayout(tpid);
            hKL = (IntPtr)(hKL.ToInt32() & 0x0000FFFF);
            InputLanguageManager m = InputLanguageManager.Current;
            m.CurrentInputLanguage = new System.Globalization.CultureInfo(hKL.ToInt32());
            //IntPtr i = LoadKeyboardLayout(hKL.ToString(), 1);

            InputLanguage = InputLanguageManager.Current.CurrentInputLanguage.ToString();



回答2:


you can also get using WMI:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_BIOS"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_BIOS instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("CurrentLanguage: {0}", queryObj["CurrentLanguage"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}


来源:https://stackoverflow.com/questions/12951067/how-to-determine-the-current-input-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!