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
{
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
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
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);
const int KL_NAMELENGTH = 9;
const uint KLF_ACTIVATE = 1;
[DllImport("user32.dll")]
public static extern long LoadKeyboardLayout(string pwszKLID, uint Flags);
[DllImport("user32.dll")]
public static extern long GetKeyboardLayoutName(System.Text.StringBuilder pwszKLID);
public static string getKLName()
{
System.Text.StringBuilder name = new System.Text.StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
return name.ToString();
}
///
InputLanguage _arabicInput;
InputLanguage _englishInput;
GlobalKeyboardHook gHook;
public Form1()
{
InitializeComponent();
_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)))
gHook.HookedKeys.Add(key);
gHook.hook();
}
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)
{
}
}
}