Generating Your Own Keyboard Layout
Это содержимое пока не доступно на вашем языке.
If none of the existing layouts fits the way you type, a computer can help you design your own. What do you type most often, and what would you like to feel easier? Your initial answers give the computer a place to start searching, and the layouts it finds make the trade-offs easier to see. As you compare them, you may discover that something you expected to matter doesn’t, or that a different benefit catches your attention. Each round gives you a clearer sense of what you want, and brings you closer to a layout you’d be happy to use every day.
Scoring Function
Section titled “Scoring Function”To compare layouts, you need a way to judge how well each one balances your priorities. A scoring function combines two kinds of measurements into a single score: n-gram-based metrics and aggregate-based metrics.
N-gram-based metrics calculate part of the score from short sequences of keystrokes. For example, a same-finger bigram counts two consecutive presses on different keys by the same finger.
Aggregate-based metrics contribute to the score based on totals across the corpus. For example, a soft constraint can penalize pinky usage above a chosen share of all presses. The penalty depends on the total share, so no single n-gram can determine it.
Balancing the factors in a scoring function takes trial and error. The layouts a search produces show what the current weights reward and what they allow the search to give up. Compare the layouts and their metric scores. Decide which metrics need more weight and which are driving too much of the result. Adjust the weights a little, then generate another set of layouts. Each round builds a feel for how the weights shape the layouts you get.
Corpus and Alphabet
Section titled “Corpus and Alphabet”A corpus is the sample of text used to score layouts. Linguists collect large samples to study how people use a language; those samples can also represent what people type. The closest fit is a corpus in the language or languages you use, with text in the style you usually type. The Leipzig Corpora Collection, ConvoKit, and Tatoeba offer different kinds of text. A sample of source code, for example, represents programming better than a collection of example sentences does.
The alphabet defines which characters the analysis includes. It can reflect what matters to type quickly and without pausing: letters and the punctuation you use every day, perhaps, rather than every symbol the layout can produce. For programming and other specialized work, characters that are rare in ordinary prose can be common enough to matter. The corpus and alphabet work together: one represents what gets typed, the other decides what counts in the analysis.
A corpus built from your own keystrokes gives the most direct picture of what you actually type. It can capture your writing habits and things that public text leaves out, such as how you navigate in Neovim, Emacs, or Helix. That fidelity also makes the data sensitive. A log can contain passwords, private messages, or confidential work. In some cases, even derived n-gram counts may reveal enough to reconstruct parts of a password or identifying information. A short recording limited to a specific app reduces what the corpus can expose.
Letter Arrangement
Section titled “Letter Arrangement”The simplest form of layout optimization is finding the arrangement of letters on a key grid that achieves the best score. Carpalx first documented this approach in the context of keyboard layouts. Oxeylyzer is a modern, performant implementation.
Normalize the text as needed, for example by converting all letters to lowercase, then count its character n-grams. You only need to process a corpus once for a given normalization, alphabet, and maximum n-gram width.
For each layout, assign each letter to a key and each key to a finger. Map each corpus n-gram to its sequence of keys and fingers, then calculate the score using the saved counts.
Simulated Annealing
Section titled “Simulated Annealing”Simulated annealing needs two things from a layout designer: a scoring function and a way to propose a change to a layout. For a grid of letters, a move can swap two letters or cycle several. The designer defines which moves are legal. For example, letters used in familiar shortcuts can stay in place while the rest move. The algorithm uses the score to guide a series of changes through the possible arrangements, looking for layouts that better match the designer’s priorities. Wikipedia’s simulated annealing pseudocode provides a starting point for implementation.
Annealing parameters depend on the scale of the score and how long a run can take. Start with a temperature that accepts some of the worse moves seen in a short trial. The cooling rate determines how quickly those moves become less likely, and the iteration budget determines how many candidates the search can try. Compare runs from several starting layouts to see whether slower cooling or more iterations consistently improves the best score.
Incremental Scoring
Section titled “Incremental Scoring”When you score a new candidate, calculate only what changed. Swapping two letters changes the contributions of only the n-grams that contain either letter. Subtract their old contributions and add their new ones, counting each affected n-gram once. This saves a full pass over the corpus n-grams for every candidate.
Cheap totals such as pinky usage can still be recalculated from the unigram counts.
Performance
Section titled “Performance”For a fixed alphabet and a score based on patterns up to trigrams, a well-optimized native implementation with a tuned annealing schedule can often find the global optimum in minutes to hours on a modern consumer machine. That makes it fast enough to test different scoring functions and constraints. The resulting layouts show whether a score rewards the qualities you want, and whether the tradeoffs you’re aiming for are possible within the search space.
Corpus Rewriting
Section titled “Corpus Rewriting”Rewriting the corpus lets an optimizer score presses for repeat and one-shot Shift. On Magic Sturdy, “book” can use
As long as the rewrite rules stay fixed, you can rewrite the corpus and count its n-grams once. Simulated annealing can then reuse those counts across many candidate layouts.
Chogalyzer is one example. A separate tool can also rewrite the corpus before passing it to any layout optimizer that can treat the replacement symbols as assignable keys.
Extended N-grams
Section titled “Extended N-grams”Extended n-grams let the search try new magic rules without replaying the corpus for every candidate. For rules with one letter of context and one letter of output, count four-letter windows once. For each candidate, use the first letter as context, apply its magic rules within the window, and score the final three keystrokes. Weight each result by the window’s count in the corpus.
Scoring each window independently is an approximation. When two rules could chain, overlapping windows can make different choices about which letter magic produces and count keystroke patterns that cannot coexist in a single typing of the text. The frequency of possible chains helps gauge how often this can happen. A bound on score error also depends on how many scored windows a chain can affect and how much those windows contribute to the score.
Surrogate Models
Section titled “Surrogate Models”When the question you want to answer requires modeling more complex key behavior, an exact evaluation can be too slow for simulated annealing, which needs to score millions of candidates to converge on a global optimum. In these cases, you’ll need to use a surrogate model. For layout search, that means a custom scoring kernel that takes enough shortcuts to make the search fast while still finding the strongest candidates.
The usual performance techniques apply: compile to native code, run independent searches in parallel, and keep allocations, hashes, and locks out of the scoring loop.
To check that the speedup still leads you to the best layouts, compare the surrogate’s scores and rankings with a reference analyzer such as Keygen Max. These comparisons show how large the errors are and whether the surrogate systematically favors certain candidates. Refine the model where those errors affect the answer, then score the finalists with the reference analyzer.