WebGLKeyHijack - jimdroberts/FishMMO GitHub Wiki
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.
- public int[] HijackKeyCodes Array of key codes to hijack in the browser. Prevents default browser actions for these keys.
- 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.
- Attach the
WebGLKeyHijack
script to a GameObject in your WebGL scene. - Assign the
HijackKeyCodes
array in the Inspector with the key codes you want to hijack (e.g., F5, Ctrl+R, etc.). - The script will automatically set up the browser listeners on Awake for WebGL builds.
// 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 };
}
}
- 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.