Keyboard Input
SourcesThe KeyboardInput block enables real-time manual control of simulation models using standard keyboard inputs (Arrow keys or WASD keys). Clicking the block on the canvas arms it with keyboard focus; pressing directional keys modifies two independent coordinate outputs ( and ). Placing a KeyboardInput block in a diagram automatically switches the simulation engine to real-time wall-clock pacing, making it ideal for interactive flight simulators, robotic teleoperation, steering gaming demonstrations, and manual disturbance injection.
Mathematical Model
The keyboard listener interprets keydown and keyup events into discrete directional states :
The output signals emitted from the two right-side ports are scaled by gain ():
When the user clicks elsewhere on the canvas, the block disarms automatically and both key states reset safely to zero ().
Inputs & Outputs
| Direction | ID | Label | Type | Status |
|---|---|---|---|---|
| ← Out | x |
X | number | Output |
| ← Out | y |
Y | number | Output |
Parameters
| Parameters | Label | Type | Default | Description |
|---|---|---|---|---|
keyX |
X State | number | 0 |
-1 (left/A), 0, or 1 (right/D) — written by the keyboard listener |
keyY |
Y State | number | 0 |
-1 (down/S), 0, or 1 (up/W) — written by the keyboard listener |
gain |
Gain | number | 1 |
Output scale: out = key state × gain |
Usage Examples
Example 1: Vehicle Steering and Throttle Game-Style Control
Teleoperating a simulated electric ground vehicle. Port provides forward/reverse drive torque with gain = 50.0 (Nm), while port commands front-wheel steering angle with gain = 25.0 (degrees). Cascading port through a RateLimiter block provides smooth, realistic steering wheel dynamics instead of abrupt step transitions.
Example 2: Interactive Dynamic Disturbance Injection
Testing controller disturbance rejection under human command. Connecting port through a Gain block of directly into the force summing junction of a magnetic levitation or inverted pendulum model. Tapping the arrow keys delivers calibrated impulse pushes to verify real-time stabilization.
Remarks & Best Practices
- Click-to-Arm Safety Focus: The block requires an explicit mouse click on its canvas tile to arm keyboard listening. Clicking anywhere outside the block immediately disarms keyboard capture and zeros the output, preventing stuck key states.
- Dual Key Mapping: Simultaneously supports both standard Arrow keys and WASD keys, accommodating both left-handed and right-handed ergonomic workflows.
- Smoothing with Dynamic Filters: Because keyboard presses emit instantaneous step transitions (), cascading the output through a
RateLimiter,TransferFunction, orLeadLagblock creates smooth analog-feeling actuation profiles. - Automatic Real-Time Engine Synchronization: Including a KeyboardInput block automatically locks the simulation solver to 1:1 real-time execution.
Related Components
- Joystick2D: Continuous two-axis analog mouse/touch thumbstick control.
- Slider: Single-axis continuous fader control with variable driving capability.
- RateLimiter: Limits the rate of change of discrete keyboard step commands.
- Constant: Fixed, non-interactive signal source.