winzik/engine/tests/Input/PhysicalKeysTest.php
David Nurdin b05cdc82e3 JumPHP 0.5.52: physical keys in the browser (Keyboard::physicalHeld())
Keyboard::physicalHeld() and physicalPressed(): the physical keys as SDL
scancodes (where the key is, whatever the layout), what a remote console
(VNC / QEMU) needs so the remote machine applies its own layout. Desktop:
SDL's keyboard state. Browser: SDL's emscripten port makes its scancodes from
the character (AZERTY's "a" key came out as A), so the page sends
KeyboardEvent.code itself (jumphp-device.js, JsBridge 'physkey');
physicalPressed() keeps a quick tap that went down and up between two frames.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-11 05:51:35 +01:00

32 lines
1.1 KiB
PHP

<?php
namespace JumPHP\Tests\Input;
use JumPHP\Input\Keyboard;
use PHPUnit\Framework\TestCase;
/** The physical keys the page reports (KeyboardEvent.code), as SDL scancodes: what a remote console needs. */
final class PhysicalKeysTest extends TestCase
{
public function testThePageCodesBecomeScancodesWhateverTheLayout(): void
{
$k = new Keyboard();
$k->physical('KeyQ', true); // the key AZERTY prints "a" on
$k->physical('ShiftLeft', true);
self::assertSame([20, 225], $k->physicalHeld());
$k->physical('KeyQ', false);
self::assertSame([225], $k->physicalHeld());
$k->physical('NotAKey', true);
self::assertSame([225], $k->physicalHeld(), 'an unknown code is ignored');
}
public function testEveryKeyIsLetGoWhenThePageLosesTheFocus(): void
{
$k = new Keyboard();
$k->physical('KeyA', true);
$k->physical('IntlBackslash', true); // the < > key of European keyboards
self::assertSame([4, 100], $k->physicalHeld());
$k->physical('', false);
self::assertSame([], $k->physicalHeld());
}
}