The equivalent way of Input.GetKeyDown() in new input system
I am new to using the new input system.
I am wondering how to do something similar to Input.GetKeyDown() ?
I just want the variable isKPressedThisFrame to be true at the frame I press down the key, but the code followed will not set isKPressedThisFrame to false if I am holding down the key ( the inputAction.cancelled event will not be triggered if I don't release the key)
float isKPressedThisFrame;
public void OnKPress(){
isKPressedThisFrame = true;
}
public void OnKRelease(){
isKPressedThisFrame = false;
}
public void OnEnable(){
inputAction.Performed += OnKPress;
inputAction.Cancelled += OnKRelease;
}
Answer by KatharinaBrand · Feb 09, 2020 at 09:30 AM
For GetKeyDown the documentation says you should use
Keyboard.current.spaceKey.wasPressedThisFrame
I dont think I understand your question correctly because from your code Id expect isKPressedThisFrame to be true as long as you hold the button. Why should cancelled be called if it hasnt been released?
But if you say you only want the first frame you could do something like this
private void Update(){
if (isKPressedThisFrame)
isKPressedThisFrame = false;
}
Haven't tested it, maybe it needs to run as a Late Update