Faizan
Faizan

Reputation: 1898

Simulate arrows key press in C# WPF

I want to simulate the LEFT and RIGHT Arrows key press when an IF condition gets true. Uptill now I have tried this so far, but it does not works. I am also controlling mouse using win32.dll import method. If I use the SendKeys.Send("LEFT"); method, my mouse movement does not get controlled. I feel like the code does not run after this call.

I have included system.windows.forms at top, what am I doing wrong?

How can I simulate arrows keys in a simple way?

     if (shoulderLeft.Position.Z > shoulderRight.Position.Z)
                            {
                                status.Fill = new SolidColorBrush(System.Windows.Media.Colors.DarkBlue);
                               SendKeys.Send("LEFT");
                               // System.Windows.Forms.MessageBox.Show("Left ROTATION");


                            }
                                //right rotate
                            else if ((shoulderLeft.Position.Z < shoulderRight.Position.Z))
                            {
                                SendKeys.Send("RIGHT");
                                status.Fill = new SolidColorBrush(System.Windows.Media.Colors.Red);

                            }

Upvotes: 0

Views: 3677

Answers (3)

Rich S
Rich S

Reputation: 428

Simulating input events is usually not a good practice, instead wrap the code that runs in your event handler in a parameterized method, and then call that function when you want to simulate the event. For example (slightly contrived):

void myForm_MouseMoved(object sender, MouseEventArgs e)
{
    MoveCharacter(e.X, e.Y);
}

void myForm_SomethingElseHappened(object sender, EventArgs e)
{
    if(IsCharacterTooLow()) 
    {            
        MoveCharacter(_currentPos.X, _currentPos.Y+20)
    }
}

Hope this is useful.

Upvotes: 0

Ashutosh Vyas
Ashutosh Vyas

Reputation: 419

SendKeys.Send("{LEFT}");
SendKeys.Send("{RIGHT}");

Upvotes: 1

Chostakovitch
Chostakovitch

Reputation: 965

You should add brackets around LEFT and RIGHT in your code :

SendKeys.Send("{LEFT}");

Here is the complete code list.

Upvotes: 1

Related Questions