none
how to make a particular gesture hold the key of the keyboard? RRS feed

  • Question

  • Hello,

    I want to create a gesture controlled car racing game wherein the user does a particular gesture indicating he wants the car to move towards the right, for instance, extending the right arm. What this requires is, until his arm is in that extended state, the right arrow key should be pressed and as soon as he relaxes back his right arm, the key should be released.

    The below code has two relationships, BEGINNING and ENDING, and two joints being monitored which are defined in the .xml file through which various gestures are defined. What it does is, it monitors for the beginning and ending relationship of the user, and checks if it matches the predefined ones, if it does match then, it presses a key and releases it i.e. it's just like pressing it once. What i require for my game is that it should remain pressed until the ending relationship. Please help me with it....

      
    public class GestureMap
        {
            public List<Gesture> Items;
            public List<string> Messages;
            public bool MessagesWaiting;
            public int GestureResetTimeout;
    
    
            public GestureMap()
            {
                Items = new List<Gesture>();
                Messages = new List<string>();
    
            }
    
    
            public void LoadGesturesFromXml(string xmlfilename)
            {
                Items.Clear();
                var gesture = new Gesture();
                var firstpass = true;
                var mappingdone = false;
    
                Messages.Add("Loading XML File " + xmlfilename + " from application directory");
                MessagesWaiting = true;
    
                try
                {
                    var reader = new XmlTextReader(xmlfilename);
    
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (reader.Name == "Gestures")
                                {
                                    Messages.Add("Reading Header Information from XML File");
                                    MessagesWaiting = true;
                                    ParseHeaderFromXml(reader);
                                }
                                if (reader.Name == "Gesture")
                                {
                                    if (firstpass == false && mappingdone)
                                    {
                                        Items.Add(gesture);
                                        mappingdone = false;
                                    }
    
                                    Messages.Add("Gesture Found in XML File");
                                    MessagesWaiting = true;
                                    gesture = ParseGestureFromXml(reader);
                                    firstpass = false;
                                }
                                if (reader.Name == "GestureComponent")
                                {
                                    Messages.Add("Gesture Component Found in XML File");
                                    MessagesWaiting = true;
                                    gesture = ParseGestureComponentsFromXml(reader, gesture);
                                    mappingdone = true;
                                }
                                break;
                        }
    
                    }
    
                    if (firstpass == false && mappingdone)
                    {
                        Items.Add(gesture);
                    }
    
                }
                catch (Exception ex)
                {
                    Messages.Add("Error Loading XML File: " + ex);
                    MessagesWaiting = true;
                }
    
                Messages.Add("Completed XML File Load");
                MessagesWaiting = true;
            }
    
    
            public void ParseHeaderFromXml(XmlReader reader)
            {
                try
                {
                    var timeout = reader.GetAttribute("GestureResetTimeout");
                    if (timeout != null)
                    {
                        GestureResetTimeout = Convert.ToInt32(timeout);
                    }
                }
                catch (Exception ex)
                {
                    Messages.Add("Error Loading XML File: " + ex);
                    MessagesWaiting = true;
                }
    
            }
    
    
            public Gesture ParseGestureComponentsFromXml(XmlReader reader, Gesture gesture)
            {
                var firstjoint = reader.GetAttribute("FirstJoint");
                var secondjoint = reader.GetAttribute("SecondJoint");
                JointType firstjointtype;
                JointType secondjointtype;
                GestureComponent component;
    
                if (firstjoint != null)
                {
                    firstjointtype = (JointType)(Enum.Parse(typeof(JointType), firstjoint));
                    Messages.Add("First Joint: " + firstjoint);
                }
                else
                {
                    return gesture;
                }
    
                if (secondjoint != null)
                {
                    secondjointtype = (JointType)(Enum.Parse(typeof(JointType), secondjoint));
                    Messages.Add("Second Joint: " + secondjoint);
                }
                else
                {
                    return gesture;
                }
    
                var beginningrelationship = reader.GetAttribute("BeginningRelationship");
                var endingrelationship = reader.GetAttribute("EndingRelationship");
    
    
                var endingrelationshippos = JointRelationship.None;
                var beginningrelationshippos = JointRelationship.None;
    
    
                if (endingrelationship != null)
                {
                    endingrelationshippos = (JointRelationship)(Enum.Parse(typeof(JointRelationship), endingrelationship));
                    Messages.Add("Ending Relationship: " + endingrelationship);
                }
    
                if (beginningrelationship != null)
                {
                    beginningrelationshippos = (JointRelationship)(Enum.Parse(typeof(JointRelationship), beginningrelationship));
                    Messages.Add("Beginning Relationship: " + beginningrelationship);
                }
    
                if (beginningrelationshippos != JointRelationship.None)
                {
                    component = new GestureComponent(firstjointtype, secondjointtype, endingrelationshippos,
                                                         beginningrelationshippos);
                }
                else
                {
                    component = new GestureComponent(firstjointtype, secondjointtype, endingrelationshippos);
                }
    
                gesture.Components.Add(component);
                Messages.Add("Component added to gesture:" + gesture.Description);
    
                return gesture;
            }
    
    
            public Gesture ParseGestureFromXml(XmlTextReader reader)
            {
                var name = reader.GetAttribute("Description");
                var maxexecutiontime = reader.GetAttribute("MaxExecutionTime") ?? "0";
                var keycode = reader.GetAttribute("MappedKeyCode") ?? "NONAME";
    
                Messages.Add("Gesture Name: " + name);
                Messages.Add("Maximum Gesture Execution Time: " + maxexecutiontime);
                Messages.Add("KeyCode Mapping: " + keycode);
                MessagesWaiting = true;
    
                var gesture = new Gesture(name, Convert.ToInt32(maxexecutiontime));
                gesture.MappedKeycode = (VirtualKeyCode)(Enum.Parse(typeof(VirtualKeyCode), keycode));
                gesture.MaximumExecutionTime = Convert.ToInt32(maxexecutiontime);
    
                return gesture;
            }
        }
    
    }
    

    • Moved by CoolDadTx Thursday, January 24, 2013 2:50 PM Kinect related
    Thursday, January 24, 2013 2:23 PM

All replies

  • SendKeys would be the easiest method to send keyboard input to the game. You need to grab the programs processid first though. Heres some sample sendkeys code (its in vb.net but it would be very easy to change to c#):

         Dim WinHandle as Long
         WinHandle = Shell("calc.exe", 1)   ' Run Calculator
         AppActivate WinHandle    ' Activate the Calculator
         SendKeys.Send("keys to send here")
    

    Another method would be to use SendInput api in windows.


    Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth. - "Sherlock holmes" "speak softly and carry a big stick" - theodore roosevelt. Fear leads to anger, anger leads to hate, hate leads to suffering - Yoda. Blog - http://www.computerprofessions.co.nr

    Thursday, January 31, 2013 6:35 PM