-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinuxVersionNotTested
More file actions
119 lines (99 loc) · 3.67 KB
/
Copy pathLinuxVersionNotTested
File metadata and controls
119 lines (99 loc) · 3.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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("X11")]
private static extern IntPtr XOpenDisplay(IntPtr display);
[DllImport("X11")]
private static extern IntPtr XkbAllocKeyboard();
[DllImport("X11")]
private static extern int XkbGetNames(IntPtr display, uint which, IntPtr xkb);
[DllImport("X11")]
private static extern void XkbFreeKeyboard(IntPtr xkb, uint which, bool freeAll);
private Dictionary<string, string> keyboardLayouts = new Dictionary<string, string>
{
{ "us", "QWERTY" }, // English - United States
{ "gb", "QWERTY" }, // English - United Kingdom
{ "fr", "AZERTY" }, // French - France
{ "be", "AZERTY" }, // Belgian (Period) - Belgium
{ "de", "QWERTZ" }, // German - Germany
{ "el", "Greek" }, // Greek
{ "hu", "Hungarian" }, // Hungarian
{ "es", "Spanish" }, // Spanish - Spain
{ "he", "Hebrew" }, // Hebrew
{ "ru", "JCUKEN" }, // Russian - Russia
{ "jp", "Hiragana/Katakana" }, // Japanese - Japan
{ "kr", "Hangul" }, // Korean - Korea
{ "tw", "Bopomofo" }, // Chinese (Traditional) - Taiwan
};
void Start()
{
DetectKeyboardLayout();
}
void DetectKeyboardLayout()
{
IntPtr display = XOpenDisplay(IntPtr.Zero);
if (display == IntPtr.Zero)
{
Debug.LogError("Failed to open X display");
return;
}
IntPtr xkb = XkbAllocKeyboard();
if (xkb == IntPtr.Zero)
{
Debug.LogError("Failed to allocate XKB keyboard");
return;
}
int result = XkbGetNames(display, 0x0100, xkb); // 0x0100 for XkbKeyNamesMask
if (result != 1)
{
Debug.LogError("Failed to get XKB names");
XkbFreeKeyboard(xkb, 0x0100, true);
return;
}
IntPtr namesPtr = Marshal.ReadIntPtr(xkb, 12); // Assuming layout name at offset 12
string layoutId = Marshal.PtrToStringAnsi(namesPtr).ToLower();
XkbFreeKeyboard(xkb, 0x0100, true);
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 display = XOpenDisplay(IntPtr.Zero);
if (display == IntPtr.Zero)
{
Debug.LogError("Failed to open X display");
return;
}
IntPtr xkb = XkbAllocKeyboard();
if (xkb == IntPtr.Zero)
{
Debug.LogError("Failed to allocate XKB keyboard");
return;
}
int result = XkbGetNames(display, 0x0100, xkb); // 0x0100 for XkbKeyNamesMask
if (result != 1)
{
Debug.LogError("Failed to get XKB names");
XkbFreeKeyboard(xkb, 0x0100, true);
return;
}
IntPtr namesPtr = Marshal.ReadIntPtr(xkb, 12); // Assuming layout name at offset 12
string layoutId = Marshal.PtrToStringAnsi(namesPtr).ToLower();
XkbFreeKeyboard(xkb, 0x0100, true);
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);
}
}