其他进程的GetKeyboardLayoutName?

基本上,我想要做的是让GetKeyboardLayoutName返回其他进程的键盘ID(KLID)。 默认情况下,它只重新显示我的应用程序窗口的键盘ID。 我也试过GetKeyboardLayout,但是它返回一个不幸的HKL(它接受另一个窗口的hwnd)。

或者,如果有一种将HKL转换成KLID的方法,那也是很棒的,但是我怀疑是不是有这样的一个问题。

那么,我怎么能做到这一点呢? 我希望能够使用获得的KLID作为LoadKeyboardLayout函数的参数。

Solutions Collecting From Web of "其他进程的GetKeyboardLayoutName?"

我想出了一个解决方法,以便能够检索另一个应用程序的键盘布局。 这个过程很简单:

  1. 使用GetKeyboardLayout获取其他应用程序中的键盘语言的HKL。 要获得另一个应用程序的句柄,可以使用GetForegroundWindow获取窗口的句柄,然后使用GetWindowThreadProcessId获取具有先前检索的窗口句柄的进程的句柄。

  2. 将以前提取的HKL设置为我的应用程序的键盘句柄,使用ActivateKeyboardLayout。

  3. 获取我的应用程序的键盘语言ID(KLID)。

最后,我的应用程序将使用与其他应用程序相同的键盘语言,并从我的应用程序中,我可以轻松地提取KLID。 这里是我在我的项目中使用的示例代码。 希望它也能帮助你。

using System.Runtime.InteropServices; ... StringBuilder Input = new StringBuilder(9); [DllImport("user32.dll")] static extern uint GetKeyboardLayoutList(int nBuff, [Out] IntPtr[] lpList); [DllImport("user32.dll")] private static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags); [DllImport("user32.dll")] static extern bool GetKeyboardLayoutName([Out] StringBuilder pwszKLID); [DllImport("user32.dll")] static extern IntPtr GetKeyboardLayout(uint idThread); [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("user32.dll")] public static extern int ActivateKeyboardLayout(int HKL, int flags); private static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam); private static uint WM_INPUTLANGCHANGEREQUEST = 0x0050; private static int HWND_BROADCAST = 0xffff; private static uint KLF_ACTIVATE = 1; ... public StringBuilder GetInput() { IntPtr layout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero)); ActivateKeyboardLayout((int)layout, 100); GetKeyboardLayoutName(Input); return Input; } public void SetInput(string InputID) { PostMessage(HWND_BROADCAST, WM_INPUTLANGCHANGEREQUEST, IntPtr.Zero, LoadKeyboardLayout(InputID, KLF_ACTIVATE)); } 

GetInput()方法返回一个StringBuilder,它可以使用ToString()将其转换为字符串,然后用SetInput()方法设置键盘语言。

