-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathkeyboard_layout_manager.py
More file actions
62 lines (51 loc) · 2.18 KB
/
keyboard_layout_manager.py
File metadata and controls
62 lines (51 loc) · 2.18 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
import json
import os
import sys
from typing import Dict, Optional
import keyboard
class KeyboardLayoutManager:
def __init__(self, layout_name: str, base_path: str = "resources/keyboards"):
self.base_path = self._resolve_base_path(base_path)
self.default_layout = "azerty_fr"
self.layout_name = layout_name or self.default_layout
self.layout_map = self._load_layout(self.layout_name)
self.scan_to_key = {scan: key for key, scan in self.layout_map.items()}
def _resolve_base_path(self, relative_path: str) -> str:
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative_path)
return relative_path
def _load_layout(self, layout_name: str) -> Dict[str, int]:
file_path = os.path.join(self.base_path, f"{layout_name}.yml")
if not os.path.exists(file_path):
file_path = os.path.join(self.base_path, f"{self.default_layout}.yml")
try:
with open(file_path, "r", encoding="utf-8") as stream:
data = json.load(stream)
except Exception:
data = {}
keys = data.get("keys", {})
normalized = {}
for key, value in keys.items():
try:
normalized[str(key).lower().strip()] = int(value)
except Exception:
continue
return normalized
def set_layout(self, layout_name: str) -> None:
self.layout_name = layout_name or self.default_layout
self.layout_map = self._load_layout(self.layout_name)
self.scan_to_key = {scan: key for key, scan in self.layout_map.items()}
def key_to_scan(self, key_name: str) -> Optional[int]:
if not key_name:
return None
return self.layout_map.get(key_name.lower().strip())
def scan_to_key_name(self, scan_code: int) -> Optional[str]:
return self.scan_to_key.get(scan_code)
def resolve_scan_code(self, token: str) -> Optional[int]:
scan = self.key_to_scan(token)
if scan is not None:
return scan
try:
return keyboard.key_to_scan_codes(token)[0]
except Exception:
return None