在拖放过程中处理 KeyDown

发布于 2024-10-06 17:00:57 字数 218 浏览 4 评论 0原文

当我的控件上正在进行拖放操作时(即在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-10-13 17:00:58

QueryContinueDrag 事件在拖动源上引发。检查您感兴趣的按键状态将需要 pinvoke,该事件仅旨在帮助识别 Escape 键和修饰键状态更改。需要记住的是,这些键的任何特殊操作非常是无法被发现的。

    [DllImport("user32.dll")]
    private static extern short GetKeyState(Keys key);

它返回一个值<当按键按下时为 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.

    [DllImport("user32.dll")]
    private static extern short GetKeyState(Keys key);

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.

叹倦 2024-10-13 17:00:58

您还可以尝试:
Keyboard.IsKeyDown(); 方法来检查是否按下了特定的键,即:

bool isKeyPressed = Keyboard.IsKeyDown(Key.LeftAlt);

它与前面的答案类似,但它是本机.NET方法,因此不需要您导入任何功能。

这里提出了类似的问题:在拖放过程中处理 KeyDown。或者 keydown 事件不起作用,但有人建议让它像事件一样工作。

更新

第一个解决方案似乎仅适用于 WPF。不过,如果您想检查修饰键的状态,有一种利用属性 Form.ModifierKeys 的方法应该可以在 WinForms 中正常工作。该示例演示如何检查是否同时按下了alt(左alt)和ctrl键:

if (Form.ModifierKeys == (Keys.Alt | Keys.Control))
{
    //TODO: insert your code here
}

You can also try:
Keyboard.IsKeyDown(); method to check if a specific key is pressed, i.e.:

bool isKeyPressed = Keyboard.IsKeyDown(Key.LeftAlt);

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:

if (Form.ModifierKeys == (Keys.Alt | Keys.Control))
{
    //TODO: insert your code here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文