我只是在这里留下来,以防其他人需要知道每个代码代表什么。 我花了几个小时才手工制作这个清单。 这是每个布局代码到我的Windows 7版本可用的布局名称的映射。 我必须亲自输入姓名,所以请原谅。 代码以编程方式收集。

 using System; using System.Runtime.InteropServices; using System.Text; using System.Windows.Input; namespace DesktopDictation.DeviceButtonMappings { public class KeyboardLayout { const int KL_NAMELENGTH = 9; [DllImport("user32.dll")] private static extern long GetKeyboardLayoutName(StringBuilder pwszKLID); //each keyboard layout is defined in Windows as a hex code public static string GetLayoutCode() { var name = new StringBuilder(KL_NAMELENGTH); GetKeyboardLayoutName(name); return name.ToString(); } //This is a mapping table from hex to descriptive name public static String MapLayoutName(string code = null) { if (code == null) code = GetLayoutCode(); switch (code) { case "0000041C": return "Albanian"; case "00000401": return "Arabic (101)"; case "00010401": return "Arabic (102)"; case "00020401": return "Arabic (102) Azerty"; case "0000042B": return "Armenian eastern"; case "0001042B": return "Armenian Western"; case "0000044D": return "Assamese - inscript"; case "0000082C": return "Azeri Cyrillic"; case "0000042C": return "Azeri Latin"; case "0000046D": return "Bashkir"; case "00000423": return "Belarusian"; case "0000080C": return "Belgian French"; case "00000813": return "Belgian (period)"; case "0001080C": return "Belgian (comma)"; case "00000445": return "Bengali"; case "00010445": return "Bengali - inscript (legacy)"; case "00020445": return "Bengali - inscript"; case "0000201A": return "Bosnian (cyrillic)"; case "00030402": return "Bulgarian"; case "00000402": return "Bulgarian(typewriter)"; case "00010402": return "Bulgarian (latin)"; case "00020402": return "Bulgarian (phonetic)"; case "00040402": return "Bulgarian (phonetic traditional)"; case "00011009": return "Canada Multilingual"; case "00001009": return "Canada French"; case "00000C0C": return "Canada French (legacy)"; case "00000404": return "Chinese (traditional) - us keyboard"; case "00000804": return "Chinese (simplified) -us keyboard"; case "00000C04": return "Chinese (traditional, hong kong sar) - us keyboard"; case "00001004": return "Chinese (simplified, singapore) - us keyboard"; case "00001404": return "Chinese (traditional, macao sar) - us keyboard"; case "00000405": return "Czech"; case "00020405": return "Czech programmers"; case "00010405": return "Czech (qwerty)"; case "0000041A": return "Croatian"; case "00000439": return "Deanagari - inscript"; case "00000406": return "Danish"; case "00000465": return "Divehi phonetic"; case "00010465": return "Divehi typewriter"; case "00000413": return "Dutch"; case "00000425": return "Estonian"; case "00000438": return "Faeroese"; case "0000040B": return "Finnish"; case "0001083B": return "Finnish with sami"; case "0000040C": return "French"; case "00011809": return "Gaelic"; case "00000437": return "Georgian"; case "00020437": return "Georgian (ergonomic)"; case "00010437": return "Georgian (qwerty)"; case "00000407": return "German"; case "00010407": return "German (ibm)"; case "0000046F": return "Greenlandic"; case "00000468": return "Hausa"; case "0000040D": return "Hebrew"; case "00010439": return "Hindi traditional"; case "00000408": return "Greek"; case "00010408": return "Greek (220)"; case "00030408": return "Greek (220) latin"; case "00020408": return "Greek (319)"; case "00040408": return "Greek (319) latin"; case "00050408": return "Greek latin"; case "00060408": return "Greek polyonic"; case "00000447": return "Gujarati"; case "0000040E": return "Hungarian"; case "0001040E": return "Hungarian 101 key"; case "0000040F": return "Icelandic"; case "00000470": return "Igbo"; case "0000085D": return "Inuktitut - latin"; case "0001045D": return "Inuktitut - naqittaut"; case "00001809": return "Irish"; case "00000410": return "Italian"; case "00010410": return "Italian (142)"; case "00000411": return "Japanese"; case "0000044B": return "Kannada"; case "0000043F": return "Kazakh"; case "00000453": return "Khmer"; case "00000412": return "Korean"; case "00000440": return "Kyrgyz cyrillic"; case "00000454": return "Lao"; case "0000080A": return "Latin america"; case "00000426": return "Latvian"; case "00010426": return "Latvian (qwerty)"; case "00010427": return "Lithuanian"; case "00000427": return "Lithuanian ibm"; case "00020427": return "Lithuanian standard"; case "0000046E": return "Luxembourgish"; case "0000042F": return "Macedonian (fyrom)"; case "0001042F": return "Macedonian (fyrom) - standard"; case "0000044C": return "Malayalam"; case "0000043A": return "Maltese 47-key"; case "0001043A": return "Maltese 48-key"; case "0000044E": return "Marathi"; case "00000481": return "Maroi"; case "00000450": return "Mongolian cyrillic"; case "00000850": return "Mongolian (mongolian script)"; case "00000461": return "Nepali"; case "00000414": return "Norwegian"; case "0000043B": return "Norwegian with sami"; case "00000448": return "Oriya"; case "00000463": return "Pashto (afghanistan)"; case "00000429": return "Persian"; case "00000415": return "Polish (programmers)"; case "00010415": return "Polish (214)"; case "00000816": return "Portuguese"; case "00000416": return "Portuguese (brazillian abnt)"; case "00010416": return "Portuguese (brazillian abnt2)"; case "00000446": return "Punjabi"; case "00010418": return "Romanian (standard)"; case "00000418": return "Romanian (legacy)"; case "00020418": return "Romanian (programmers)"; case "00000419": return "Russian"; case "00010419": return "Russian (typewriter)"; case "0002083B": return "Sami extended finland-sweden"; case "0001043B": return "Sami extended norway"; case "00000C1A": return "Serbian (cyrillic)"; case "0000081A": return "Serbian (latin)"; case "0000046C": return "Sesotho sa Leboa"; case "00000432": return "Setswana"; case "0000045B": return "Sinhala"; case "0001045B": return "Sinhala -Wij 9"; case "0000041B": return "Slovak"; case "0001041B": return "Slovak (qwerty)"; case "00000424": return "Slovenian"; case "0001042E": return "Sorbian extended"; case "0002042E": return "Sorbian standard"; case "0000042E": return "Sorbian standard (legacy)"; case "0000040A": return "Spanish"; case "0001040A": return "Spanish variation"; case "0000041D": return "Swedish"; case "0000083B": return "Swedish with sami"; case "00000807": return "Swiss german"; case "0000100C": return "Swiss french"; case "0000045A": return "Syriac"; case "0001045A": return "Syriac phonetic"; case "00000428": return "Tajik"; case "00000449": return "Tamil"; case "00000444": return "Tatar"; case "0000044A": return "Telugu"; case "0000041E": return "Thai Kedmanee"; case "0002041E": return "Thai Kedmanee (non-shiftlock)"; case "0001041E": return "Thai Pattachote"; case "0003041E": return "Thai Pattachote (non-shiftlock)"; case "00000451": return "Tibetan (prc)"; case "0001041F": return "Turkish F"; case "0000041F": return "Turkish Q"; case "00000442": return "Turkmen"; case "00000422": return "Ukrainian"; case "00020422": return "Ukrainian (enhanced)"; case "00000809": return "United Kingdom"; case "00000452": return "United Kingdom Extended"; case "00000409": return "United States"; case "00010409": return "United States - dvorak"; case "00030409": return "United States - dvorak left hand"; case "00050409": return "United States - dvorak right hand"; case "00004009": return "United States - india"; case "00020409": return "United States - international"; case "00000420": return "Urdu"; case "00010480": return "Uyghur"; case "00000480": return "Uyghur (legacy)"; case "00000843": return "Uzbek cyrillic"; case "0000042A": return "Vietnamese"; case "00000485": return "Yakut"; case "0000046A": return "Yoruba"; case "00000488": return "Wolof"; default: return "unknown"; } } } }