Click here to Skip to main content
15,936,119 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have this code it works fine in the same window (in my form process )
i need to add some code to get a handle to the other current active window of anther process's
to get the current language layout of it
i did googled and search so but did-end
plz help
what should i write and where?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace kkey
public partial class Form1 : Form
private static extern IntPtr GetForegroundWindow();

static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

// When you don't want the ProcessId, use this overload and pass IntPtr.Zero for the second parameter
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

const int KL_NAMELENGTH = 9;
const uint KLF_ACTIVATE = 1;

public static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);
public static extern long GetKeyboardLayoutName(System.Text.StringBuilder pwszKLID);

public static string getKLName()
System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
return name.ToString();

InputLanguage _arabicInput;
InputLanguage _englishInput;

GlobalKeyboardHook gHook;
public Form1()

_arabicInput = GetInputLanguageByName("arabic");
_englishInput = GetInputLanguageByName("english");

public static InputLanguage GetInputLanguageByName(string inputName)
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
if (lang.Culture.EnglishName.ToLower().StartsWith(inputName))
return lang;
return null;

private void Form1_Load(object sender, EventArgs e)
gHook = new GlobalKeyboardHook();

gHook.KeyDown += new KeyEventHandler(gHook_KeyDown);
foreach (Keys key in Enum.GetValues(typeof(Keys)))

public void gHook_KeyDown(object sender, KeyEventArgs e)
string keystrike = "";
keystrike = ((char)e.KeyValue).ToString();

IntPtr handle = GetForegroundWindow();
string lang = "";
lang = getKLName();
String english = "00000409";
string Arabic = "00000401";
if (lang == english)
textBox1.Text = getKLName();

if (lang == Arabic)
textBox2.Text = getKLName();


if (InputLanguage.CurrentInputLanguage == _arabicInput)

textBox1.Text += keystrike;
if (InputLanguage.CurrentInputLanguage == _englishInput)



private void label1_Click(object sender, EventArgs e)


private void textBox1_MouseEnter(object sender, EventArgs e)


private void textBox1_TextChanged(object sender, EventArgs e)


private void textBox2_TextChanged(object sender, EventArgs e)

Sergey Alexandrovich Kryukov 4-Aug-13 1:41am    
What is "language layout"?
Ahmad Sadeq 4-Aug-13 2:24am
Ahmad Sadeq 4-Aug-13 2:30am    
the program i working will but in the current window
it get all the keystroke's and get them in the file
but the layout of the active window like IE or chrome
how to get it wither it is
for the active window
i don't know how to explain myself and i really don't know how to ask my question
Sergey Alexandrovich Kryukov 4-Aug-13 3:08am    
At least one can not understand that by "language" you mean English, Arabic, etc., not C#, VB.NET, etc.
Perhaps you can explain the rest... :-)
Ahmad Sadeq 4-Aug-13 17:02pm    
will the think am looking for is in my answer
but the answer return a string of numbers refers to the language like
67699721 = English
and 67175425 = Arabic
what do i need to write to get a string like "En" or "Ar"

1 solution

i searched so and finally find this answer
it's the way

string xxx = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)).ToString();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900