-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardLayoutDetector.cs
More file actions
59 lines (50 loc) · 2.15 KB
/
Copy pathKeyboardLayoutDetector.cs
File metadata and controls
59 lines (50 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
public class KeyboardLayoutDetector : MonoBehaviour
{
private const int KL_NAMELENGTH = 9;
[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);
[DllImport("user32.dll")]
private static extern int GetKeyboardLayoutName([Out] StringBuilder pwszKLID);
private Dictionary<string, string> keyboardLayouts = new Dictionary<string, string>
{
{ "00000409", "QWERTY" }, // English - United States
{ "00000809", "QWERTY" }, // English - United Kingdom
{ "0000040C", "AZERTY" }, // French - France
{ "0000080C", "AZERTY" }, // Belgian (Period) - Belgium
{ "00000407", "QWERTZ" }, // German - Germany
{ "00000408", "Greek" }, // Greek
{ "0000040E", "Hungarian" }, // Hungarian
{ "0000040A", "Spanish" }, // Spanish - Spain
{ "0000040D", "Hebrew" }, // Hebrew
{ "00000419", "JCUKEN" }, // Russian - Russia
{ "00000411", "Hiragana/Katakana" }, // Japanese - Japan
{ "00000412", "Hangul" }, // Korean - Korea
{ "00000404", "Bopomofo" }, // Chinese (Traditional) - Taiwan
};
void Start() => DetectKeyboardLayout();
void DetectKeyboardLayout()
{
StringBuilder name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
string layoutId = name.ToString();
Debug.Log("Layout ID: " + layoutId);
if (keyboardLayouts.TryGetValue(layoutId, out string layoutName))
Debug.Log("Keyboard detected : " + layoutName);
else
Debug.Log("Unrecognized keyboard layout");
}
void OnGUI()
{
StringBuilder name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(name);
string layoutId = name.ToString();
string layout = keyboardLayouts.ContainsKey(layoutId) ? keyboardLayouts[layoutId] : "UNKNOWN";
GUI.Label(new Rect(10, 10, 200, 20), "Keyboard : " + layout);
GUI.Label(new Rect(10, 30, 200, 20), "Keyboard ID : " + layoutId);
}
}