⌨

Keyboard Input

Sources
Run the live example Free · no sign-up · runs in your browser

The 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 (XX and YY). 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 sx(t),sy(t)∈{−1,0,1}s_x(t), s_y(t) \in \{-1, 0, 1\}:

sx(t)={−1,ArrowLeft / A+1,ArrowRight / D0,otherwise,sy(t)={+1,ArrowUp / W−1,ArrowDown / S0,otherwises_x(t) = \begin{cases} -1, & \text{ArrowLeft / A} \\ +1, & \text{ArrowRight / D} \\ 0, & \text{otherwise} \end{cases}, \quad s_y(t) = \begin{cases} +1, & \text{ArrowUp / W} \\ -1, & \text{ArrowDown / S} \\ 0, & \text{otherwise} \end{cases}

The output signals emitted from the two right-side ports are scaled by gain (KK):

xout(t)=K⋅sx(t)yout(t)=K⋅sy(t)\begin{aligned} x_{\text{out}}(t) &= K \cdot s_x(t) \\ y_{\text{out}}(t) &= K \cdot s_y(t) \end{aligned}

When the user clicks elsewhere on the canvas, the block disarms automatically and both key states reset safely to zero (sx=0,sy=0s_x = 0, s_y = 0).

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 yy provides forward/reverse drive torque with gain = 50.0 (Nm), while port xx commands front-wheel steering angle with gain = 25.0 (degrees). Cascading port xx 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 yy through a Gain block of 10.010.0 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 (±K\pm K), cascading the output through a RateLimiter, TransferFunction, or LeadLag block 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.