Using keyboard to control PTZ camera

I am trying to control a PTZ camera using the keyboard arrow keys this method has two problems one it is running far too fast and sending out too many serial commands, and the second problem is when I let go of the key I want to send a stop command to the camera.
To detect the keypress I am using this code:
 Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
    ByVal keyData As Keys) As Boolean

        Select Case keyData
            Case Keys.Down
                Call tiltdown1()
            Case Keys.Up
                Call tiltup1()
            Case Keys.Right
                Call Panright1()
            Case Keys.Left
                Call Panleft1()
            Case Keys.M
                Call zoomout1()
            Case Keys.N
                Call zoomin1()
            Case Keys.S
                Call allStop1()

        End Select
please remember this is a two wire link with no handshaking to the serial port at 2400 baud.
So my question again if I press for example the left arrow key I just want the program to send one pan left command and then when I release the key send one stop command.

Any help would be much appreciated. Please remember I'm a very very new user.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I think what may be happening is that when a key is pressed you are sending multiple commands to the camera instead of just one. If the operator holds the key down for a few seconds your program is probably looping a few hundred times and sending a few hundred commands. By the time the stop command is pressed the camera is still processing all those other commands.
The solution will depend on how your camera works.
If it keeps panning after a single command until the stop command is received then you will need to make sure only one pan command is sent. You could do this with a variable which you set when a key is pressed and then check that variable before sending another command.
If the camera only moves a little bit with each command then you will need to use a timer to control the rate the commands are sent so there aren't extra commands waiting in the camera's serial buffer to be processed.
LetchfordPAuthor Commented:
Thank you for your reply
This camera run's until the stop command is sent so I just need to send 1 command to start but how do you do it?
And any thoughts on how send the stop command when you when you stop pressing a key?  
I am very new to this
You need to set a variable when a key is pressed.
Each time you process the keypress (or keydown) event you should first check the variable. If you have already reacted to this key press then don't react again.
You also need to check for keyup event. On keyup you need to send the stop commend.

The logic might go as follows:

Set LastKeyDown variable to "".

On KeyPress
  If KeyPressed=LastKeyDown then exit function.
  If KeyPressed=Down then send tilt down command.
  If KeyPressed=Up then send tilt up  command.
  If KeyPressed=Left then send tilt left command.
  If KeyPressed=Right then send tilt right command.
  and so on for all your commands.
 Set LastKeyDown variable to KeyPressed.
end keypress function.

On KeyUp
 Send stop command.
 Set LastKeyDown variable to KeyPressed.
end keyup function.

Obviously you will need to convert this logic to your VB code.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

LetchfordPAuthor Commented:
Hello there
yes I understand the logic, but don't understand the coding.
For instance how do you implement key up does not seem to work within that loop plan currently using?
You need to set events on your form to capture each time a key is pressed or up.
LetchfordPAuthor Commented:
Hi Akb
Sorry I need a VB example as I do not know what you mean.
Please can you show me a example?
There is a very basic tutorial here:
It shows you how to use the keydown event in an edit box.
Use the same principal to put a keypress and keyup event on your form.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
LetchfordPAuthor Commented:
Need something simple no good just say do that, sometimes you got to show an example
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.