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. Dismiss Notice

Method of detecting key code with new input system (without declaring that key)

Discussion in 'Input System' started by ypvypv, May 2, 2022.

  1. ypvypv

    ypvypv

    Joined:
    Apr 16, 2019
    Posts:
    7
    Hi,

    I believe this question has been asked many times before but I can't get a straight answer from searching the forum.

    There is a function that processes any key press but it requires to know what that key is:

    Code (CSharp):
    1. public void OnKeyDown(KeyCode key)
    2. {
    3.   ...
    4.   e.KeyCode = key;
    5.   OnEvent(e);
    6.   ...
    7. }
    It is registered with the unity input system like this:
    Code (CSharp):
    1. UI.S.started += ctx => OnKeyDown(KeyCode.S);
    I am looking for a way to do this without looping through a list of available keys as it is inefficient. I just would like to know what key code I am getting from the input system, without explicitly registering that key, as is done above.

    The motivation is simple: I have a system that I do not know which hot key the software will want to use in the future and I do not want and am unable to bind keys explicitly in code because I do not own all the code that is interested in using the input system.

    Thanks in advance!
     
  2. dmytro_at_unity

    dmytro_at_unity

    Unity Technologies

    Joined:
    Feb 12, 2021
    Posts:
    212
    "ctx.control" in the callback will be pointing to a control that triggered it, from there you can type check if it's "KeyControl" and that has keyCode and scanCode, hope the helps.