Slavisha
Slavisha

Reputation: 219

How to use Control.KeyUp Event in my code?

I am having issue ( or lack of talent ) with my C# code. I am using this code to control Robot via bluetooth and i have it working fine when I press: W - forward, S - Backward, A - Left, D - Right, Q - Forward Left, E - Forward Right, Z - Backward left, X - Backward Right, O - lift the robot arm, P - Put down the arm, N - Open the grip, M - Close the Grip, Y - Stop all command above,

Now these are handled through Keydown command and they work fine. The problem is that i do not know how stop any of these command when I am not pressing any of the keys. As temporary fix I have been using "Y" key which stops all actions but I am sure there is a better way to address this using keyUp or something like that.

Could you please give me just one example with maybe my command W how should i handle that if the W is not pressed anymore and i want robot to stop?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;

namespace RoverControl
{
    public partial class Form1 : Form
    {
        MJPEGStream stream;

        public Form1()
        {
            InitializeComponent();
        }

        // Declare the comands for Rover control//
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "W")     // Keyboard characeter "W" //
                try
                {
                    serialPort1.Write("F");     // Passing the command "Forward" through letter "F" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover."); // Exception in the case letter "W" is pressed without connection being established//
                }

            if (e.KeyCode.ToString() == "S")    // Keyboard characeter "S" //
                try
                {
                    serialPort1.Write("B");     // Passing the command "Backward" through letter "B" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "A")    // Keyboard characeter "A" //
                try
                {
                    serialPort1.Write("L");     // Passing the command "Left" through letter "L" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "D")    // Keyboard characeter "D" //
                try
                {
                    serialPort1.Write("R");    // Passing the command "Right" through letter "R" in arduino code//
                }
                catch (Exception)
                {
                MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Q")    // Keyboard characeter "Q" //
                try
                {
                serialPort1.Write("G");    // Passing the command "Forward Left" through letter "G" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "E")    // Keyboard characeter "E" //
                try
                {
                    serialPort1.Write("I");    // Passing the command "Forward Right" through letter "I" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Z")    // Keyboard characeter "Z" //
                try
                {
                    serialPort1.Write("H");    // Passing the command "Backward Left" through letter "H" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "X")    // Keyboard characeter "X" //
                try
                {
                    serialPort1.Write("J");    // Passing the command "Backward Right" through letter "J" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "O")    // Keyboard characeter "O" //
                try
                {
                    serialPort1.Write("O");    // Passing the command "Up" through letter "O" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "P")    // Keyboard characeter "P" //
                try
                {
                    serialPort1.Write("P");    // Passing the command "Down" through letter "P" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "N")    // Keyboard characeter "N" //
                try
                {
                    serialPort1.Write("N");    // Passing the command "Open" through letter "N" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "M")    // Keyboard characeter "M" //
                try
                {
                    serialPort1.Write("M");    // Passing the command "Close" through letter "M" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "C")    // Keyboard characeter "C" //
                try
                {
                    serialPort1.Write("C");    // Passing the command "" through letter "C" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "c")    // Keyboard characeter "c" //
                try
                {
                    serialPort1.Write("c");    // Passing the command "" through letter "c" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "V")    // Keyboard characeter "V" //
                try
                {
                    serialPort1.Write("V");    // Passing the command "" through letter "V" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "v")    // Keyboard characeter "v" //
                try
                {
                    serialPort1.Write("v");    // Passing the command "" through letter "v" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "U")    // Keyboard characeter "U" //
                try
                {
                    serialPort1.Write("U");    // Passing the command "" through letter "U" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "u")    // Keyboard characeter "u" //
                try
                {
                    serialPort1.Write("u");    // Passing the command "" through letter "u" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Y")    // Keyboard characeter "Y" //
                try
                {
                    serialPort1.Write("S");    // Passing the command "Stop" through letter "S" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
        }


            void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bmp = (Bitmap) eventArgs.Frame.Clone();
            pictureBox1.Image = bmp;
        }

        private void Start_Click(object sender, EventArgs e)
        {
            string IP = "";
            IP = textBox3.Text;
            stream = new MJPEGStream(IP);
            stream.NewFrame += stream_NewFrame;
            try
            {
            stream.Start();
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter valid IP address.");
            }
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            stream.Stop();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string COM = "";
            COM = comboBox1.Text;
            serialPort1.PortName = COM;
            serialPort1.BaudRate = 9600;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();
            }
            catch (Exception)
            {
                var dialogResult = MessageBox.Show("Please select correct Comunication Port.");
            }
        }

        private void Disconnect_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
        }

    }
}

Ok so when i add the code you suggested i am still having same issue. I press W to move forward and when i release the W key it still going forward. Did I edited code wrongly?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;

namespace RoverControl
{
    public partial class Form1 : Form
    {
        MJPEGStream stream;

        public Form1()
        {
            InitializeComponent();
        }

        // Declare the comands for Rover control//
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.ToString() == "W")     // Keyboard characeter "W" //
                try
                {
                    serialPort1.Write("F");     // Passing the command "Forward" through letter "F" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover."); // Exception in the case letter "W" is pressed without connection being established//
                }

            if (e.KeyCode.ToString() == "S")    // Keyboard characeter "S" //
                try
                {
                    serialPort1.Write("B");     // Passing the command "Backward" through letter "B" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "A")    // Keyboard characeter "A" //
                try
                {
                    serialPort1.Write("L");     // Passing the command "Left" through letter "L" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "D")    // Keyboard characeter "D" //
                try
                {
                    serialPort1.Write("R");    // Passing the command "Right" through letter "R" in arduino code//
                }
                catch (Exception)
                {
                MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Q")    // Keyboard characeter "Q" //
                try
                {
                serialPort1.Write("G");    // Passing the command "Forward Left" through letter "G" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "E")    // Keyboard characeter "E" //
                try
                {
                    serialPort1.Write("I");    // Passing the command "Forward Right" through letter "I" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Z")    // Keyboard characeter "Z" //
                try
                {
                    serialPort1.Write("H");    // Passing the command "Backward Left" through letter "H" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "X")    // Keyboard characeter "X" //
                try
                {
                    serialPort1.Write("J");    // Passing the command "Backward Right" through letter "J" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "O")    // Keyboard characeter "O" //
                try
                {
                    serialPort1.Write("O");    // Passing the command "Up" through letter "O" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }

            if (e.KeyCode.ToString() == "P")    // Keyboard characeter "P" //
                try
                {
                    serialPort1.Write("P");    // Passing the command "Down" through letter "P" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "N")    // Keyboard characeter "N" //
                try
                {
                    serialPort1.Write("N");    // Passing the command "Open" through letter "N" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "M")    // Keyboard characeter "M" //
                try
                {
                    serialPort1.Write("M");    // Passing the command "Close" through letter "M" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "C")    // Keyboard characeter "C" //
                try
                {
                    serialPort1.Write("C");    // Passing the command "" through letter "C" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "c")    // Keyboard characeter "c" //
                try
                {
                    serialPort1.Write("c");    // Passing the command "" through letter "c" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "V")    // Keyboard characeter "V" //
                try
                {
                    serialPort1.Write("V");    // Passing the command "" through letter "V" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "v")    // Keyboard characeter "v" //
                try
                {
                    serialPort1.Write("v");    // Passing the command "" through letter "v" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "U")    // Keyboard characeter "U" //
                try
                {
                    serialPort1.Write("U");    // Passing the command "" through letter "U" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "u")    // Keyboard characeter "u" //
                try
                {
                    serialPort1.Write("u");    // Passing the command "" through letter "u" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
            if (e.KeyCode.ToString() == "Y")    // Keyboard characeter "Y" //
                try
                {
                    serialPort1.Write("S");    // Passing the command "Stop" through letter "S" in arduino code//
                }
                catch (Exception)
                {
                    MessageBox.Show("Please establish the connection with rover.");
                }
        }
        void Form1_KeyUp(object sender, KeyEventArgs e)
        {
                try
            {
                serialPort1.Write("S");    // Passing the command "Stop" through letter "S" in arduino code//
            }
            catch (Exception)
            {
                MessageBox.Show("Please establish the connection with rover.");
            }
        }

        void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bmp = (Bitmap) eventArgs.Frame.Clone();
            pictureBox1.Image = bmp;
        }

        private void Start_Click(object sender, EventArgs e)
        {
            string IP = "";
            IP = textBox3.Text;
            stream = new MJPEGStream(IP);
            stream.NewFrame += stream_NewFrame;
            try
            {
            stream.Start();
            }
            catch (Exception)
            {
                MessageBox.Show("Please enter valid IP address.");
            }
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            stream.Stop();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string COM = "";
            COM = comboBox1.Text;
            serialPort1.PortName = COM;
            serialPort1.BaudRate = 9600;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Open();
            }
            catch (Exception)
            {
                var dialogResult = MessageBox.Show("Please select correct Comunication Port.");
            }
        }

        private void Disconnect_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
        }

    }
}

Upvotes: 0

Views: 498

Answers (2)

Chandrashekar Jupalli
Chandrashekar Jupalli

Reputation: 347

Repalce constructor:

        public Form1()
        {
           InitializeComponent();
           this.KeyUp += new KeyEventHandler(Form1_KeyUp);
        }

then add

private void Form1_KeyUp(object sender, KeyEventArgs e)
            {                
                if (e.KeyCode.ToString() == "S" || e.KeyCode.ToString() == "W")//Check conditions
                {
                    try
                    {
                        serialPort1.Write("S");    // Passing the command "Stop" through letter "S" in arduino code//
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Please establish the connection with rover.");
                    }
                }
            }

Upvotes: 0

Calvin Zhang
Calvin Zhang

Reputation: 11

first of all, it is not recommended to use non-realtime system to control moving things. this includes the bluetooth communication protocol and the PC itself (more specifically, Windows OS). such systems will never be able to pass any commercial or industrial tests - for obvious safety issues.

now back to your questions. what i would do is,

  • define the protocol between PC and the robotic controller in a "state" manner, instead of a event-driven manner. for example, instead of sending the event that the "move forward" key has just been pressed or released, we should send the state that describes whether "move forward" key is being pressed. the robot side doesn't need to know when the key is pressed or released, it only needs to know whether it should move forward.

  • on the PC side, maintain a state machine which, when in running mode, send the key state regularly, say every 10ms.

  • to detect the current key state, you can either detect it directly via Keyboard.IsKeyDown() method if you use WPF, or maintain your own state with KeyUp / KeyDown events with WinForm

  • on the robotic controller side, things gets relatively easy because most of them runs in a state machine manner, and that eventually you have to convert the control commands into single bit digital output to the motor or servo. and the state-based communication protocol works perfectly with this. for example, the "move forward key is being pressed" state translates directly to "turn on digital output xxx for the X axis motor".

hope it helps.

Upvotes: 1

Related Questions