Skip to content

Keyboard & Mouse ​

php-vio provides comprehensive input handling through the active window platform – its native Win32, Cocoa, X11 or Wayland layer, or GLFW (see vio_platform()). Key codes are the same on every platform. You can poll state per-frame or register event callbacks.

Keyboard ​

Polling ​

php
// Is the key currently held down?
if (vio_key_pressed($ctx, VIO_KEY_W)) {
    $y -= $speed;
}

// Was the key just pressed this frame? (edge-triggered)
if (vio_key_just_pressed($ctx, VIO_KEY_SPACE)) {
    jump();
}

// Was the key just released this frame?
if (vio_key_released($ctx, VIO_KEY_LEFT_SHIFT)) {
    stopSprinting();
}

Text Input ​

php
// Get all characters typed this frame as UTF-8
$chars = vio_chars_typed($ctx);
if ($chars !== "") {
    $inputBuffer .= $chars;
}

Key Callbacks ​

php
vio_on_key($ctx, function(int $key, int $action, int $mods) {
    if ($key === VIO_KEY_F11 && $action === VIO_PRESS) {
        // Toggle fullscreen
    }

    // Check modifiers
    if ($mods & VIO_MOD_CONTROL) {
        // Ctrl is held
    }
});

Character Callback ​

php
vio_on_char($ctx, function(int $codepoint) {
    $char = mb_chr($codepoint);
    echo "Typed: $char\n";
});

Input Injection (Testing) ​

Simulate key presses without a real keyboard:

php
vio_inject_key($ctx, VIO_KEY_W, VIO_PRESS);
vio_poll_events($ctx);

assert(vio_key_pressed($ctx, VIO_KEY_W) === true);

vio_inject_key($ctx, VIO_KEY_W, VIO_RELEASE);

Mouse ​

Position ​

php
[$x, $y] = vio_mouse_position($ctx);

// Movement since last frame
[$dx, $dy] = vio_mouse_delta($ctx);

Buttons ​

php
if (vio_mouse_button($ctx, VIO_MOUSE_LEFT)) {
    // Left button held
}
ConstantButton
VIO_MOUSE_LEFTLeft click
VIO_MOUSE_RIGHTRight click
VIO_MOUSE_MIDDLEMiddle click / wheel

Scroll ​

php
[$scrollX, $scrollY] = vio_mouse_scroll($ctx);
// scrollY > 0 = scroll up, scrollY < 0 = scroll down

Mouse Injection (Testing) ​

php
vio_inject_mouse_move($ctx, 400.0, 300.0);
vio_inject_mouse_button($ctx, VIO_MOUSE_LEFT, VIO_PRESS);

Resize Callback ​

php
vio_on_resize($ctx, function(int $width, int $height) {
    vio_viewport($ctx, 0, 0, $width, $height);
    // Update projection matrix...
});

Key Constants ​

All key constants follow GLFW naming with a VIO_KEY_ prefix:

php
VIO_KEY_A ... VIO_KEY_Z
VIO_KEY_0 ... VIO_KEY_9
VIO_KEY_F1 ... VIO_KEY_F12
VIO_KEY_SPACE, VIO_KEY_ENTER, VIO_KEY_TAB, VIO_KEY_BACKSPACE
VIO_KEY_ESCAPE, VIO_KEY_DELETE, VIO_KEY_INSERT
VIO_KEY_UP, VIO_KEY_DOWN, VIO_KEY_LEFT, VIO_KEY_RIGHT
VIO_KEY_LEFT_SHIFT, VIO_KEY_LEFT_CONTROL, VIO_KEY_LEFT_ALT
// ... and many more

See Constants Reference for the complete list.

Modifier Bitmask ​

ConstantValue
VIO_MOD_SHIFT0x0001
VIO_MOD_CONTROL0x0002
VIO_MOD_ALT0x0004
VIO_MOD_SUPER0x0008
VIO_MOD_CAPS_LOCK0x0010
VIO_MOD_NUM_LOCK0x0020

Actions ​

ConstantMeaning
VIO_RELEASEKey/button was released
VIO_PRESSKey/button was pressed
VIO_REPEATKey is being held (auto-repeat)

Released under the MIT License.