r/Unity3D•
milotoad

How to program keycodes

I was wondering how to send a keycode that would be as if I was holding down the arrow key because right now I am using the input managers first 2 indices for the arrow keys. any ideas?

Posted Dec 14, 2014, 6:24 PM UTC

010
AlekaeiAdapterGamesStudio•

do you mean like press the left arrow key and then have a character keep moving left even when you let go and the stop moving as soon as you let go?

1
milotoadOP•

I just want to emulate the key signal for me holding down the left arrow key

1
poisonpage•

You need to parametrize the KeyCode. If you look up the Command Pattern, this covers the basic idea of injecting or emulating actions, inputs, commands, etc. To break it down simply, the actor that can receive inputs should be able to receive them real-time from your keyboard AND injected as a parameter. I see someone evaluating a key press in update such as:

If ( input.getkeydown( KeyCode.leftarrow) ){ //move left code }

So instead, you can do this within your actor class:

void Update(){

If ( input.getkeydown( KeyCode.leftarrow) ){ MoveActor( KeyCode.leftArrow ); }

}

Public void MoveActor( KeyCode p_keycode ){

Switch (p_keycode){ Case KeyCode.leftarrow: //move left code Break; }

Now, MoveActor method is public, so from your replay/emulation/macro class, just call:

actorInstance.MoveActor( KeyCode.Left );

This is a very, very crude example and but is laid out so you can see how the KeyCode input is decoupled from the actor so you can inject input however and whenever you'd like to.

2
AlekaeiAdapterGamesStudio•

what do you want to do with the emulation and can i have your current code

1
AlekaeiAdapterGamesStudio•

c#?? or javascript??

1
milotoadOP•

just wanted send the game the signal with a toggle of some sort that I am holding down the arrow key

1
WiredEarp•

Are you wanting to send input to your own game, or another game/app?

1
milotoadOP•

To my own game. At the moment I am using the built-in input manager

1
WiredEarp•

As a quick guess, I'd go with something similar to FreeTimeDev's answer in the link you posted. In your current code where it says (for example) 'if(input.getkeydown(keycode.leftarrow) just OR it with the bool, so

'if((input.getkeydown(keycode.leftarrow) || bLeftArrowDown) { do movement code }.

1