GetKeyboardLayout function (Windows)

Switch View :
ScriptFree
GetKeyboardLayout function

Applies to: desktop apps only

Retrieves the active input locale identifier (formerly called the keyboard layout) for the specified thread. If the idThread parameter is zero, the input locale identifier for the active thread is returned.

Syntax

HKL WINAPI GetKeyboardLayout(
  __in  DWORD idThread
);

Parameters

idThread [in]

Type: DWORD

The identifier of the thread to query, or 0 for the current thread.

Return value

Type: HKL

The return value is the input locale identifier for the thread. The low word contains a Language Identifier for the input language and the high word contains a device handle to the physical layout of the keyboard.

Remarks

The input locale identifier is a broader concept than a keyboard layout, since it can also encompass a speech-to-text converter, an Input Method Editor (IME), or any other form of input.

Since the keyboard layout can be dynamically changed, applications that cache information about the current keyboard layout should process the WM_INPUTLANGCHANGE message to be informed of changes in the input language.

To get the KLID (keyboard layout ID) of the currently active HKL, call the GetKeyboardLayoutName. To find out if a particular KLID is loaded, call GetKeyboardLayoutList, load the keyboard with LoadKeyboardLayout, and then check to see if the HKL is the same as the one already loaded. If not, unload it after.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

Library

User32.lib

DLL

User32.dll

See also

Reference
ActivateKeyboardLayout
LoadKeyboardLayout
WM_INPUTLANGCHANGE
Conceptual
Keyboard Input
Other Resources
CreateThread

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Community Content

DrTusk
HKL type is wrong?
Why does this return a void* type? It should be  DWORD

Artem U
Sybase PowerBuilder syntax
FUNCTION Long GetKeyboardLayout(uLong dwLayout ) LIBRARY "USER32.DLL"

sergeyt2
Pass Thread ID instead of Process ID
Why do you all pass processID to GetKeyboardLayout? Please, read the descriptin of the function above and pay attention: it accepts id of a thread, not id of a process !

ijavid
ERROR_INVALID_HANDLE
Error while getting the active window's keyboard layout via function: $0 $0staticpublicIntPtr getActiveKeyBoardLayout()
   
{            
       
int handle =0;
        handle
=GetForegroundWindow();

       
IntPtr i =newIntPtr(handle);
       
HandleRef hr =newHandleRef(wrapper, i);
       
int pid;
       
GetWindowThreadProcessId(hr,out pid);

       
IntPtr layout =GetKeyboardLayout(pid);

       
int er =Marshal.GetLastWin32Error();
       
if(er >0)
       
{
           
System.Console.Out.WriteLine("error "+ er.ToString());
       
}

       
return layout;
   
}
$0 $0

dmex
C# syntax
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern IntPtr GetKeyboardLayout(int dwLayout);

dmex
vb.net syntax
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Public Shared Function GetKeyboardLayout(ByVal dwLayout As Integer) As IntPtr
End Function