-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMacintoshVersionNotTested
More file actions
87 lines (73 loc) · 3.33 KB
/
Copy pathMacintoshVersionNotTested
File metadata and controls
87 lines (73 loc) · 3.33 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
public class KeyboardLayoutDetector : MonoBehaviour
{
private const int KL_NAMELENGTH = 256; // macOS uses longer layout names
[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
private static extern IntPtr GetKeyboardLayoutInternal();
[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
private static extern void GetIndCLayout([In, Out] IntPtr curKeyboardLayout);
private Dictionary<string, string> keyboardLayouts = new Dictionary<string, string>
{
{ "com.apple.keylayout.US", "QWERTY" }, // English - United States
{ "com.apple.keylayout.UK", "QWERTY" }, // English - United Kingdom
{ "com.apple.keylayout.French", "AZERTY" }, // French - France
{ "com.apple.keylayout.CanadianFrench", "AZERTY" }, // Canadian French
{ "com.apple.keylayout.German", "QWERTZ" }, // German - Germany
{ "com.apple.keylayout.Greek", "Greek" }, // Greek
{ "com.apple.keylayout.Hungarian", "Hungarian" }, // Hungarian
{ "com.apple.keylayout.Spanish", "Spanish" }, // Spanish - Spain
{ "com.apple.keylayout.Hebrew", "Hebrew" }, // Hebrew
{ "com.apple.keylayout.Russian", "JCUKEN" }, // Russian - Russia
{ "com.apple.keylayout.Japanese", "Hiragana/Katakana" }, // Japanese - Japan
{ "com.apple.keylayout.Korean", "Hangul" }, // Korean - Korea
{ "com.apple.keylayout.Chinese", "Bopomofo" }, // Chinese (Traditional) - Taiwan
};
void Start()
{
DetectKeyboardLayout();
}
void DetectKeyboardLayout()
{
IntPtr curKeyboardLayout = GetKeyboardLayoutInternal();
if (curKeyboardLayout == IntPtr.Zero)
{
Debug.LogError("Failed to retrieve current keyboard layout");
return;
}
GetIndCLayout(curKeyboardLayout);
StringBuilder name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(curKeyboardLayout, name);
string layoutId = name.ToString().Trim();
Debug.Log("Layout ID: " + layoutId);
if (keyboardLayouts.TryGetValue(layoutId, out string layoutName))
{
Debug.Log("Keyboard detected: " + layoutName);
}
else
{
Debug.Log("Keyboard layout not recognized");
}
}
void OnGUI()
{
IntPtr curKeyboardLayout = GetKeyboardLayoutInternal();
if (curKeyboardLayout == IntPtr.Zero)
{
Debug.LogError("Failed to retrieve current keyboard layout");
return;
}
GetIndCLayout(curKeyboardLayout);
StringBuilder name = new StringBuilder(KL_NAMELENGTH);
GetKeyboardLayoutName(curKeyboardLayout, name);
string layoutId = name.ToString().Trim();
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);
}
[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
private static extern void GetKeyboardLayoutName(IntPtr curKeyboardLayout, StringBuilder pwszKLID);
}