Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow for Name overrides in key picker #125

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/hid-usage-name-overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"35": { "short": "6" },
"36": { "short": "7" },
"37": { "short": "8" },
"38": { "short": "9" },
"39": { "short": "0" },
"40": { "short": "Ret", "med": "Return" },
"38": { "short": "9", "Name":"Keyboard 9 and Left Parenthesis"},
"39": { "short": "0", "Name":"Keyboard 9 and Right Parenthesis"},
"40": { "short": "Ret", "med": "Return", "Name":"Keyboard Return/Enter"},
"41": { "short": "Esc", "long": "Escape" },
"42": { "short": "BkSp", "med": "BkSpc", "long": "Backspace" },
"42": { "short": "BkSp", "med": "BkSpc", "long": "Backspace", "Name": "Keyboard Backspace"},
"44": { "short": "␣", "med": "Space" },
"45": { "short": "-", "med": "Dash" },
"46": { "short": "=", "med": "Equals" },
"47": { "short": "{" },
"48": { "short": "}" },
"47": { "short": "[", "Name": "Keyboard Left Bracket and Brace" },
"48": { "short": "]", "Name": "Keyboard Right Bracket and Brace" },
"49": { "short": "\\" },
"50": { "short": "NUHS", "long": "NonUS Hash" },
"51": { "short": ";" },
Expand Down Expand Up @@ -60,15 +60,16 @@
"100": { "short": "NUBS" },
"103": { "short": "=" },
"133": { "short": "," },
"158": { "Name": "Keyboard Non-PC Return"},
"134": { "short": "=" },
"176": { "short": "00" },
"177": { "short": "000" },
"224": { "short": "Ctrl", "med": "L Ctrl" },
"225": { "short": "Shft", "med": "L Shft", "long": "L Shift" },
"225": { "short": "Shft", "med": "L Shft", "long": "L Shift" },
"226": { "short": "Alt", "med": "L Alt", "long": "Left Alt" },
"227": { "short": "GUI", "med": "L GUI", "long": "Left GUI" },
"228": { "short": "Ctrl", "med": "R Ctrl" },
"229": { "short": "Shft", "med": "R Shft", "long": "R Shift" },
"229": { "short": "Shft", "med": "R Shft", "long": "R Shift" },
"230": { "short": "AltG", "med": "AltGr" },
"231": { "short": "GUI", "med": "R GUI", "long": "Right GUI" }
},
Expand Down
30 changes: 21 additions & 9 deletions src/hid-usages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface HidLabels {
short?: string;
med?: string;
long?: string;
Name?: string;
}

const overrides: Record<string, Record<string, HidLabels>> = HidOverrides;
Expand All @@ -30,16 +31,27 @@ export const hid_usage_page_and_id_from_usage = (

export const hid_usage_page_get_ids = (
usage_page: number
): UsagePageInfo | undefined => UsagePages.find((p) => p.Id === usage_page);
): UsagePageInfo | undefined => {

export const hid_usage_get_label = (
usage_page: number,
usage_id: number
): string | undefined =>
overrides[usage_page.toString()]?.[usage_id.toString()]?.short ||
UsagePages.find((p) => p.Id === usage_page)?.UsageIds?.find(
(u) => u.Id === usage_id
)?.Name;
// Fetch the relevant usage page from the core keyboard/consumer usage tables
const usagePageInfo = UsagePages.find((p) => p.Id === usage_page);

// Filter overrides to include only entries with a valid override key of Name
const filteredOverrides = Object.fromEntries(
Object.entries(overrides[usage_page])?.filter(
([_, overrideData]) => overrideData.Name
)
);

// Mutate the usagePageInfo with the discovered "Name" overrides
for (const key in filteredOverrides) {
const usageId = usagePageInfo?.UsageIds.find((p) => p.Id === parseInt(key));
if (usageId) {
usageId.Name = filteredOverrides[key].Name as string;
}
}
return usagePageInfo;
};

export const hid_usage_get_labels = (
usage_page: number,
Expand Down