Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the current input language?

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:

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..

like image 725
Abdallah Nasir Avatar asked Oct 18 '12 09:10

Abdallah Nasir


2 Answers

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();
like image 70
Abdallah Nasir Avatar answered Oct 14 '22 00:10

Abdallah Nasir


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);
            }
        }
    }
}
like image 20
Arshad Avatar answered Oct 14 '22 01:10

Arshad