调试键盘事件,例如“Ctrl”和“向上箭头”
我想调试程序的一部分,该部分旨在响应键盘输入,例如 Ctrl+↑。
因此,我在感兴趣的区域的代码中放置了一个断点。但是,一旦我按下 Ctrl 键,程序就会跳转到该断点。这种情况发生在我按下箭头键之前,因此我发现这种情况很难调试。
那么,如何调试Ctrl+↑等多键输入事件呢?
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+↑?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 Visual Studio 调试代码,则可以通过向断点添加条件来调试这种情况。
为此,请右键单击代码语句左侧的断点图标,然后单击
条件...
适用于您的情况的条件示例如下: :现在您可以调试多键输入事件,例如 Ctrl+↑,而无需更改任何代码。
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:Now you can debug multi-key input events such as Ctrl+↑ without the need to change any of your code.
您可以使用 System.Diagnostics.Debug.WriteLine 将调试信息写入 Listeners 集合中的跟踪侦听器。
示例:
在 Visual Studio 菜单栏中,选择“View”->“Output”以查看输出。
You can use System.Diagnostics.Debug.WriteLine to write debug information to the trace listeners in the Listeners collection.
Example:
In the Visual Studio menu bar, select View->Output to see the output.
您使用什么事件?按键按下?尝试使用 KeyUp。在您释放 CTRL+组合键之前,它不会触发。
What event do you use? KeyDown? Try using KeyUp. It won't fire until you release CTRL+key combination.
将断点放在
if
子句中怎么样?这样,只有满足条件时才会中断。How about just putting the breakpoint inside the
if
clause? That way it only breaks if the condition is fulfilled.查看 Debug.Assert 方法。它将允许您仅根据条件进行调试。执行将继续,直到条件为假。您可以执行类似的操作(伪代码):
Debug.Assert(NOT up key Pressed);
这将使它忽略除向上键之外的任何按键。
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.
一般来说,您唯一的选择是将一些消息打印到控制台或错误日志。否则调试器 UI 会干扰您的代码。例如,Visual Studio 调试器可以在遇到断点时打印表达式的值,因此您无需编写特殊代码。
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.