Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Sending KeyDowns to Unity

Discussion in 'Web' started by ricardoCoelho, Jul 24, 2020.

  1. ricardoCoelho


    Jul 6, 2020
    I'm working on an web application that uses Unity as a 3D visualiser of objects.
    I'm running Unity with 'WebGLInput.captureAllKeyboardInput = false;' so that I can type outside of the Unity window. Problem is, my camera controlling script uses keyDown events to change behaviours
    (right click + mouse drag = orbit camera around object)
    (right click + alt down + mouse drag = pan camera)

    The way I'm making this work is when the user presses ALT, I'm executing: " unityInstance.SendMessage('Main', 'ThisKeyIsDown', 'Left Alt Key'); "
    and when the user releases the ALT key, I execute:
    " unityInstance.SendMessage('Main', 'ThisKeyIsUp', 'Left Alt Key'); "

    This is just one behaviour of what I'm planning to use with keydowns and if I wanted to use more keys, I'd have to repeat that behaviour for every key I want to use.
    Is there a way to convert the JS keycodes to Unity keycodes?
    This conversion can occur on either side, Unity or JS
    I'm looking for an algorithm to convert the keys without having to manually type the entire conversion table manually for the entire keyboard like so:

    Left Alt = JS 18 | Unity 308
    Left Control = JS 17 | Unity 306
    F = JS 70 | Unity 102

    JS keycodes:
    Unity keycodes: