Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 14, 2025

This PR implements a streamlined input event management system for JSAR Runtime, enabling host applications to dispatch keyboard events globally to all active content runtimes.

Architecture Overview

The implementation provides clean separation between event capture and event management:

Host Application (Unity/Unreal/Native)
    ↓ (captures platform-specific input)
InputManager (in Constellation)
    ↓ (IPC via InputEvent)
All Active JSAR Content Runtimes
    ↓ (standard DOM KeyboardEvents)
Web Applications

Key Components

1. InputManager Module (src/runtime/input-manager/)

  • TrInputManager: Central class for managing global input event dispatch
  • Integrated into TrConstellation for proper lifecycle management
  • Host-driven design - no direct system event listening
  • Global event dispatching to all active content runtimes simultaneously

2. Extended Event System

  • New InputEvent type added to native event system with keyboard support
  • Proper serialization/deserialization for IPC transport
  • Focused on keyboard events only (no mouse events)

3. Simple Embedder API

// Dispatch keyboard event globally to all content runtimes
bool dispatchKeyboardEvent(const TrKeyboardEventData& eventData);

// Helper functions for common events
auto keyDown = input_manager::keyboard_events::createKeyDownEvent("a", "KeyA", false, true, false, false);

Usage Example

Host Application (C++):

class MyApp : public TrEmbedder {
public:
    void onUserInput(const std::string& key) {
        auto eventData = input_manager::keyboard_events::createKeyDownEvent(
            key, getKeyCode(key), false, false, false, false);
        dispatchKeyboardEvent(eventData);
    }
};

Web Application (JavaScript):

// Standard DOM event listeners work automatically
document.addEventListener('keydown', function(event) {
    console.log('Key:', event.key, 'Code:', event.code);
    console.log('Modifiers:', event.ctrlKey, event.altKey);
});

Design Benefits

  • Global by Design: All keyboard events are automatically dispatched to every active content runtime
  • Platform Independence: Host applications handle platform-specific input capture
  • Standard Compliance: Generates standard DOM KeyboardEvents
  • Performance: Efficient IPC with minimal overhead
  • Simplicity: Single dispatch method with no content targeting complexity
  • Accessibility: Enables keyboard accessibility in spatial web applications

Testing

  • Unit tests for InputManager and event serialization
  • Helper function tests for keyboard event creation

This implementation provides a focused, global approach to keyboard input management suitable for spatial web environments where input events should be available to all active content.

Fixes #209.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits August 14, 2025 14:35
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add InputManager module and IPC-driven KeyboardEvent support (host-driven input injection) Add InputManager module and IPC-driven KeyboardEvent support (host-driven input injection) Aug 14, 2025
Copilot finished work on behalf of yorkie August 14, 2025 14:49
@Copilot Copilot AI requested a review from yorkie August 14, 2025 14:49
Copy link
Member

@yorkie yorkie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot See my reviews, and:

  1. Revert the changes on examples
  2. Revert all the JavaScript/TypeScript changes

}

// Initialize InputManager after constellation is created
inputManager = make_shared<input_manager::TrInputManager>(constellation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

input manager should be in constellation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved InputManager initialization to constellation.cpp instead of embedder.cpp. The InputManager is now created as part of TrConstellation and accessed via constellation->inputManager. Commit: 69ed0f1

* @param eventData The keyboard event data
* @return true if the event was successfully injected, false otherwise
*/
bool injectKeyboardEvent(uint32_t contentId, const TrKeyboardEventData &eventData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool injectKeyboardEvent(uint32_t contentId, const TrKeyboardEventData &eventData);
bool dispatchKeyboardEvent(const TrKeyboardEventData &eventData);

Keyboard event is a global event, no need the content id to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed method name to dispatchKeyboardEvent and removed the contentId parameter. Keyboard events are now globally dispatched to all active content runtimes. Commit: 69ed0f1

* @param eventData The keyboard event data
* @return number of content runtimes that received the event
*/
int broadcastKeyboardEvent(const TrKeyboardEventData &eventData);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto, remove this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the broadcastKeyboardEvent method as requested. Now there's only one dispatchKeyboardEvent method that handles global dispatch. Commit: 69ed0f1

* This class provides an abstraction layer between platform-specific input
* capture (Unity, Unreal, native, etc.) and JSAR Runtime's event system.
*/
class TrInputManager
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please redesign this class as the input event is globally at runtime, we don't need to dispatch a keyboard event to a specific content, each input/keyboard event should be dispatched for both listened contents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redesigned the InputManager class to handle global input events. Removed content-specific methods and simplified the API to only support global dispatch via dispatchKeyboardEvent. All keyboard events are now dispatched to all listening content runtimes automatically. Commit: 69ed0f1

KeyboardPress,
MouseDown,
MouseUp,
MouseMove,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MouseMove can't be exist at this project, because we don't allow content receive the native window's mouse move event, as for MouseDown and MouseUp, it has been handled by WebXR input source such as primary actions, so you need to remove them here, too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed MouseMove, MouseDown, and MouseUp from the TrInputEventType enum. The input event system now only supports keyboard events (KeyboardDown, KeyboardUp, KeyboardPress). Commit: 69ed0f1

…plify API, remove examples and TS changes

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 14, 2025

@copilot See my reviews, and:

  1. Revert the changes on examples
    ...

Done! I've reverted all changes to the examples directory and all JavaScript/TypeScript changes as requested. Commit: 69ed0f1

@Copilot Copilot AI changed the title Add InputManager module and IPC-driven KeyboardEvent support (host-driven input injection) Add InputManager with global keyboard event dispatching Aug 14, 2025
@Copilot Copilot AI requested a review from yorkie August 14, 2025 16:10
Copilot finished work on behalf of yorkie August 14, 2025 16:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add InputManager module and IPC-driven KeyboardEvent support (host-driven input injection)
2 participants