WebGLKeyHijack - jimdroberts/FishMMO GitHub Wiki

Description

WebGLKeyHijack is a class that hijacks web browser key listeners to help WebGL builds stay contained within the browser. It prevents certain keys from executing browser functions while the WebGL game is focused. Not intended for malicious use; solely for improving the WebGL game experience.


API Access

Fields

  • public int[] HijackKeyCodes Array of key codes to hijack in the browser. Prevents default browser actions for these keys.

Methods

  • void Awake() Called when the script instance is being loaded. Sets up key hijacking for WebGL builds.
  • public void ClientQuit() Quits the WebGL client by calling the browser-side quit function.

Basic Usage

Setup

  1. Attach the WebGLKeyHijack script to a GameObject in your WebGL scene.
  2. Assign the HijackKeyCodes array in the Inspector with the key codes you want to hijack (e.g., F5, Ctrl+R, etc.).
  3. The script will automatically set up the browser listeners on Awake for WebGL builds.

Example

// Example: Hijacking F5 and Ctrl+R in WebGL
public class WebGLSetup : MonoBehaviour
{
    public WebGLKeyHijack keyHijack;

    void Start()
    {
        // F5 = 116, R = 82 (for Ctrl+R)
        keyHijack.HijackKeyCodes = new int[] { 116, 82 };
    }
}

Best Practices

  • Only use this script in WebGL builds; it has no effect in other platforms.
  • Assign only the necessary key codes to avoid interfering with normal browser behavior.
  • Use ClientQuit() to trigger a browser-side quit action if needed.
  • Ensure your browser-side JavaScript is set up to handle the hijack and quit functions.
⚠️ **GitHub.com Fallback** ⚠️