Skip to content

Instantly share code, notes, and snippets.

@JerryNixon
Last active January 5, 2025 06:29
Show Gist options
  • Save JerryNixon/eb1cf73540dc8f46177954181036afb4 to your computer and use it in GitHub Desktop.
Save JerryNixon/eb1cf73540dc8f46177954181036afb4 to your computer and use it in GitHub Desktop.
Console Keyboard
using System.Drawing;
public class Program
{
static Program()
{
Console.Clear();
Console.CursorVisible = false;
}
public static readonly string[] Keyboard =
[
" 1 2 3 4 5 6 7 8 9 0 ",
" Q W E R T Y U I O P ",
" A S D F G H J K L ",
" Z X C V B N M ",
];
private static readonly int TopOffset = 2;
private static readonly int LeftOffset = 2;
public static void Main(string[] args)
{
ConsoleKeyInfo keyInfo = default;
while (true)
{
for (int index = 0; index < Keyboard.Length; index++)
{
var line = Keyboard[index];
Draw(line, LeftOffset, TopOffset + index * 2);
if (keyInfo.KeyChar is char key
&& line.Contains(keyInfo.KeyChar, StringComparison.CurrentCultureIgnoreCase))
{
HighlightLetter(index, line, key);
}
}
keyInfo = Console.ReadKey(true);
}
static void HighlightLetter(int index, string item, char key)
{
var top = index * 2 + TopOffset;
var left = item.IndexOf(key, StringComparison.CurrentCultureIgnoreCase) + LeftOffset;
Draw(key.ToString().ToUpper(), left, top, ConsoleColor.Black, ConsoleColor.White);
}
}
public static void Draw(string text, int? x = null, int? y = null,
ConsoleColor? foregroundColor = null, ConsoleColor? backgroundColor = null)
{
var (left, top) = Console.GetCursorPosition();
var point = new Point(left, top);
Console.SetCursorPosition(x ?? left, y ?? top);
Console.ForegroundColor = foregroundColor ?? Console.ForegroundColor;
Console.BackgroundColor = backgroundColor ?? Console.BackgroundColor;
Console.Write(text);
Console.ResetColor();
Console.SetCursorPosition(left, top);
}
}

Comprehensive Checklist with Acceptance Criteria

General Console Behavior

  • The application must:
    • Clear all previous content in the console on startup.
    • Hide the console cursor during execution.
    • Run continuously until manually exited.
    • Dynamically update console output without flickering.

Keyboard Display

  • The keyboard must:
    • Consist of fixed rows:
      • Row 1: 1 2 3 4 5 6 7 8 9 0
      • Row 2: Q W E R T Y U I O P
      • Row 3: A S D F G H J K L
      • Row 4: Z X C V B N M
    • Display rows starting at a configurable TopOffset (default: 2).
    • Display rows starting at a configurable LeftOffset (default: 2).
    • Include consistent spacing for alignment and readability.
    • Have a vertical spacing of 2 units between rows.

Key Highlight Behavior

  • The program must:
    • Highlight the corresponding key when pressed.
    • Use ConsoleColor.Black for the text and ConsoleColor.White for the background of the highlighted key.
    • Highlight keys case-insensitively (e.g., a or A highlights the same key).
    • Only highlight the first matching key if the key exists in multiple rows.
    • Do nothing when a pressed key is not in the Keyboard array.

Input Handling

  • The program must:
    • Detect all printable character keys.
    • Match and highlight keys regardless of case.
    • Ignore non-printable keys (e.g., Arrow, Escape, Tab).

Drawing Logic

  • The Draw method must:
    • Write a string to a specific console position (x, y).
    • Allow configurable text and background colors.
    • Default to the current console cursor position and colors if no values are provided.
    • Restore the console cursor to its original position after drawing.

Highlight Calculation

  • Highlight positioning must:
    • Correctly calculate the horizontal position as Character index + LeftOffset.
    • Correctly calculate the vertical position as (Row index * 2) + TopOffset.
    • Use case-insensitive matching to locate characters in a row.

Error Handling

  • The program must:
    • Ignore invalid or unsupported keys.
    • Not crash if unsupported keys are pressed.
    • Continue functioning correctly when unsupported keys are pressed.

Performance

  • The program must:
    • Only redraw the keyboard when necessary (e.g., on key press).
    • Avoid flickering during updates.
    • Handle rapid consecutive key presses without lag.

Acceptance Criteria

  • The keyboard is displayed correctly at the configured position with all rows aligned.
  • Pressing a valid key highlights the corresponding key in the keyboard display.
  • The highlight uses the specified colors (ConsoleColor.Black text, ConsoleColor.White background).
  • Case-insensitive input correctly highlights the appropriate key.
  • Pressing keys not on the keyboard display does not affect the program behavior.
  • Non-printable keys are ignored without affecting the display or causing crashes.
  • The console cursor remains hidden throughout the program's execution.
  • Console updates occur smoothly without flickering or clearing the entire screen.
  • The program handles rapid consecutive key presses without visual glitches or performance degradation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment