在一个拖放处理的KeyDown。或者keydown事件不workign [英] Handle KeyDown during a drag drop. Or keydown event not workign

查看:162
本文介绍了在一个拖放处理的KeyDown。或者keydown事件不workign的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的处理事件的KeyDown,而用户拖放工作正常。

I need to handle event "KeyDown" while user drag and drop is working.

使用示例。 当用户点击Alt键光标下一些图像将被改变,用户可以删除附加项数据。

Example of use. When user click "alt" some image under cursor will be changed and user can drop additional item data.

不过的KeyDown不工作!只有ESC键!

But KeyDown not working at all! Only esc button!!

请帮忙。我真的需要来确定的事件。

Please help. I really need to determine event.

P.S。我想这code:

P.s. I tried this code:

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

但我只能上移动鼠标确定点击的按钮。但我真的需要关键的关闭事件,以确定ALT被点击了。

But i can determine clicked button only on mouse move. But i really need key down event to determine that alt was clicked.

推荐答案

您可以尝试使用:

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

和付诸apropriate事件适合您的需求,喜欢的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毫秒),它应该出现立即作出反应。您可以使用一个额外的标志来处理每个按键preSS和释放(不是每次定时器触发的事件)一次。

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)事件,但它可以很容易地修改只要你想监视多达键。

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.

首先,要检测添加两个字段到类窗口的键,键向上的的:

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 窗口的加载事件。

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

然后,你可以实现你刚才添加为加载事件处理的方法和初始化计时器有:

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。或者keydown事件不workign的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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