Bohn
Bohn

Reputation: 26919

Debugging keyboard events like 'Ctrl+Up Arrow'

I want to debug a part of a program that is intended to respond to keyboard input such as Ctrl+.

So, I put a breakpoint in the code in the area interest. However, once I press the Ctrl key the program jumps to that breakpoint. This happens before I have pressed an arrow key, so I'm finding this situation difficult to debug.

So, how can I debug a multi-key input event such as Ctrl+?

Upvotes: 4

Views: 2183

Answers (6)

Homam
Homam

Reputation: 23841

You can use System.Diagnostics.Debug.WriteLine to write debug information to the trace listeners in the Listeners collection.

Example:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.Up)
        System.Diagnostics.Debug.WriteLine("Up with control have pressed");
}

In the Visual Studio menu bar, select View->Output to see the output.

Upvotes: 4

Martin
Martin

Reputation: 66

If you are using Visual Studio to debug your code, you can debug this situation by adding a condition to your breakpoint.

To do so, right-click the break point icon to the left of your code statement and click Condition... An example of a condition that would apply to your situation is:

e.Control && e.KeyCode == Keys.Up

Now you can debug multi-key input events such as Ctrl+ without the need to change any of your code.

Upvotes: 5

CodesInChaos
CodesInChaos

Reputation: 108790

How about just putting the breakpoint inside the if clause? That way it only breaks if the condition is fulfilled.

Upvotes: 2

Oleg Kolosov
Oleg Kolosov

Reputation: 1588

In general your only option is to print some messages to console or error log. Otherwise debugger UI will interfere with your code. Visual Studio debugger, for example, can print values of expressions on hitting breakpoint, so you don't need to write special code.

Upvotes: 1

Have a look at the Debug.Assert method. It will allow you to only go to debug based on a condition. Execution will continue until the condition is false. You could do something like (pseudo-code): Debug.Assert(NOT up key pressed); This will make it ignore any key presses but the up key.

Upvotes: 1

dzendras
dzendras

Reputation: 4751

What event do you use? KeyDown? Try using KeyUp. It won't fire until you release CTRL+key combination.

Upvotes: 2

Related Questions