-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwindows.py
More file actions
109 lines (97 loc) · 4.02 KB
/
windows.py
File metadata and controls
109 lines (97 loc) · 4.02 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
#Author-Thomas Axelsson
#Description-Lists all keyboard shortcuts
# This file is part of KeyboardShortcutsSimple, a Fusion 360 add-in for naming
# features directly after creation.
#
# Copyright (C) 2020 Thomas Axelsson
#
# KeyboardShortcutsSimple is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# KeyboardShortcutsSimple is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with KeyboardShortcutsSimple. If not, see <https://www.gnu.org/licenses/>.
# Platform-specific code
import ctypes
import pathlib
# Virtual Keycodes are available here:
# https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
FUSION_VK_MAPPING = { 'Slash': ord('/'),
'Equal': ord('='),
'Question': ord('?'),
'backspace': 0x08,
'delete': 0x2e,
'escape': 0x1b,
'return': 0x0d,
'space': 0x20,
'f1': 0x70,
'f2': 0x71,
'f3': 0x72,
'f4': 0x73,
'f5': 0x74,
'f6': 0x75,
'f7': 0x76,
'f8': 0x77,
'f9': 0x78,
'f10': 0x79,
'f11': 0x7a,
'f12': 0x7b,
'f13': 0x7c,
'f14': 0x7d,
'f15': 0x7e,
'f16': 0x7f,
'f17': 0x80,
'f18': 0x81,
'f19': 0x82,
'f20': 0x83,
'f21': 0x84,
'f22': 0x85,
'f23': 0x86,
'f24': 0x87,
}
def fusion_key_to_keyboard_key(key_sequence):
keys = key_sequence.split('+')
vk, shift_state = fusion_key_to_vk(keys[-1])
# Get the scancode from the virtual key and then get the label from the scan code
# The scan code represents the actual physical key
MAPVK_VK_TO_VSC_EX = 4
keyname_buf = ctypes.create_unicode_buffer(32)
input_locale = ctypes.windll.User32.GetKeyboardLayout(0)
scan_code = ctypes.windll.User32.MapVirtualKeyExW(vk, MAPVK_VK_TO_VSC_EX, input_locale)
ret = ctypes.windll.User32.GetKeyNameTextW(scan_code << 16, keyname_buf, ctypes.sizeof(keyname_buf))
if ret > 0:
label = keyname_buf.value
else:
label = keys[-1]
print(f"Failed to get name for vk 0x{vk:x}: {ctypes.GetLastError()}")
keys[-1] = label
return '+'.join(keys), label
def fusion_key_to_vk(fusion_key):
if len(fusion_key) == 1:
char = ord(fusion_key)
else:
try:
char = FUSION_VK_MAPPING[fusion_key]
except KeyError:
print(f"No keyboard mapping for \"{fusion_key}\". Ignoring.")
return None, None
ret = ctypes.windll.User32.VkKeyScanW(char)
keycode = ret & 0xff
shift_state = ret >> 8
return keycode, shift_state
def find_options_file(app):
CSIDL_APPDATA = 26
SHGFP_TYPE_CURRENT = 0
# SHGetFolderPath is deprecated, but SHGetKnownFolderPath is much more cumbersome to use.
# Could just use win32com in that case, to simplify things.
roaming_path_buf = ctypes.create_unicode_buffer(ctypes.wintypes.MAX_PATH)
ctypes.windll.shell32.SHGetFolderPathW(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, roaming_path_buf)
roaming_path = pathlib.Path(roaming_path_buf.value)
options_path = roaming_path / 'Autodesk' / 'Neutron Platform' / 'Options' / app.userId / 'NGlobalOptions.xml'
return options_path