I thought there was an easy way to get the layout in the form "AZERTY" or "QWERTY", but apparently there isn't... However, you can cheat by requesting the names of the keys from their scan code. This code returns "AZERTY", "QWERTY" or "QWERTZ", depending on the current layout:
private static string GetKeyboardLayout()
{
var layout = new StringBuilder();
var buffer = new StringBuilder(64);
for (int i = 0; i < 6; i++)
{
int scanCode = 0x10 + i;
int lParam = scanCode << 16;
GetKeyNameText(lParam, buffer, buffer.Capacity);
layout.Append(buffer.ToString());
}
return layout.ToString();
}
[DllImport("user32.dll")]
private static extern int GetKeyNameText(int lParam, StringBuilder lpString, int cchSize);
But of course, it won't work for Dvorak keyboards, since the layout doesn't actually spell "DVORAK"... (and I suspect the returned value would be meaningless for non-latin keyboards too)