在拖放过程中处理 KeyDown
当我的控件上正在进行拖放操作时(即在 DragEnter 和 DragLeave 之间),我需要响应按键事件(O、C、G 键等,而不是修饰键)。但是,此阶段不会调用 KeyDown 事件。
我尝试选择我的控件并专门将焦点设置在 DragEnter 上,但这不起作用。
编辑:
汉斯的答案基本上是正确的,除了我必须使用 GetAsynchKeyState 来获得我想要的行为。
I need to respond to keydown events (O, C, G keys etc., not modifier keys) while a Drag+Drop operation is in progress over my control (i.e. between DragEnter and DragLeave). However the KeyDown event is not called at this stage.
I've tried selecting my control and specifically setting focus on DragEnter, but that doesn't work.
EDIT:
Hans' answer is basically correct, except I had to use GetAsynchKeyState to get the behaviour I wanted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QueryContinueDrag 事件在拖动源上引发。检查您感兴趣的按键状态将需要 pinvoke,该事件仅旨在帮助识别 Escape 键和修饰键状态更改。需要记住的是,这些键的任何特殊操作非常是无法被发现的。
它返回一个值<当按键按下时为 0。我不能说它一定能正常工作,但当我尝试时它看起来不错。
The QueryContinueDrag event is raised on the drag source. Checking for the state of the keys you are interested in is going to require pinvoke, the event is only designed to help recognize the Escape key and modifier key state changes. Which is something to keep in mind, that these keys have any special action is very undiscoverable.
It returns a value < 0 when the key is down. I can't say it's guaranteed to work correctly but it looked good when I tried it.
您还可以尝试:
Keyboard.IsKeyDown();
方法来检查是否按下了特定的键,即:它与前面的答案类似,但它是本机.NET方法,因此不需要您导入任何功能。
这里提出了类似的问题:在拖放过程中处理 KeyDown。或者 keydown 事件不起作用,但有人建议让它像事件一样工作。
更新
第一个解决方案似乎仅适用于 WPF。不过,如果您想检查修饰键的状态,有一种利用属性
Form.ModifierKeys
的方法应该可以在 WinForms 中正常工作。该示例演示如何检查是否同时按下了alt(左alt)和ctrl键:You can also try:
Keyboard.IsKeyDown();
method to check if a specific key is pressed, i.e.:It's similar to the previous answer, but it's a native .NET method, so it doesn't require you to import any functions.
A similar question has been asked here: Handle KeyDown during a drag drop. Or keydown event not workign, but there was a suggestion to make it work like an event.
UPDATE
The first solution seems to work only in WPF. If you want to check states of modifier keys, there is, however, a method utilizing a property
Form.ModifierKeys
that should work correctly in WinForms. The example shows how to check if alt (left alt) and ctrl keys are both pressed: