GooseUI

Components

useKeyboardLayout

New

useKeyboardLayout notices when text was typed with the wrong layout switched on, Russian (JCUKEN) instead of English (QWERTY) or the other way round, and offers the corrected text. "ghbdtn" turns into the Russian word for "hello". The conversion and the check are pure functions you can use without React.

Preview

Pick a sample or type with the wrong layout. The hint appears when most words look mistyped.

Field expects:
Samples:
Waiting for input

Installation

npx shadcn@latest add @gooseui/use-keyboard-layout
pnpm dlx shadcn@latest add @gooseui/use-keyboard-layout
yarn dlx shadcn@latest add @gooseui/use-keyboard-layout
bunx --bun shadcn@latest add @gooseui/use-keyboard-layout

Usage

import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

import { useKeyboardLayout } from "@/hooks/use-keyboard-layout"

export function NameField() {
  const [value, setValue] = useState("")
  const { mismatch, suggestion, fix } = useKeyboardLayout(value, {
    expected: "ru",
    onFix: setValue,
  })

  return (
    <div>
      <Input value={value} onChange={(e) => setValue(e.target.value)} />
      {mismatch && (
        <Button type="button" onClick={fix}>
          Did you mean {suggestion}?
        </Button>
      )}
    </div>
  )
}

Examples

Plain conversion

convertLayout maps every character to the same key on the other layout, with no guessing. Shift characters are covered too: ? on one layout is , on the other.

Result:
import { convertLayout } from "@/hooks/use-keyboard-layout"

convertLayout("ghbdtn", "ru") // the Russian word for "hello"
convertLayout(input, "en")    // Cyrillic keys back to QWERTY

Fix on blur

For search boxes you can fix the query quietly when the field loses focus, and keep the original in case the user wants it back.

import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"

const [query, setQuery] = useState("")
const [original, setOriginal] = useState<string | null>(null)
const { mismatch, fix } = useKeyboardLayout(query, {
  onFix: (fixed) => {
    setOriginal(query)
    setQuery(fixed)
  },
})

<Input
  value={query}
  onChange={(e) => setQuery(e.target.value)}
  onBlur={() => mismatch && fix()}
/>
{original && (
  <Button type="button" onClick={() => setQuery(original)}>
    Search for "{original}" instead
  </Button>
)}

Check without the hook

detectLayoutMismatch converts only the words that look mistyped and leaves the rest alone, so mixed text keeps its real English words.

import { detectLayoutMismatch } from "@/hooks/use-keyboard-layout"

const result = detectLayoutMismatch("ghbdtn vbh", "ru")
// result.mismatch → true
// result.from     → "en"
// result.fixed    → the same phrase on the Russian layout

detectLayoutMismatch("hello", "ru").mismatch
// → false: "hello" is a real word, it stays as is

Parameters

NameTypeDefaultDescription
valuestring-Current text of the field
options.expected"en" | "ru"-Layout the field expects. Only words in the other script are checked. Omit to check both directions
options.minLengthnumber2Ignore text with fewer letters, so one key press does not trigger a hint
options.onFix(fixed: string) => void-Receives the corrected text when fix() is called

Returns

NameTypeDescription
mismatchbooleanMost words look typed on the wrong layout
suggestionstring | nullCorrected text, or null when there is nothing to fix
typedLayout"en" | "ru" | nullLayout the text was actually typed on
fix() => stringSends the suggestion to onFix and returns it

Functions

convertLayout(text, to)Convert every character to the other layout
detectLayoutMismatch(text, expected?) → { mismatch, fixed, from }Check the text and build the corrected version
detectLayout(text)Which script dominates the text, or null without letters

How it decides

Each word gets a score for how unlike a real word it looks: no vowels, long consonant runs, letter pairs that never occur, punctuation between letters. If the converted word scores better than the original, the word counts as mistyped. The text is flagged when more than half of the checked words are.

Without a dictionary, short words that make sense on both layouts can slip through. That is the price of a few kilobytes instead of a word list.