Debug keyboard events such as Ctrl + Up Arrow

I want to debug the part of the program designed to respond to keyboard input, for example Ctrl + โ†‘ .

So, I set a breakpoint in the code in the area of โ€‹โ€‹interest. However, as soon as I press the Ctrl key, the program jumps to this breakpoint. This happens before I hit the arrow key, so I find it difficult to debug this situation.

So, how can I debug an input event with multiple keys, e.g. Ctrl + โ†‘ ?

+4
source share
6 answers

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

To do this, right-click the breakpoint icon to the left of the code statement and click Condition... An example of a condition that will apply to your situation:

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

Now you can debug input events with multiple keys, such as Ctrl + โ†‘ without the need to change any code.

+5
source

You can use System.Diagnostics.Debug.WriteLine to write debugging information to 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"); } 

From the Visual Studio menu bar, select View-> Output to see the result.

+4
source

What event are you using? Keydown? Try using KeyUp. It does not fire until you release the CTRL + key combination.

+2
source

How to simply set a breakpoint inside an if clause? Thus, it only breaks if the condition is met.

+2
source

Take a look at the Debug.Assert method. This will only allow you to switch to state based debugging. Execution will continue until the condition becomes false. You can do something like (pseudo-code): Debug.Assert(NOT up key pressed); This will make it ignore any keystrokes, but the keys are up.

+1
source

In general, your only option is to print some messages in the console or error log. Otherwise, the debugger user interface will interfere with your code. The Visual Studio debugger, for example, can print expression values โ€‹โ€‹when it breaks, so you donโ€™t need to write special code.

+1
source

Source: https://habr.com/ru/post/1336048/


All Articles