Input Drivers
Portix OS provides PS/2 keyboard and mouse drivers with scancode decoding, modifier tracking, and button state management.Keyboard
KeyboardState
Tracks keyboard state including modifiers, scancodes, and extended key sequences.- Shift (left/right)
- Caps Lock
- Ctrl
- Alt
- E0 prefix (for extended keys)
KeyboardState::new()
Creates a new keyboard state.KeyboardState::feed_byte()
Processes a single scancode byte from the PS/2 buffer.Scancode byte from port 0x60
Some(Key) when a complete key press is decoded, None for modifier changes or multi-byte sequences.
Use this method when implementing unified PS/2 buffer draining. The caller must read from port 0x60 and verify AUXB=0 before calling.
KeyboardState::poll()
Polls the keyboard hardware directly.Some(Key) if a key was pressed, None otherwise.
KeyboardState::ctrl()
Returns true if Ctrl is currently held.KeyboardState::alt()
Returns true if Alt is currently held.Key
Represents a decoded keyboard key.Char(u8)
Printable ASCII character with Shift/Caps Lock applied
Enter
Enter/Return key (scancode 0x1C)
Backspace
Backspace key (scancode 0x0E)
Tab
Tab key (scancode 0x0F)
Escape
Escape key (scancode 0x01)
Up/Down/Left/Right
Arrow keys (scancodes 0x48/0x50/0x4B/0x4D or E0 prefix)
F1..F10
Function keys (scancodes 0x3B..0x44)
Delete/Home/End/PageUp/PageDown/Insert
Extended navigation keys (E0 prefix)
Mouse
MouseState
Tracks PS/2 mouse position, buttons, and internal packet state.Current cursor X coordinate (clamped to 0..max_x)
Current cursor Y coordinate (clamped to 0..max_y)
Current button state bitmask (bit 0=left, 1=right, 2=middle)
Previous frame’s button state (for click detection)
Screen bounds (set via
init())True if mouse was successfully initialized
True if scroll wheel is supported (not yet implemented)
Cumulative error count (decreases on successful packets)
Number of times the mouse has been reset
MouseState::new()
Creates a new mouse state.Default bounds: 1024×768
MouseState::init()
Initializes the PS/2 mouse hardware.Screen width in pixels
Screen height in pixels
true if initialization succeeded, false otherwise.
Initialization steps:
- Drains the keyboard controller buffer
- Enables the auxiliary (mouse) device (command 0xA8)
- Configures interrupts in the controller configuration byte
- Sends mouse reset (0xF6) and set defaults (0xF3 100)
- Enables data reporting (0xF4)
MouseState::begin_frame()
Prepares for a new polling frame.- Copies
buttonstoprev_buttons - Resets
scroll_deltato 0
Call once per frame before calling
feed(). This is required for click detection to work correctly.MouseState::feed()
Processes a single byte from the PS/2 mouse.Byte from port 0x60 (caller must verify AUXB=1 in status register)
true if a complete 3-byte packet was received and the mouse moved or buttons changed.
PS/2 Mouse Packet Format:
- Byte 0: Flags (bit 3=always 1, bit 4=X sign, bit 5=Y sign, bits 0-2=buttons)
- Byte 1: X movement (low 8 bits)
- Byte 2: Y movement (low 8 bits)
- Rejects packets with flags bit 3 = 0 (sync error)
- Rejects deltas > 120 pixels (teleport threshold)
- Rejects packets with overflow bits (0xC0) set
- Automatically resets after 25 errors
MouseState::intelligent_reset()
Resets the mouse hardware if errors accumulate.- Drains buffer
- Sends reset defaults (0xF6)
- Re-enables data reporting (0xF4)
- Clears error count and packet index
- Increments reset counter
error_count >= 25 during packet processing.
Mouse Button Queries
MouseState::left_btn()
Returns true if left button is currently pressed.MouseState::right_btn()
Returns true if right button is currently pressed.MouseState::middle_btn()
Returns true if middle button is currently pressed.MouseState::left_clicked()
Returns true if left button was just pressed this frame.buttons & 0x01 != 0 && prev_buttons & 0x01 == 0
MouseState::right_clicked()
Returns true if right button was just pressed this frame.MouseState::left_released()
Returns true if left button was just released this frame.Unified Buffer Draining
The recommended approach for handling both keyboard and mouse:- No lost bytes from buffer competition
- Single drain loop per frame
- Correct button state tracking
- Automatic error recovery