0

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);

                            }
Faizan
  • 1,847
  • 8
  • 40
  • 63
  • Are you using a Kinect sensor and you want the user to do an action which will trigger the key press? I have just seen you mentioned the kinect in a comment below and I thought I would ask. You might want to add that fact into your question and flags as someone with knowledge about kinect may be able to help you better. – Keithin8a May 20 '15 at 09:27

3 Answers3

1

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

SendKeys.Send("{LEFT}");

Here is the complete code list.

Chostakovitch
  • 965
  • 1
  • 9
  • 33
  • I did add the brackets, but still the same problem. My mouse stops get controlled. Also, if I comment out this entire line, every things works fine. What are the prerequisites of using SendKeys function? Any special libraries to be included, how? – Faizan May 19 '15 at 13:42
  • Can you put a break point on this call in Visual Studio and check the behaviour of your code after this line ? – Chostakovitch May 19 '15 at 13:48
1
SendKeys.Send("{LEFT}");
SendKeys.Send("{RIGHT}");
Ashutosh Vyas
  • 419
  • 4
  • 17
  • Still not working bro! Please help... What do I need to include in my code to make this run? – Faizan May 19 '15 at 13:43
  • So I just figured out, if I comment out the SendKeys.Send("RIGHT"); then my mouse controll works but still the left key is not pressed. The code is running in infinite loop because it is using kinect user skeleton. Is that some that is creating the problem/ – Faizan May 19 '15 at 13:47
  • These are two seperate key strokes. `SendKeys.Send("{LEFT}")` would send LEFT Arrow Key and `SendKeys.Send("{RIGHT}")` will send the RIGHT arrow key. – Ashutosh Vyas May 20 '15 at 07:41
0

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.

Rich S
  • 418
  • 3
  • 8
  • How will this work for arrow keys of keyboard left and right? I want to press those buttons programmatically once a condition is true. – Faizan May 19 '15 at 13:57
  • Sorry yes I should have used KeyPress events instead, but works the same way. I suspect you don't really want to press the buttons programatically, but rather you want the effect of pressing the button to run: https://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs%28v=vs.110%29.aspx – Rich S May 19 '15 at 14:00