如何确定当前的输入语言?

时间:2012-10-18 09:11:21

标签: c# winapi keyboard keyboard-hook

我正在设计一个屏幕键盘,

我需要确定用户设置的语言以及他现在在其他线程中使用的语言,

即。我需要知道在任务栏语言切换器中选择的语言:

language switcher

P.S。当前文化返回屏幕键盘应用程序中使用的语言,这不是我正在寻找的情况..

2 个答案:

答案 0 :(得分:6)

解决方案是获取前景窗口的键盘布局,然后将其应用于屏幕键盘,并以通常的方式检查语言..

            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();

答案 1 :(得分:5)

您也可以使用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);
            }
        }
    }
}