Implementation:Google deepmind Dm control Viewer User Input
| Knowledge Sources | |
|---|---|
| Domains | Input Handling, GUI, Visualization |
| Last Updated | 2026-02-15 04:00 GMT |
Overview
This module defines keyboard and mouse input constants, action binding types, and the InputMap class that maps key/mouse combinations to callback actions for the dm_control viewer.
Description
The user input module provides a platform-independent input abstraction layer for the viewer. It defines constants for key codes (KEY_A through KEY_Z, KEY_F1 through KEY_F12, arrow keys, modifiers, etc.), mouse buttons (LEFT, RIGHT, MIDDLE), modifier keys (SHIFT, CONTROL, ALT, SUPER), and action states (PRESS, RELEASE, REPEAT), mirroring GLFW constants without a direct GLFW dependency.
Three named tuple types define distinct binding patterns. Exclusive defines press-and-hold actions: the callback receives True when the key combination is pressed and False when released. DoubleClick defines mouse double-click triggers limited to mouse button codes. Range maps a collection of key combinations to a single callback, passing the index of the triggering combination as an argument.
The InputMap class is the central input dispatcher. It registers with keyboard and mouse event sources (via their on_key, on_click, on_double_click, on_move, and on_scroll observables), maintains dictionaries of action and double-click callbacks keyed by (keycode, modifier) tuples, and dispatches events to bound callbacks. It supports planar motion bindings (for mouse movement) and z-axis bindings (for scroll wheel). Exclusive actions use a locking mechanism where only one exclusive action can be active at a time.
Usage
Use this module when defining or extending the viewer's interactive controls. The constants and binding types provide the vocabulary for input mapping, while InputMap handles the runtime dispatch from raw input events to application-level actions.
Code Reference
Source Location
- Repository: Google_deepmind_Dm_control
- File: dm_control/viewer/user_input.py
- Lines: 1-310
Signature
# Action states
RELEASE = 0
PRESS = 1
REPEAT = 2
# Key constants (subset)
KEY_SPACE = 32
KEY_A = 65
KEY_ESCAPE = 256
KEY_F1 = 290
# ... (full set of KEY_* and MOD_* constants)
# Mouse buttons
MOUSE_BUTTON_LEFT = 0
MOUSE_BUTTON_RIGHT = 1
MOUSE_BUTTON_MIDDLE = 2
# Modifier keys
MOD_NONE = 0
MOD_SHIFT = 0x0001
MOD_CONTROL = 0x0002
MOD_ALT = 0x0004
MOD_SUPER = 0x0008
MOD_SHIFT_CONTROL = MOD_SHIFT | MOD_CONTROL
class Exclusive(collections.namedtuple('Exclusive', 'combination')):
"""Defines an exclusive action (press-and-hold with True/False callbacks)."""
class DoubleClick(collections.namedtuple('DoubleClick', 'combination')):
"""Defines a mouse double click action."""
class Range(collections.namedtuple('Range', 'collection')):
"""Binds a number of key combinations to an indexed callback."""
class InputMap:
def __init__(self, mouse, keyboard): ...
def clear_bindings(self): ...
def bind(self, callback, key_binding): ...
def bind_plane(self, callback): ...
def bind_z_axis(self, callback): ...
Import
from dm_control.viewer import user_input
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| mouse | GlfwMouse | Yes | Mouse device handler with on_click, on_double_click, on_move, on_scroll observables |
| keyboard | GlfwKeyboard | Yes | Keyboard device handler with on_key observable |
| callback | callable | Yes (bind) | Function to invoke when the key binding is triggered |
| key_binding | int, tuple, Exclusive, DoubleClick, or Range | Yes (bind) | Key combination specification |
Outputs
| Name | Type | Description |
|---|---|---|
| (dispatched callbacks) | None | Callbacks are invoked with appropriate arguments: no args for simple bindings, True/False for Exclusive, index for Range |
Usage Examples
Basic Usage
from dm_control.viewer import user_input
# Create an input map (requires mouse and keyboard handlers)
input_map = user_input.InputMap(mouse, keyboard)
# Bind a simple key press
def on_escape():
print('Escape pressed')
input_map.bind(on_escape, user_input.KEY_ESCAPE)
# Bind a key with modifier
def zoom_to_scene():
print('Zoom to scene')
input_map.bind(zoom_to_scene, (user_input.KEY_A, user_input.MOD_CONTROL))
Exclusive and Range Bindings
from dm_control.viewer import user_input
# Bind an exclusive (press-and-hold) action
def pan_camera(enable):
if enable:
print('Start panning')
else:
print('Stop panning')
input_map.bind(pan_camera, user_input.Exclusive(user_input.MOUSE_BUTTON_RIGHT))
# Bind a range of keys to a single indexed callback
def toggle_group(index):
print(f'Toggle group {index}')
input_map.bind(toggle_group, user_input.Range(
[user_input.KEY_0, user_input.KEY_1, user_input.KEY_2]))