silverlight keydown 事件不会因箭头键而触发

发布于 2024-07-07 20:19:48 字数 201 浏览 8 评论 0原文

我在滚动视图中有一个画布。 我将 keydown 事件处理程序附加到滚动视图。 对于大多数键,都会调用处理程序。

但是,对于箭头键,不会调用处理程序。 相反,滚动视图会向适当的方向滚动。

我还将一个 keyup 处理程序附加到滚动视图,并且确实为箭头键调用了 keyup。

有什么办法可以在这里获取箭头键按下事件吗?

I have a canvas inside a scrollview. I attached a keydown event handler to the scrollview. For most keys, the handler gets called.

However, for the arrow keys, the handler does not get called. Instead, the scrollview gets scrolled in the appropriate direction.

I also attached a keyup handler to the scrollview and the keyup does get called for the arrow keys.

Is there any way to get the arrow key down event here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

嘴硬脾气大 2024-07-14 20:19:48

只需使用 KeyUp 事件即可。 奇迹般有效。

Just use the KeyUp event. Works like a charm.

苄①跕圉湢 2024-07-14 20:19:48

我发现了这个愚蠢的技巧来让它发挥作用。 将滚动视图设置为不是制表符可以防止它吃掉关键事件。但是后来我在页面上有另一个文本框,它突然总是具有焦点,因为滚动视图不再有焦点。 所以我通过让一个不可见的文本框获得焦点来解决这个问题。

scrollView.IsTabStop = false;

invisibleTextBox.Foreground = new SolidColorBrush(Colors.Transparent);
invisibleTextBox.Background = new SolidColorBrush(Colors.Transparent);
Canvas.SetZIndex(invisibleTextBox, -1000);
invisibleTextBox.KeyDown += new KeyEventHandler(HandleKeyDown);
invisibleTextBox.KeyUp += new KeyEventHandler(HandleKeyUp);

编辑:我还必须将文本框移出画布,因为尽管不可见,但其轮廓仍然显示。

第二次编辑:我使用了文本框,因为这是我发现的第一个可以捕获 KeyDown 事件的东西。 但是,UserControl 可以。 因此,使用 UserControl 而不是不可见的文本框可能是更好的做法。 如果需要,您可以在 UserControl 上调用 Focus()。

I found this silly hack to make it work. Setting the scrollview to not be a tabstop keeps it from eating the key events.. but then I had another textbox on the page that all of a sudden ALWAYS had focus because the scrollview didn't anymore. So I fixed that by letting an invisible textbox get focus.

scrollView.IsTabStop = false;

invisibleTextBox.Foreground = new SolidColorBrush(Colors.Transparent);
invisibleTextBox.Background = new SolidColorBrush(Colors.Transparent);
Canvas.SetZIndex(invisibleTextBox, -1000);
invisibleTextBox.KeyDown += new KeyEventHandler(HandleKeyDown);
invisibleTextBox.KeyUp += new KeyEventHandler(HandleKeyUp);

Edit: I also had to move the text box off the canvas because despite being invisible, its outline still showed up.

2nd Edit: I used a textbox because that was the first thing I found that could capture KeyDown events. However, a UserControl can. So it would probably be better practice to use a UserControl instead of an invisible text box. You can call Focus() on the UserControl if needed.

舟遥客 2024-07-14 20:19:48

这是一个可能的答案 - 我还没有机会测试这一点。 不过,我过去也遇到过类似的麻烦,当控件在您获取事件之前就消耗了它们。 您可以尝试以下一些操作:

  1. 使用 PreviewKeyDown 事件,我想这就是它的名字。 它可以让您在事件被控件消耗之前获取事件。
  2. 尝试 mblandfo 的建议,尽管如果您这样做,您可能会将整个事情包装在用户控件中,以对代码的其余部分隐藏您正在做的事情。
  3. 向 Canvas 对象添加一个键处理程序,您可以捕获那里的事件,并通过您自己的事件“冒泡”它。

除了 1),所有这些都算作黑客,真的,但祝你好运,我希望其中之一适合你!

This is a possible answer - I haven't had a chance to test this. I've had similar trouble in the past though, when a control is consuming the events before you can get at them. There's a few things you may be able to try:

  1. Use the PreviewKeyDown event, I think that's what it's called. It may let you get at the event before it's consumed by the control.
  2. Try mblandfo's suggestion, although if you do this you probably ant to wrap the whole thing up in a user control to hide what you're doing from the rest of your code.
  3. Add a key handler to the Canvas object, you may be able to catch the event there, and "bubble" it up through your own event.

Except for 1) all of these count as hacks, really, but good luck, I hope one of them works for you!

北风几吹夏 2024-07-14 20:19:48

“使用 PreviewKeyDown 事件,我认为这就是它的名字。它可以让您在该事件被控件消耗之前获取该事件。”

这有效。 只需设置事件参数“Handled = true”,ScrollViewer(或 ListBox)在处理完事件后就不会捕获该事件。 不过,我不需要对 ListBox 使用 IsTabStop 属性; 无论如何,它似乎没有做任何事情。

"Use the PreviewKeyDown event, I think that's what it's called. It may let you get at the event before it's consumed by the control."

This works. Just set the event arguments "Handled = true" and the ScrollViewer (or ListBox) wont grab onto the event after you've already handled it. I didn't need to use the IsTabStop property for the ListBox though; that and it didn't seem to do anything anyways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文