Skip to content

Instantly share code, notes, and snippets.

@ali1234
Created July 25, 2023 12:35
Show Gist options
  • Save ali1234/c785092870cd381d6d9fa705afe9b44c to your computer and use it in GitHub Desktop.
Save ali1234/c785092870cd381d6d9fa705afe9b44c to your computer and use it in GitHub Desktop.
OpenScad keyboard layout generator
/*
Keyboard data
*/
row_space = 2.1;
key_space = 1.8;
surround = 0.6;
key_colour = [0.1, 0.1, 0.1];
shell_colour = [0.5, 0.5, 0.5];
letter_colour = "white";
fn_colour = "orange";
/*
Data is a list of rows.
Each row is a height and a list of keys.
Each key is a list of labels for the key face, and a width.
*/
data = [
[8.6, [
[["Esc", "", "FLc", ""], 16.66],
[["F1", "", "F11", ""], 16.66],
[["F2", "", "F12", ""], 16.66],
[["F3", "", "\u266a", ""], 16.66],
[["F4", "", "\u23ee", ""], 16.66],
[["F5", "", "\u23ef", ""], 16.66],
[["F6", "", "\u23ed", ""], 16.66],
[["F7", "", "\U01f507", ""], 16.66],
[["F8", "", "\U01f508", ""], 16.66],
[["F9", "", "\U01f509", ""], 16.66],
[["F10", "", "SLc", ""], 16.66],
[["NumLc", "", "", ""], 16.66],
[["PsBrk", "", "", ""], 16.66],
[["Delete", "", "", ""], 16.66]
]],
[16, [
[["¬", "` ¦", "", ""], 16],
[["!", "1", "", ""], 16],
[["\"", "2", "", ""], 16],
[["£", "3", "", ""], 16],
[["$", "4", "", ""], 16],
[["%", "5", "", ""], 16],
[["^", "6", "", ""], 16],
[["&", "7", "7", ""], 16],
[["*", "8", "8", ""], 16],
[["(", "9", "9", ""], 16],
[[")", "0", "*", ""], 16],
[["_", "-", "", ""], 16],
[["+", "=", "", ""], 16],
[["Backspace", "\u2190", "", ""], 25.5] // \u232b
]],
[16, [
[["Tab", "\u2b7e", "", ""], 25.5],
[["Q", "", "", ""], 16],
[["W", "", "", ""], 16],
[["E", "", "", ""], 16],
[["R", "", "", ""], 16],
[["T", "", "", ""], 16],
[["Y", "", "", ""], 16],
[["U", "", "4", ""], 16],
[["I", "", "5", ""], 16],
[["O", "", "6", ""], 16],
[["P", "", "-", ""], 16],
[["{", "[", "", ""], 16],
[["}", "]", "", ""], 16],
[["~", "#", "", ""], 16]
]],
[16, [
[["Caps Lock", "\u21ea", "", ""], 30.3],
[["A", "", "", ""], 16],
[["S", "", "", ""], 16],
[["D", "", "", ""], 16],
[["F", "", "", ""], 16],
[["G", "", "", ""], 16],
[["H", "", "", ""], 16],
[["J", "", "1", ""], 16],
[["K", "", "2", ""], 16],
[["L", "", "3", ""], 16],
[[":", ";", "+", ""], 16],
[["@", "'", "", ""], 16],
[["Return", "\u21b5", "Enter", ""], 30.3] // \u23ce ⌤
]],
[16, [
//[["Shift", "\u21e7", "", ""], 39.8],
[["Shift", "\u21e7", "", ""], 20.8],
[["|", "\\", "", ""], 16],
[["Z", "", "", ""], 16],
[["X", "", "", ""], 16],
[["C", "", "", ""], 16],
[["V", "", "", ""], 16],
[["B", "", "", ""], 16],
[["N", "", "", ""], 16],
[["M", "", "0", ""], 16],
[["<", ",", "", ""], 16],
[[">", ".", ".", ""], 16],
[["?", "/", "/", ""], 16],
[["Shift", "\u21e7", "", ""], 39.8]
]],
[18, [
[["Ctrl", "^", "", ""], 16],
[["Fn", "", "", ""], 16],
[["Super", "\u2318", "", ""], 16],
[["Alt", "\u2325", "", ""], 21],
[["", "", "", ""], 89.6],
[["Alt", "\u2325", "", ""], 21],
[["Ctrl", "^", "", ""], 16],
[["", "\u2190", "Home", ""], 16.66],
[["\u2191", "\u2193", "PgUp", "PgDn"], 16.66],
[["", "\u2192", "End", ""], 16.66]
]],
];
/*
OpenScad keyboard layout generator
Alistair Buxton <a.j.buxton@gmail.com>
MIT license
*/
$fn=16;
include <config.scad>;
function calc_row_y(arr, space, _i=0, _acc=[]) =
_i==len(arr) ? _acc : calc_row_y(
arr, space, _i+1,
concat(
[_i==0 ? 0 : _acc[0]+space+arr[len(arr)-_i][0]], _acc
)
);
function calc_row_x(arr, space, _i=0, _acc=[]) =
_i==len(arr) ? _acc : calc_row_x(
arr, space, _i+1,
concat(
_acc, [_i==0 ? 0 : _acc[_i-1]+space+arr[_i-1][1]]
)
);
row_y = calc_row_y(data, row_space + (2*surround));
row_x = [for (r = data) calc_row_x(r[1], key_space + (2*surround))];
echo(row_x);
module key_outline() {
offset(1.5) offset(-1.5) square([$key_data[0][1], $key_data[1]]);
}
module key() {
x = 2.8;
difference() {
color(key_colour) linear_extrude(height=1.5) key_outline();
color($key_data[0][0][0] == "Fn" ? fn_colour : letter_colour) {
translate([x, $key_data[1]-x, 1.2]) linear_extrude(height=1, convexity=10) text($key_data[0][0][0], size=x, valign="top", font="VL Gothic:style=Regular");
translate([x, x, 1.2]) linear_extrude(height=1) text($key_data[0][0][1], size=x, valign="bottom", font="VL Gothic:style=Regular");
}
color(fn_colour) {
translate([$key_data[0][1]-x, $key_data[1]-x, 1.2]) linear_extrude(height=1) text($key_data[0][0][2], size=2.2, valign="top", halign="right", font="VL Gothic:style=Regular");
translate([$key_data[0][1]-x, x+0.4, 1.2]) linear_extrude(height=1) text($key_data[0][0][3], size=2.2, valign="bottom", halign="right", font="VL Gothic:style=Regular");
}
}
}
module make_keys() {
translate([6, 6]) {
for (r = [0:5]) {
y = row_y[r];
translate([0, y]) {
for (k = [0:len(data[r][1])-1]) {
$key_data = [data[r][1][k], data[r][0]];
translate([row_x[r][k], 0]) children();
}
}
}
}
}
translate([-142, -71]) {
color(shell_colour) translate([0, 0, -10]) linear_extrude(height=10) difference() {
offset(6) offset(-6) square([284, 122]);
offset(surround) make_keys() key_outline();
}
make_keys() key();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment