I was wondering how you would do this in unity C# , I need to trigger enter key press . I know it is possible to do this in WPF(visual studio) C# but not in unity.
Thanks in advance. ![]()
I’m not sure whether there’s an easier way, but you could always write your own input manager that allows you to do just this.
Before going ahead and implementing one, I’d wait for some more replies though.
Thanks but I thought there might be a simpler way I mean In visual C# i can do that using this code. So it should work in unity C# as well right in some modified form?
var key =Key.A;// Key to sendvar target =Keyboard.FocusedElement;// Target elementvar routedEvent =Keyboard.KeyDownEvent;// Event to send
target.RaiseEvent(newSystem.Windows.Input.KeyEventArgs(Keyboard.PrimaryDevice,System.Windows.PresentationSource.FromVisual((Visual)target),0, key){RoutedEvent= routedEvent });
Unity isn’t using .net, but mono. System.Windows.Input isn’t available. Look for an alternative.
Why would you ever need this, if you do your input code is too tightly coupled with your logic.
Unfortunately there is no easy answer for this. The two solutions suggested already are worth pursuing.
I would suggest decoupling as being the better to implement straight off the bat.
I want this for testing on Test Runner.
I wander if there is a way to throw a key press event on the EventSystem or something.