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.
LetchfordPAsked:
Who is Participating?
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.

akbCommented:
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.
0
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
0
akbCommented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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?
0
akbCommented:
You need to set events on your form to capture each time a key is pressed or up.
0
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?
0
akbCommented:
There is a very basic tutorial here: https://www.youtube.com/watch?v=f7PTEXzXZmY
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.
0

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
0
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.