- Home /
Can you simulate key press within WebView?
I'm not entirly skilled with code but im attempting to use WebView to move a game from browser to mobile. I have the game working and openeing fine, but I need to make UI buttons to siulate keyboard key presses. This is the code i currently have. I am unsure why but it does nothing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
public class MouseSim : MonoBehaviour
{
public bool going;
public KeyboardState currentDown;
public KeyboardState currentUp;
public Keyboard keyboard;
public KeyboardState wDown = new KeyboardState();
public KeyboardState wUp = new KeyboardState();
void Start()
{
keyboard = InputSystem.AddDevice<Keyboard>();
wDown.Press(Key.W);
wUp.Release(Key.W);
}
//When button is pressed this is sent in by the button
public void setKey(string key)
{
if (going)
return;
if (key == "w")
{
currentDown = wDown;
currentUp = wUp;
}
}
//When the button is pressed this is sent in after the setKey("w")
public void setTime(float time)
{
if (going)
return;
StartCoroutine(KeyPress(time));
}
IEnumerator KeyPress(float wait)
{
going = true;
InputSystem.QueueStateEvent(keyboard, currentDown);
yield return new WaitForSeconds((Random.Range(0f, (wait / 10f)) + wait));
InputSystem.QueueStateEvent(keyboard, currentUp);
going = false;
}
}
It is running through all of the functions (Checked with a Debug.Log) and has the values of float time = 1; string key = "w"; I'm unsure where to go from here and if anyone has any ideas of diffrent ways to simulate a key press or even to tell webview a key was pressed then Im open to ideas.
Your answer
Follow this Question
Related Questions
Input system looses keypress after a few frames [BUG?] 0 Answers
Key not recognized 2 Answers
Is it possible to change keyboard input inGame 1 Answer
Distribute terrain in zones 3 Answers
Mobile Keyboard hide on tap screen. 2 Answers