Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How to move cursor using keyboard

I have two vertical cursors and easily I can point to them by mouse and move them with left mouse click. Now, I like to use for example, left and right arrow keys to move the cursor to left and right. How can I do this? I am using measurement Studio 7.1 and c#.
0 Kudos
Message 1 of 9
(6,542 Views)
You can do this by handling the graph's KeyDown event, then check the KeyCode property of the KeyEventArgs that's passed to the KeyDown event handler. If it's Keys.Left, call XYCursor.MovePrevious and if it's Keys.Right, call XYCursor.MoveNext. For example, create a new Windows Forms project, add a WaveformGraph to the form, add a cursor to the graph, add an event handler for the graph's KeyDown event, and then use the following code in the event handler:

private void OnGraphKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Left)
xyCursor1.MovePrevious();
else if (e.KeyCode == Keys.Right)
xyCursor1.MoveNext();
}

Add some code to plot data on the graph, run, and then you can use the left and right arrow keys to
move the cursor left and right through the data.

This solution will only work for one cursor at a time, though. It may be a little trickier in your application since you have two cursors. Did you want this to work for one cursor or both cursors? If both, how do you want to specify which cursor should move when you press the arrow keys?

- Elton
0 Kudos
Message 2 of 9
(6,542 Views)
Thanks for answering my question. Yes I need to move both cursors but not at the same time. If I know which cursor has been selected then I can move that specific cursor. Am I right?
0 Kudos
Message 3 of 9
(6,542 Views)
Yes, but how will you know which cursor has been selected? There's no built-in concept of a selected cursor in the graph, but there are easy ways to work around this. I was just curious how you were going to handle it in your application.

- Elton
0 Kudos
Message 4 of 9
(6,542 Views)
One way that I thought and it hasn't worked yet is as follow:
To check the cursor1 position and see if it is the same as what ever user has been clicked.

if (Control.MousePosition.X == xyCursor1.XPosition &&
Control.MousePosition.Y == xyCursor1.YPosition)
{
if (e.KeyCode == Keys.Left)
xyCursor1.MovePrevious();
else if (e.KeyCode == Keys.Right)
xyCursor1.MoveNext();
}
else
{
if (Control.MousePosition.X == xyCursor2.XPosition &&
Control.MousePosition.Y == xyCursor2.YPosition)
{
if (e.KeyCode == Keys.Left)
xyCursor2.MovePrevious();
else i
f (e.KeyCode == Keys.Right)
xyCursor2.MoveNext();

}
}

It seems the unit of cursor position is double and it is different with mouse click position. I may need to know the mouse click position in client screen. I like to know other ideas. Measurement studio some how knows user has clicked on cursor1 or cursor2.
0 Kudos
Message 5 of 9
(6,542 Views)
The cursor's XPosition and YPosition properties are in data coordinates and they are associated with plot data of the cursor's Plot property. If you want to know if the mouse is over a cursor, use the graph's GetCursorAt method. GetCursorAt takes x and y parameters in client coordinates and if it returns a non-null reference, there's a cursor at the specified coordinate.

- Elton
0 Kudos
Message 6 of 9
(6,542 Views)
Ok. But how GetCursorAt method helps me to know which cursor my mouse is on? That is why I tried to compare the position of mouse and cursor1 or cursor2.
0 Kudos
Message 7 of 9
(6,542 Views)
Why do you need to know which cursor the mouse is on? GetCursorAt returns a cursor reference if there is a cursor at the specified coordinates. If you get a non-null reference from GetCursorAt, you can then use it to call MoveNext or MovePrevious. If you need to know if it's cursor1 or cursor2, you can compare the cursor that GetCursorAt returns to cursor1 or cursor2. Either way, GetCursorAt should be useful with what you're trying to do.

- Elton
0 Kudos
Message 8 of 9
(6,542 Views)
Thank you. I got it. I needed to play with two "click" and "keyDown" event of graph to get the way I want to work. Thanks.
0 Kudos
Message 9 of 9
(6,542 Views)