KeyDown 在拖放过程中被忽略 [英] KeyDown is ignored during the drag and drop

查看:36
本文介绍了KeyDown 在拖放过程中被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理KeyDown"拖放操作期间的事件.

I need to handle "KeyDown" event during the drag and drop action.

目标是在用户按住alt"并拖动对象时显示一些通知或更改光标.

The goal is to show some notification or change cursor when user is holding 'alt' and dragging an object.

但我注意到 KeyDown 不起作用,只有 ESC 按钮以某种方式响应!

But I have noticed that KeyDown is not working, only ESC button is responding somehow!

我已经尝试使用下一个 DLL 导入代码运行它:

I tried already to run it with the next DLL import code:

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

这可以显示按钮是否被按下,但我什至真的在寻找按下!拖动对象时如何处理键盘按下事件的任何提示?

This can show whether button was pressed, but I really searching for the press even! Any tip how to handle keyboard down event when dragging an object?

推荐答案

您可以尝试使用:

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

并将其放入适合您需要的适当事件中,例如 DragEnter、DragOver 或使用计时器并以恒定速率检查键的状态.

And put it into apropriate event that fits your needs, like DragEnter, DragOver or use a timer and check the state of the key at a constant rate.

如果您使用的定时器触发事件​​的频率足够高(例如每 100 毫秒),它应该会立即做出反应.您可以使用额外的标志来处理每次按键按下和释放一次(而不是每次计时器触发其事件时).

If you use a Timer that fires event often enough (like each 100ms) it should appear to react instantly. You can use an additional flag to handle each key press and release once (and NOT each time the timer fires its event).

更新

我已经实现了这个只是为了测试它,即使用户正在拖动某些东西它似乎也能正常工作(我已经测试了拖动文本).

I've implemented this just to test it and it seems to work correctly even if a user is dragging something (I've tested this for dragging a text).

这是一个简单的例子,当你想检测一个特定键(左 ALT)的 key downkey up 事件时,但它可以很容易地修改监控任意数量的键.

It's for a simple case, when you want to detect key down and key up events for one specific key (left ALT), but it can be quite easily modified to monitor as many keys as you want.

首先在你要检测key downkey up的Window的类中添加两个字段:

First, add two fields to the class of the Window you want to detect the key down and key up in:

DispatcherTimer timer = new DispatcherTimer();
bool keyPressed = false;

DispatcherTimer 在这里使用,因此定时器引发的事件在窗口的UI线程的上下文中执行.

DispatcherTimer is used here, so the event raised by timer is executed in the context of window's UI thread.

接下来,将方法 Window_Loaded 添加到窗口的 Loaded 事件.

Next, add a method Window_Loaded to the window's Loaded event.

然后,您可以实现刚刚添加为 Loaded 事件处理程序的方法并在那里初始化计时器:

Then, you can implement the method you've just added as Loaded event handler and initialize the timer there:

 private void Window_Loaded(object sender, RoutedEventArgs e)
 {
     timer.Tick += new EventHandler(timer_Tick);
     //timer will raise an event checking key state each 100ms
     timer.Interval = TimeSpan.FromMilliseconds(100);
     timer.Start();
 }

最后,您应该添加 timer_Tick 方法的实现,该方法正在检查您要监控的键的状态:

Finally, you should add implementation of timer_Tick method that is checking state of the key that you want to monitor:

void timer_Tick(object sender, EventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.LeftAlt))
        {
            //checking flag to handle each key press only once
            if (!keyPressed)
            {
                keyPressed = true;

                //TODO: write your key down handling code here
                //i.e.: sampleRichTextBox.AppendText("Left Alt is down;\t");
            }
        }
        else
        {
            //checking flag to handle each key release only once
            if (keyPressed)
            {
                keyPressed = false;

                //TODO: write your key up handling code here
                //i.e.: sampleRichTextBox.AppendText("Left Alt is up;\t");
            }
        }
    }

如果您需要有关此解决方案的更多信息,请告诉我.

If you need any more information about this solution, let me know.

这篇关于KeyDown 在拖放过程中被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