Home

Awesome

XRKeys

npm version language license

A highly performant and plug-and-play WebXR keyboard library for Three.js applications. XRKeys empowers your WebXR applications with typing capabilities at the minimal expense of just two draw calls, while eliminating the need for extra setup such as loading fonts for the keyboard.

Features

Demo App

A demo app has been built using Three.js to showcase XRKeys in action. You can check out the demo app at https://felixtrz.github.io/xrkeys/ to see how XRKeys works and to get a feel for its capabilities.

This demo app is a great resource for developers who are considering using XRKeys in their projects. It provides a hands-on experience with the library and will give you a better understanding of how it works and what it can do.

Installation

To install XRKeys, simply run the following command in your terminal:

$ npm install xrkeys

Or if you prefer using Yarn:

$ yarn add xrkeys

Usage

Using XRKeys is simple. First, import the package in your WebXR project:

import XRKeys from 'xrkeys';

NOTE: NOTE: XRKeys requires Three.js as a peer dependency, make sure to have a recent version installed.

Then, create an instance of the XR keyboard:

const keyboard = await XRKeys.create();

The XRKeys object created extends THREE.Group, and can be used as such. Setup the keyboard and add it to your scene:

// listen for keypress events
keyboard.addEventListener('keypress', (e) => {
	console.log('Key pressed: ' + e.key);
});

// you can bind a custom function to the enter key
keyboard.onEnter = (text) => {
	console.log('Entered text: ' + text);
};

scene.add(keyboard);

Update the keyboard in your render loop:

function render() {
	// update the keyboard with a target ray and a pressing flag
	const targetRaySpace = renderer.xr.getController(0);
	keyboard.update(targetRaySpace, triggerIsPressed);

	// you can also access the keyboard's text content anytime
	console.log(keyboard.text);
}

API Reference

XRKeysConfig type

XRKeysConfig is an object type that can have the following optional properties:

XRKeys class

XRKeys is a class that extends THREE.Group.

Properties

Methods

License

MIT License © 2023 Felix Zhang