diff --git a/locales/en.yml b/locales/en.yml index c5557605..4ae5997b 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -4685,6 +4685,26 @@ tools: placeholder-your-url-to-parse: Your url to parse... url-must-have-http-or-https-prefix: Url must have http:// or https:// prefix button-open-url: Open URL + keyboard-tester: + instructions: + highlight: Pressed keys will be highlighted + press: Press any key on your keyboard to test it + reset: Click the "Reset" button to clear test records + tested: Tested keys will be shown in green + title: Instructions + keys: Keys + layout: 'Keyboard Layout:' + legend: + pressed: Pressed + tested: Tested + untested: Untested + progress: Test Progress + reset: Reset + size: 'Keyboard Size:' + sizes: + '60': 60% Compact + full: Full Size (100%) + tkl: TKL (80%) text-to-binary: title: Text to UTF-8 binary description: Convert text to its UTF-8 binary representation and vice-versa. diff --git a/src/tools/keyboard-tester/index.ts b/src/tools/keyboard-tester/index.ts new file mode 100644 index 00000000..820c92b0 --- /dev/null +++ b/src/tools/keyboard-tester/index.ts @@ -0,0 +1,26 @@ +import { defineTool } from '../tool'; + +export const tool = defineTool({ + name: 'Keyboard Tester', + path: '/keyboard-tester', + description: + 'Test your keyboard keys and check if all keys are working properly. Visual feedback for each key press with support for different keyboard layouts.', + keywords: [ + 'keyboard', + 'tester', + 'test', + 'keys', + 'layout', + 'mechanical', + 'typing', + 'qwerty', + 'azerty', + 'dvorak', + 'check', + 'working', + ], + component: () => import('./keyboard-tester.vue'), + icon: defineAsyncComponent(() => import('@vicons/tabler/es/Keyboard')), + createdAt: new Date('2026-07-11'), + category: 'Forensic', +}); diff --git a/src/tools/keyboard-tester/keyboard-tester.service.test.ts b/src/tools/keyboard-tester/keyboard-tester.service.test.ts new file mode 100644 index 00000000..770cb7b8 --- /dev/null +++ b/src/tools/keyboard-tester/keyboard-tester.service.test.ts @@ -0,0 +1,503 @@ +// oxlint-disable vitest/no-conditional-expect +import { describe, expect, it, beforeEach, vi } from 'vitest'; +import type { KeyDefinition, KeyboardLayout, KeyboardSize } from './keyboard-tester.service'; +import { + KeyboardLayoutEnum, + KeyboardSizeEnum, + getKeyboardLayout, + getKeyLabel, + getGridColumns, + getGridRows, +} from './keyboard-tester.service'; + +describe('keyboard-tester service', () => { + describe('getKeyboardLayout', () => { + it('should return QWERTY full keyboard layout', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + expect(result).toBeDefined(); + expect(result.length).toBeGreaterThan(0); + + // Should include function keys + expect(result.some((key) => key.code === 'F1')).toBe(true); + expect(result.some((key) => key.code === 'F12')).toBe(true); + + // Should include main keys + expect(result.some((key) => key.code === 'KeyQ')).toBe(true); + expect(result.some((key) => key.code === 'KeyA')).toBe(true); + + // Should include navigation keys + expect(result.some((key) => key.code === 'ArrowUp')).toBe(true); + expect(result.some((key) => key.code === 'Home')).toBe(true); + + // Should include numpad keys + expect(result.some((key) => key.code === 'Numpad0')).toBe(true); + expect(result.some((key) => key.code === 'NumLock')).toBe(true); + }); + + it('should return QWERTY TKL keyboard layout', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.TKL); + + expect(result).toBeDefined(); + + // Should include function keys + expect(result.some((key) => key.code === 'F1')).toBe(true); + + // Should include navigation keys + expect(result.some((key) => key.code === 'ArrowUp')).toBe(true); + + // Should NOT include numpad keys + expect(result.some((key) => key.code === 'Numpad0')).toBe(false); + expect(result.some((key) => key.code === 'NumLock')).toBe(false); + }); + + it('should return QWERTY 60% keyboard layout', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.COMPACT_60); + + expect(result).toBeDefined(); + + // Should NOT include function keys + expect(result.some((key) => key.code === 'F1')).toBe(false); + + // Should NOT include navigation keys + expect(result.some((key) => key.code === 'ArrowUp')).toBe(false); + + // Should NOT include numpad keys + expect(result.some((key) => key.code === 'Numpad0')).toBe(false); + + // Should include main keys + expect(result.some((key) => key.code === 'KeyQ')).toBe(true); + expect(result.some((key) => key.code === 'Space')).toBe(true); + + // Row positions should be adjusted (starting from lower values) + const spaceKey = result.find((key) => key.code === 'Space'); + expect(spaceKey?.row).toBeLessThan(5.5); + }); + + it('should return AZERTY layout with correct key labels', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.AZERTY, KeyboardSizeEnum.FULL); + + // Check AZERTY-specific key mappings + const keyQ = result.find((key) => key.code === 'KeyQ'); + const keyW = result.find((key) => key.code === 'KeyW'); + const keyA = result.find((key) => key.code === 'KeyA'); + const keyZ = result.find((key) => key.code === 'KeyZ'); + + expect(keyQ?.label).toBe('A'); + expect(keyW?.label).toBe('Z'); + expect(keyA?.label).toBe('Q'); + expect(keyZ?.label).toBe('W'); + }); + + it('should return DVORAK layout with correct key labels', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.DVORAK, KeyboardSizeEnum.FULL); + + // Check DVORAK-specific key mappings + const keyQ = result.find((key) => key.code === 'KeyQ'); + const keyW = result.find((key) => key.code === 'KeyW'); + const keyE = result.find((key) => key.code === 'KeyE'); + + expect(keyQ?.label).toBe("'"); + expect(keyW?.label).toBe(','); + expect(keyE?.label).toBe('.'); + }); + + it('should return COLEMAK layout with correct key labels', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.COLEMAK, KeyboardSizeEnum.FULL); + + // Check COLEMAK-specific key mappings + const keyE = result.find((key) => key.code === 'KeyE'); + const keyR = result.find((key) => key.code === 'KeyR'); + const keyS = result.find((key) => key.code === 'KeyS'); + const keyD = result.find((key) => key.code === 'KeyD'); + + expect(keyE?.label).toBe('F'); + expect(keyR?.label).toBe('P'); + expect(keyS?.label).toBe('R'); + expect(keyD?.label).toBe('S'); + }); + + it('should include all required key properties', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + expect(key).toHaveProperty('code'); + expect(key).toHaveProperty('label'); + expect(key).toHaveProperty('row'); + expect(key).toHaveProperty('col'); + expect(typeof key.code).toBe('string'); + expect(typeof key.label).toBe('string'); + expect(typeof key.row).toBe('number'); + expect(typeof key.col).toBe('number'); + }); + }); + + it('should have correct key widths for special keys', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + const tab = result.find((key) => key.code === 'Tab'); + const capsLock = result.find((key) => key.code === 'CapsLock'); + const shiftLeft = result.find((key) => key.code === 'ShiftLeft'); + const space = result.find((key) => key.code === 'Space'); + const backspace = result.find((key) => key.code === 'Backspace'); + + expect(tab?.width).toBe(1.5); + expect(capsLock?.width).toBe(1.75); + expect(shiftLeft?.width).toBe(2.25); + expect(space?.width).toBe(6.25); + expect(backspace?.width).toBe(2); + }); + + it('should have correct heights for special numpad keys', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + const numpadAdd = result.find((key) => key.code === 'NumpadAdd'); + const numpadEnter = result.find((key) => key.code === 'NumpadEnter'); + + expect(numpadAdd?.height).toBe(2); + expect(numpadEnter?.height).toBe(2); + }); + + it('should include all number row keys', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + const numberKeys = [ + 'Digit1', + 'Digit2', + 'Digit3', + 'Digit4', + 'Digit5', + 'Digit6', + 'Digit7', + 'Digit8', + 'Digit9', + 'Digit0', + ]; + + numberKeys.forEach((code) => { + expect(result.some((key) => key.code === code)).toBe(true); + }); + }); + + it('should include all modifier keys', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + const modifierKeys = [ + 'ControlLeft', + 'ControlRight', + 'ShiftLeft', + 'ShiftRight', + 'AltLeft', + 'AltRight', + 'MetaLeft', + 'MetaRight', + ]; + + modifierKeys.forEach((code) => { + expect(result.some((key) => key.code === code)).toBe(true); + }); + }); + + it('should include all arrow keys', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.TKL); + + const arrowKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight']; + + arrowKeys.forEach((code) => { + expect(result.some((key) => key.code === code)).toBe(true); + }); + }); + + it('should not include arrow keys in 60% layout', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.COMPACT_60); + + const arrowKeys = ['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight']; + + arrowKeys.forEach((code) => { + expect(result.some((key) => key.code === code)).toBe(false); + }); + }); + }); + + describe('getKeyLabel', () => { + it('should return correct label for QWERTY layout', () => { + expect(getKeyLabel('KeyQ', KeyboardLayoutEnum.QWERTY)).toBe('Q'); + expect(getKeyLabel('KeyA', KeyboardLayoutEnum.QWERTY)).toBe('A'); + expect(getKeyLabel('KeyZ', KeyboardLayoutEnum.QWERTY)).toBe('Z'); + }); + + it('should return correct label for AZERTY layout', () => { + expect(getKeyLabel('KeyQ', KeyboardLayoutEnum.AZERTY)).toBe('A'); + expect(getKeyLabel('KeyA', KeyboardLayoutEnum.AZERTY)).toBe('Q'); + expect(getKeyLabel('KeyW', KeyboardLayoutEnum.AZERTY)).toBe('Z'); + expect(getKeyLabel('KeyZ', KeyboardLayoutEnum.AZERTY)).toBe('W'); + }); + + it('should return correct label for DVORAK layout', () => { + expect(getKeyLabel('KeyQ', KeyboardLayoutEnum.DVORAK)).toBe("'"); + expect(getKeyLabel('KeyW', KeyboardLayoutEnum.DVORAK)).toBe(','); + expect(getKeyLabel('KeyE', KeyboardLayoutEnum.DVORAK)).toBe('.'); + }); + + it('should return correct label for COLEMAK layout', () => { + expect(getKeyLabel('KeyE', KeyboardLayoutEnum.COLEMAK)).toBe('F'); + expect(getKeyLabel('KeyR', KeyboardLayoutEnum.COLEMAK)).toBe('P'); + expect(getKeyLabel('KeyS', KeyboardLayoutEnum.COLEMAK)).toBe('R'); + }); + + it('should return code if key not found', () => { + expect(getKeyLabel('UnknownKey', KeyboardLayoutEnum.QWERTY)).toBe('UnknownKey'); + }); + + it('should return correct labels for special keys', () => { + expect(getKeyLabel('Space', KeyboardLayoutEnum.QWERTY)).toBe('Space'); + expect(getKeyLabel('Enter', KeyboardLayoutEnum.QWERTY)).toBe('Enter'); + expect(getKeyLabel('Backspace', KeyboardLayoutEnum.QWERTY)).toBe('Backspace'); + expect(getKeyLabel('Tab', KeyboardLayoutEnum.QWERTY)).toBe('Tab'); + }); + + it('should return correct labels for function keys', () => { + expect(getKeyLabel('F1', KeyboardLayoutEnum.QWERTY)).toBe('F1'); + expect(getKeyLabel('F12', KeyboardLayoutEnum.QWERTY)).toBe('F12'); + expect(getKeyLabel('Escape', KeyboardLayoutEnum.QWERTY)).toBe('Esc'); + }); + + it('should return correct labels for arrow keys', () => { + expect(getKeyLabel('ArrowUp', KeyboardLayoutEnum.QWERTY)).toBe('↑'); + expect(getKeyLabel('ArrowDown', KeyboardLayoutEnum.QWERTY)).toBe('↓'); + expect(getKeyLabel('ArrowLeft', KeyboardLayoutEnum.QWERTY)).toBe('←'); + expect(getKeyLabel('ArrowRight', KeyboardLayoutEnum.QWERTY)).toBe('→'); + }); + + it('should return correct labels for numpad keys', () => { + expect(getKeyLabel('Numpad0', KeyboardLayoutEnum.QWERTY)).toBe('0'); + expect(getKeyLabel('Numpad5', KeyboardLayoutEnum.QWERTY)).toBe('5'); + expect(getKeyLabel('NumpadAdd', KeyboardLayoutEnum.QWERTY)).toBe('+'); + expect(getKeyLabel('NumpadMultiply', KeyboardLayoutEnum.QWERTY)).toBe('*'); + }); + + it('should work with all keyboard layouts', () => { + const layouts: KeyboardLayout[] = [ + KeyboardLayoutEnum.QWERTY, + KeyboardLayoutEnum.AZERTY, + KeyboardLayoutEnum.DVORAK, + KeyboardLayoutEnum.COLEMAK, + ]; + + layouts.forEach((layout) => { + const label = getKeyLabel('Space', layout); + expect(label).toBe('Space'); + }); + }); + }); + + describe('getGridColumns', () => { + it('should return correct columns for full keyboard', () => { + expect(getGridColumns(KeyboardSizeEnum.FULL)).toBe(92); + }); + + it('should return correct columns for TKL keyboard', () => { + expect(getGridColumns(KeyboardSizeEnum.TKL)).toBe(72); + }); + + it('should return correct columns for 60% keyboard', () => { + expect(getGridColumns(KeyboardSizeEnum.COMPACT_60)).toBe(60); + }); + + it('should return TKL columns for unknown size', () => { + expect(getGridColumns('unknown' as KeyboardSize)).toBe(72); + }); + }); + + describe('getGridRows', () => { + it('should return correct rows for full keyboard', () => { + expect(getGridRows(KeyboardSizeEnum.FULL)).toBe(28); + }); + + it('should return correct rows for TKL keyboard', () => { + expect(getGridRows(KeyboardSizeEnum.TKL)).toBe(28); + }); + + it('should return correct rows for 60% keyboard', () => { + expect(getGridRows(KeyboardSizeEnum.COMPACT_60)).toBe(22); + }); + + it('should return TKL rows for unknown size', () => { + expect(getGridRows('unknown' as KeyboardSize)).toBe(28); + }); + }); + + describe('keyDefinition structure', () => { + it('should have valid row and column positions', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + expect(key.row).toBeGreaterThanOrEqual(0); + expect(key.col).toBeGreaterThanOrEqual(0); + }); + }); + + it('should have valid width values', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + if (key.width !== undefined) { + expect(key.width).toBeGreaterThan(0); + expect(key.width).toBeLessThanOrEqual(10); + } + }); + }); + + it('should have valid height values', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + if (key.height !== undefined) { + expect(key.height).toBeGreaterThan(0); + expect(key.height).toBeLessThanOrEqual(3); + } + }); + }); + + it('should have non-empty labels', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + expect(key.label.length).toBeGreaterThan(0); + }); + }); + + it('should have non-empty codes', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + result.forEach((key) => { + expect(key.code.length).toBeGreaterThan(0); + }); + }); + }); + + describe('layout consistency', () => { + it('should have same number of main keys across all layouts', () => { + const qwerty = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const azerty = getKeyboardLayout(KeyboardLayoutEnum.AZERTY, KeyboardSizeEnum.FULL); + const dvorak = getKeyboardLayout(KeyboardLayoutEnum.DVORAK, KeyboardSizeEnum.FULL); + const colemak = getKeyboardLayout(KeyboardLayoutEnum.COLEMAK, KeyboardSizeEnum.FULL); + + expect(qwerty.length).toBe(azerty.length); + expect(qwerty.length).toBe(dvorak.length); + expect(qwerty.length).toBe(colemak.length); + }); + + it('should have same key codes across all layouts', () => { + const qwerty = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const azerty = getKeyboardLayout(KeyboardLayoutEnum.AZERTY, KeyboardSizeEnum.FULL); + + const qwertyCodes = qwerty.map((k) => k.code).sort(); + const azertyCodes = azerty.map((k) => k.code).sort(); + + expect(qwertyCodes).toEqual(azertyCodes); + }); + + it('should have same positions across all layouts', () => { + const qwerty = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const dvorak = getKeyboardLayout(KeyboardLayoutEnum.DVORAK, KeyboardSizeEnum.FULL); + + qwerty.forEach((qwertyKey) => { + const dvorakKey = dvorak.find((k) => k.code === qwertyKey.code); + expect(dvorakKey?.row).toBe(qwertyKey.row); + expect(dvorakKey?.col).toBe(qwertyKey.col); + }); + }); + + it('should only differ in labels between layouts', () => { + const qwerty = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const colemak = getKeyboardLayout(KeyboardLayoutEnum.COLEMAK, KeyboardSizeEnum.FULL); + + qwerty.forEach((qwertyKey) => { + const colemakKey = colemak.find((k) => k.code === qwertyKey.code); + expect(colemakKey?.code).toBe(qwertyKey.code); + expect(colemakKey?.row).toBe(qwertyKey.row); + expect(colemakKey?.col).toBe(qwertyKey.col); + expect(colemakKey?.width).toBe(qwertyKey.width); + expect(colemakKey?.height).toBe(qwertyKey.height); + }); + }); + }); + + describe('size variations', () => { + it('should have fewer keys in TKL than full', () => { + const full = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const tkl = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.TKL); + + expect(tkl.length).toBeLessThan(full.length); + }); + + it('should have fewer keys in 60% than TKL', () => { + const tkl = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.TKL); + const compact = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.COMPACT_60); + + expect(compact.length).toBeLessThan(tkl.length); + }); + + it('should maintain key order within each row', () => { + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + + // Group keys by row + const rowGroups = new Map(); + result.forEach((key) => { + if (!rowGroups.has(key.row)) { + rowGroups.set(key.row, []); + } + rowGroups.get(key.row)!.push(key); + }); + + // Check that columns are in ascending order within each row + rowGroups.forEach((keys) => { + const cols = keys.map((k) => k.col); + const sortedCols = [...cols].sort((a, b) => a - b); + expect(cols).toEqual(sortedCols); + }); + }); + }); + + describe('platform-specific behavior', () => { + beforeEach(() => { + // Reset navigator mock before each test + vi.unstubAllGlobals(); + }); + + it('should use "Cmd" label on Mac platform', () => { + vi.stubGlobal('navigator', { + platform: 'MacIntel', + }); + + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const metaLeft = result.find((key) => key.code === 'MetaLeft'); + const metaRight = result.find((key) => key.code === 'MetaRight'); + + expect(metaLeft?.label).toBe('Cmd'); + expect(metaRight?.label).toBe('Cmd'); + }); + + it('should use "Win" label on Windows platform', () => { + vi.stubGlobal('navigator', { + platform: 'Win32', + }); + + const result = getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + const metaLeft = result.find((key) => key.code === 'MetaLeft'); + const metaRight = result.find((key) => key.code === 'MetaRight'); + + expect(metaLeft?.label).toBe('Win'); + expect(metaRight?.label).toBe('Win'); + }); + + it('should handle missing navigator gracefully', () => { + vi.stubGlobal('navigator', undefined); + + expect(() => { + getKeyboardLayout(KeyboardLayoutEnum.QWERTY, KeyboardSizeEnum.FULL); + }).not.toThrow(); + }); + }); +}); diff --git a/src/tools/keyboard-tester/keyboard-tester.service.ts b/src/tools/keyboard-tester/keyboard-tester.service.ts new file mode 100644 index 00000000..bfe8a7ba --- /dev/null +++ b/src/tools/keyboard-tester/keyboard-tester.service.ts @@ -0,0 +1,419 @@ +// Keyboard layout definitions +export enum KeyboardLayoutEnum { + QWERTY = 'qwerty', + AZERTY = 'azerty', + DVORAK = 'dvorak', + COLEMAK = 'colemak', +} + +export enum KeyboardSizeEnum { + FULL = 'full', + TKL = 'tkl', + COMPACT_60 = '60', +} + +export type KeyboardLayout = `${KeyboardLayoutEnum}`; +export type KeyboardSize = `${KeyboardSizeEnum}`; + +export interface KeyDefinition { + code: string; + label: string; + width?: number; // in key units (1 = standard key width) + height?: number; // in key units (1 = standard key height) + row: number; + col: number; +} + +// Grid configuration constants +const GRID_CONFIG = { + FULL_COLUMNS: 92, + TKL_COLUMNS: 72, + COMPACT_60_COLUMNS: 60, + FULL_ROWS: 28, + TKL_ROWS: 28, + COMPACT_60_ROWS: 22, + ROW_OFFSET_60: 1.5, // Offset to remove function key row for 60% keyboards +} as const; + +// Key row positions +const KEY_ROWS = { + FUNCTION: 0, + NUMBER: 1.5, + QWERTY: 2.5, + ASDF: 3.5, + ZXCV: 4.5, + BOTTOM: 5.5, +} as const; + +// Key column positions for function keys +const FUNCTION_KEY_COLS = { + ESC: 0, + F1: 1.5, + F2: 2.5, + F3: 3.5, + F4: 4.5, + F5: 6, + F6: 7, + F7: 8, + F8: 9, + F9: 10.5, + F10: 11.5, + F11: 12.5, + F12: 13.5, +} as const; + +// Key widths +const KEY_WIDTHS = { + STANDARD: 1, + TAB: 1.5, + CAPS: 1.75, + SHIFT_LEFT: 2.25, + SHIFT_RIGHT: 2.75, + BACKSPACE: 2, + ENTER: 2.25, + BACKSLASH: 1.5, + CTRL: 1.25, + META: 1.25, + ALT: 1.25, + SPACE: 6.25, + MENU: 1.25, + NUMPAD_DOUBLE: 2, +} as const; + +// Navigation key positions +const NAV_KEY_COLS = { + PRINT_SCREEN: 15.5, + SCROLL_LOCK: 16.5, + PAUSE: 17.5, +} as const; + +// Numpad key positions +const NUMPAD_COLS = { + COL_1: 19, + COL_2: 20, + COL_3: 21, + COL_4: 22, +} as const; + +// Detect OS for platform-specific key labels +function isMac(): boolean { + return typeof navigator !== 'undefined' && /Mac|iPhone|iPad|iPod/.test(navigator.platform); +} + +// Base layout structure for different sizes +const functionKeys: KeyDefinition[] = [ + { code: 'Escape', label: 'Esc', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.ESC }, + { code: 'F1', label: 'F1', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F1 }, + { code: 'F2', label: 'F2', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F2 }, + { code: 'F3', label: 'F3', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F3 }, + { code: 'F4', label: 'F4', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F4 }, + { code: 'F5', label: 'F5', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F5 }, + { code: 'F6', label: 'F6', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F6 }, + { code: 'F7', label: 'F7', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F7 }, + { code: 'F8', label: 'F8', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F8 }, + { code: 'F9', label: 'F9', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F9 }, + { code: 'F10', label: 'F10', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F10 }, + { code: 'F11', label: 'F11', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F11 }, + { code: 'F12', label: 'F12', row: KEY_ROWS.FUNCTION, col: FUNCTION_KEY_COLS.F12 }, +]; + +const navigationKeys: KeyDefinition[] = [ + { code: 'PrintScreen', label: 'PrtSc', row: KEY_ROWS.FUNCTION, col: NAV_KEY_COLS.PRINT_SCREEN }, + { code: 'ScrollLock', label: 'ScrLk', row: KEY_ROWS.FUNCTION, col: NAV_KEY_COLS.SCROLL_LOCK }, + { code: 'Pause', label: 'Pause', row: KEY_ROWS.FUNCTION, col: NAV_KEY_COLS.PAUSE }, + { code: 'Insert', label: 'Ins', row: KEY_ROWS.NUMBER, col: NAV_KEY_COLS.PRINT_SCREEN }, + { code: 'Home', label: 'Home', row: KEY_ROWS.NUMBER, col: NAV_KEY_COLS.SCROLL_LOCK }, + { code: 'PageUp', label: 'PgUp', row: KEY_ROWS.NUMBER, col: NAV_KEY_COLS.PAUSE }, + { code: 'Delete', label: 'Del', row: KEY_ROWS.QWERTY, col: NAV_KEY_COLS.PRINT_SCREEN }, + { code: 'End', label: 'End', row: KEY_ROWS.QWERTY, col: NAV_KEY_COLS.SCROLL_LOCK }, + { code: 'PageDown', label: 'PgDn', row: KEY_ROWS.QWERTY, col: NAV_KEY_COLS.PAUSE }, + { code: 'ArrowUp', label: '↑', row: KEY_ROWS.ZXCV, col: NAV_KEY_COLS.SCROLL_LOCK }, + { code: 'ArrowLeft', label: '←', row: KEY_ROWS.BOTTOM, col: NAV_KEY_COLS.PRINT_SCREEN }, + { code: 'ArrowDown', label: '↓', row: KEY_ROWS.BOTTOM, col: NAV_KEY_COLS.SCROLL_LOCK }, + { code: 'ArrowRight', label: '→', row: KEY_ROWS.BOTTOM, col: NAV_KEY_COLS.PAUSE }, +]; + +const numpadKeys: KeyDefinition[] = [ + { code: 'NumLock', label: 'Num', row: KEY_ROWS.NUMBER, col: NUMPAD_COLS.COL_1 }, + { code: 'NumpadDivide', label: '/', row: KEY_ROWS.NUMBER, col: NUMPAD_COLS.COL_2 }, + { code: 'NumpadMultiply', label: '*', row: KEY_ROWS.NUMBER, col: NUMPAD_COLS.COL_3 }, + { code: 'NumpadSubtract', label: '-', row: KEY_ROWS.NUMBER, col: NUMPAD_COLS.COL_4 }, + { code: 'Numpad7', label: '7', row: KEY_ROWS.QWERTY, col: NUMPAD_COLS.COL_1 }, + { code: 'Numpad8', label: '8', row: KEY_ROWS.QWERTY, col: NUMPAD_COLS.COL_2 }, + { code: 'Numpad9', label: '9', row: KEY_ROWS.QWERTY, col: NUMPAD_COLS.COL_3 }, + { + code: 'NumpadAdd', + label: '+', + width: KEY_WIDTHS.STANDARD, + height: 2, + row: KEY_ROWS.QWERTY, + col: NUMPAD_COLS.COL_4, + }, + { code: 'Numpad4', label: '4', row: KEY_ROWS.ASDF, col: NUMPAD_COLS.COL_1 }, + { code: 'Numpad5', label: '5', row: KEY_ROWS.ASDF, col: NUMPAD_COLS.COL_2 }, + { code: 'Numpad6', label: '6', row: KEY_ROWS.ASDF, col: NUMPAD_COLS.COL_3 }, + { code: 'Numpad1', label: '1', row: KEY_ROWS.ZXCV, col: NUMPAD_COLS.COL_1 }, + { code: 'Numpad2', label: '2', row: KEY_ROWS.ZXCV, col: NUMPAD_COLS.COL_2 }, + { code: 'Numpad3', label: '3', row: KEY_ROWS.ZXCV, col: NUMPAD_COLS.COL_3 }, + { + code: 'NumpadEnter', + label: 'Enter', + width: KEY_WIDTHS.STANDARD, + height: 2, + row: KEY_ROWS.ZXCV, + col: NUMPAD_COLS.COL_4, + }, + { code: 'Numpad0', label: '0', width: KEY_WIDTHS.NUMPAD_DOUBLE, row: KEY_ROWS.BOTTOM, col: NUMPAD_COLS.COL_1 }, + { code: 'NumpadDecimal', label: '.', row: KEY_ROWS.BOTTOM, col: NUMPAD_COLS.COL_3 }, +]; + +// QWERTY Layout +function getQwertyKeys(): KeyDefinition[] { + const metaLabel = isMac() ? 'Cmd' : 'Win'; + + return [ + // Number row + { code: 'Backquote', label: '`', row: KEY_ROWS.NUMBER, col: 0 }, + { code: 'Digit1', label: '1', row: KEY_ROWS.NUMBER, col: 1 }, + { code: 'Digit2', label: '2', row: KEY_ROWS.NUMBER, col: 2 }, + { code: 'Digit3', label: '3', row: KEY_ROWS.NUMBER, col: 3 }, + { code: 'Digit4', label: '4', row: KEY_ROWS.NUMBER, col: 4 }, + { code: 'Digit5', label: '5', row: KEY_ROWS.NUMBER, col: 5 }, + { code: 'Digit6', label: '6', row: KEY_ROWS.NUMBER, col: 6 }, + { code: 'Digit7', label: '7', row: KEY_ROWS.NUMBER, col: 7 }, + { code: 'Digit8', label: '8', row: KEY_ROWS.NUMBER, col: 8 }, + { code: 'Digit9', label: '9', row: KEY_ROWS.NUMBER, col: 9 }, + { code: 'Digit0', label: '0', row: KEY_ROWS.NUMBER, col: 10 }, + { code: 'Minus', label: '-', row: KEY_ROWS.NUMBER, col: 11 }, + { code: 'Equal', label: '=', row: KEY_ROWS.NUMBER, col: 12 }, + { code: 'Backspace', label: 'Backspace', width: KEY_WIDTHS.BACKSPACE, row: KEY_ROWS.NUMBER, col: 13 }, + + // QWERTY row + { code: 'Tab', label: 'Tab', width: KEY_WIDTHS.TAB, row: KEY_ROWS.QWERTY, col: 0 }, + { code: 'KeyQ', label: 'Q', row: KEY_ROWS.QWERTY, col: 1.5 }, + { code: 'KeyW', label: 'W', row: KEY_ROWS.QWERTY, col: 2.5 }, + { code: 'KeyE', label: 'E', row: KEY_ROWS.QWERTY, col: 3.5 }, + { code: 'KeyR', label: 'R', row: KEY_ROWS.QWERTY, col: 4.5 }, + { code: 'KeyT', label: 'T', row: KEY_ROWS.QWERTY, col: 5.5 }, + { code: 'KeyY', label: 'Y', row: KEY_ROWS.QWERTY, col: 6.5 }, + { code: 'KeyU', label: 'U', row: KEY_ROWS.QWERTY, col: 7.5 }, + { code: 'KeyI', label: 'I', row: KEY_ROWS.QWERTY, col: 8.5 }, + { code: 'KeyO', label: 'O', row: KEY_ROWS.QWERTY, col: 9.5 }, + { code: 'KeyP', label: 'P', row: KEY_ROWS.QWERTY, col: 10.5 }, + { code: 'BracketLeft', label: '[', row: KEY_ROWS.QWERTY, col: 11.5 }, + { code: 'BracketRight', label: ']', row: KEY_ROWS.QWERTY, col: 12.5 }, + { code: 'Backslash', label: '\\', width: KEY_WIDTHS.BACKSLASH, row: KEY_ROWS.QWERTY, col: 13.5 }, + + // ASDF row + { code: 'CapsLock', label: 'Caps', width: KEY_WIDTHS.CAPS, row: KEY_ROWS.ASDF, col: 0 }, + { code: 'KeyA', label: 'A', row: KEY_ROWS.ASDF, col: 1.75 }, + { code: 'KeyS', label: 'S', row: KEY_ROWS.ASDF, col: 2.75 }, + { code: 'KeyD', label: 'D', row: KEY_ROWS.ASDF, col: 3.75 }, + { code: 'KeyF', label: 'F', row: KEY_ROWS.ASDF, col: 4.75 }, + { code: 'KeyG', label: 'G', row: KEY_ROWS.ASDF, col: 5.75 }, + { code: 'KeyH', label: 'H', row: KEY_ROWS.ASDF, col: 6.75 }, + { code: 'KeyJ', label: 'J', row: KEY_ROWS.ASDF, col: 7.75 }, + { code: 'KeyK', label: 'K', row: KEY_ROWS.ASDF, col: 8.75 }, + { code: 'KeyL', label: 'L', row: KEY_ROWS.ASDF, col: 9.75 }, + { code: 'Semicolon', label: ';', row: KEY_ROWS.ASDF, col: 10.75 }, + { code: 'Quote', label: "'", row: KEY_ROWS.ASDF, col: 11.75 }, + { code: 'Enter', label: 'Enter', width: KEY_WIDTHS.ENTER, row: KEY_ROWS.ASDF, col: 12.75 }, + + // ZXCV row + { code: 'ShiftLeft', label: 'Shift', width: KEY_WIDTHS.SHIFT_LEFT, row: KEY_ROWS.ZXCV, col: 0 }, + { code: 'KeyZ', label: 'Z', row: KEY_ROWS.ZXCV, col: 2.25 }, + { code: 'KeyX', label: 'X', row: KEY_ROWS.ZXCV, col: 3.25 }, + { code: 'KeyC', label: 'C', row: KEY_ROWS.ZXCV, col: 4.25 }, + { code: 'KeyV', label: 'V', row: KEY_ROWS.ZXCV, col: 5.25 }, + { code: 'KeyB', label: 'B', row: KEY_ROWS.ZXCV, col: 6.25 }, + { code: 'KeyN', label: 'N', row: KEY_ROWS.ZXCV, col: 7.25 }, + { code: 'KeyM', label: 'M', row: KEY_ROWS.ZXCV, col: 8.25 }, + { code: 'Comma', label: ',', row: KEY_ROWS.ZXCV, col: 9.25 }, + { code: 'Period', label: '.', row: KEY_ROWS.ZXCV, col: 10.25 }, + { code: 'Slash', label: '/', row: KEY_ROWS.ZXCV, col: 11.25 }, + { code: 'ShiftRight', label: 'Shift', width: KEY_WIDTHS.SHIFT_RIGHT, row: KEY_ROWS.ZXCV, col: 12.25 }, + + // Bottom row + { code: 'ControlLeft', label: 'Ctrl', width: KEY_WIDTHS.CTRL, row: KEY_ROWS.BOTTOM, col: 0 }, + { code: 'MetaLeft', label: metaLabel, width: KEY_WIDTHS.META, row: KEY_ROWS.BOTTOM, col: 1.25 }, + { code: 'AltLeft', label: 'Alt', width: KEY_WIDTHS.ALT, row: KEY_ROWS.BOTTOM, col: 2.5 }, + { code: 'Space', label: 'Space', width: KEY_WIDTHS.SPACE, row: KEY_ROWS.BOTTOM, col: 3.75 }, + { code: 'AltRight', label: 'Alt', width: KEY_WIDTHS.ALT, row: KEY_ROWS.BOTTOM, col: 10 }, + { code: 'MetaRight', label: metaLabel, width: KEY_WIDTHS.META, row: KEY_ROWS.BOTTOM, col: 11.25 }, + { code: 'ContextMenu', label: 'Menu', width: KEY_WIDTHS.MENU, row: KEY_ROWS.BOTTOM, col: 12.5 }, + { code: 'ControlRight', label: 'Ctrl', width: KEY_WIDTHS.CTRL, row: KEY_ROWS.BOTTOM, col: 13.75 }, + ]; +} + +// AZERTY Layout (French) +function getAzertyKeys(): KeyDefinition[] { + const keys = getQwertyKeys(); + const replacements: Record = { + KeyQ: 'A', + KeyW: 'Z', + KeyA: 'Q', + KeyZ: 'W', + KeyM: ',', + Comma: ';', + Semicolon: 'M', + Period: ':', + Slash: '!', + }; + + return keys.map((key) => { + const newLabel = replacements[key.code]; + if (newLabel) { + return { ...key, label: newLabel }; + } + return key; + }); +} + +// DVORAK Layout +function getDvorakKeys(): KeyDefinition[] { + const keys = getQwertyKeys(); + const replacements: Record = { + KeyQ: "'", + KeyW: ',', + KeyE: '.', + KeyR: 'P', + KeyT: 'Y', + KeyY: 'F', + KeyU: 'G', + KeyI: 'C', + KeyO: 'R', + KeyP: 'L', + BracketLeft: '/', + BracketRight: '=', + KeyA: 'A', + KeyS: 'O', + KeyD: 'E', + KeyF: 'U', + KeyG: 'I', + KeyH: 'D', + KeyJ: 'H', + KeyK: 'T', + KeyL: 'N', + Semicolon: 'S', + Quote: '-', + KeyZ: ';', + KeyX: 'Q', + KeyC: 'J', + KeyV: 'K', + KeyB: 'X', + KeyN: 'B', + KeyM: 'M', + Comma: 'W', + Period: 'V', + Slash: 'Z', + }; + + return keys.map((key) => { + const newLabel = replacements[key.code]; + if (newLabel) { + return { ...key, label: newLabel }; + } + return key; + }); +} + +// COLEMAK Layout +function getColemakKeys(): KeyDefinition[] { + const keys = getQwertyKeys(); + const replacements: Record = { + KeyE: 'F', + KeyR: 'P', + KeyT: 'G', + KeyY: 'J', + KeyU: 'L', + KeyI: 'U', + KeyO: 'Y', + KeyP: ';', + KeyS: 'R', + KeyD: 'S', + KeyF: 'T', + KeyG: 'D', + KeyJ: 'N', + KeyK: 'E', + KeyL: 'I', + Semicolon: 'O', + KeyN: 'K', + }; + + return keys.map((key) => { + const newLabel = replacements[key.code]; + if (newLabel) { + return { ...key, label: newLabel }; + } + return key; + }); +} + +export function getKeyboardLayout(layout: KeyboardLayout, size: KeyboardSize): KeyDefinition[] { + let mainKeys: KeyDefinition[] = []; + + // Get layout-specific keys + switch (layout) { + case KeyboardLayoutEnum.QWERTY: + mainKeys = getQwertyKeys(); + break; + case KeyboardLayoutEnum.AZERTY: + mainKeys = getAzertyKeys(); + break; + case KeyboardLayoutEnum.DVORAK: + mainKeys = getDvorakKeys(); + break; + case KeyboardLayoutEnum.COLEMAK: + mainKeys = getColemakKeys(); + break; + } + + // Add size-specific keys and adjust positions + switch (size) { + case KeyboardSizeEnum.FULL: + return [...functionKeys, ...mainKeys, ...navigationKeys, ...numpadKeys]; + case KeyboardSizeEnum.TKL: + return [...functionKeys, ...mainKeys, ...navigationKeys]; + case KeyboardSizeEnum.COMPACT_60: + // For 60% keyboard, adjust row positions to start from 0 + return mainKeys.map((key) => ({ + ...key, + row: key.row - GRID_CONFIG.ROW_OFFSET_60, + })); + default: + return [...functionKeys, ...mainKeys, ...navigationKeys]; + } +} + +export function getKeyLabel(code: string, layout: KeyboardLayout): string { + const keys = getKeyboardLayout(layout, 'full'); + const key = keys.find((k) => k.code === code); + return key?.label || code; +} + +// Export grid configuration for use in components +export function getGridColumns(size: KeyboardSize): number { + switch (size) { + case KeyboardSizeEnum.FULL: + return GRID_CONFIG.FULL_COLUMNS; + case KeyboardSizeEnum.TKL: + return GRID_CONFIG.TKL_COLUMNS; + case KeyboardSizeEnum.COMPACT_60: + return GRID_CONFIG.COMPACT_60_COLUMNS; + default: + return GRID_CONFIG.TKL_COLUMNS; + } +} + +export function getGridRows(size: KeyboardSize): number { + switch (size) { + case KeyboardSizeEnum.FULL: + return GRID_CONFIG.FULL_ROWS; + case KeyboardSizeEnum.TKL: + return GRID_CONFIG.TKL_ROWS; + case KeyboardSizeEnum.COMPACT_60: + return GRID_CONFIG.COMPACT_60_ROWS; + default: + return GRID_CONFIG.TKL_ROWS; + } +} diff --git a/src/tools/keyboard-tester/keyboard-tester.vue b/src/tools/keyboard-tester/keyboard-tester.vue new file mode 100644 index 00000000..069965c9 --- /dev/null +++ b/src/tools/keyboard-tester/keyboard-tester.vue @@ -0,0 +1,229 @@ + + +