silverlight keydown 事件不会因箭头键而触发
我在滚动视图中有一个画布。 我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用 KeyUp 事件即可。 奇迹般有效。
Just use the KeyUp event. Works like a charm.
我发现了这个愚蠢的技巧来让它发挥作用。 将滚动视图设置为不是制表符可以防止它吃掉关键事件。但是后来我在页面上有另一个文本框,它突然总是具有焦点,因为滚动视图不再有焦点。 所以我通过让一个不可见的文本框获得焦点来解决这个问题。
编辑:我还必须将文本框移出画布,因为尽管不可见,但其轮廓仍然显示。
第二次编辑:我使用了文本框,因为这是我发现的第一个可以捕获 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.
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.
这是一个可能的答案 - 我还没有机会测试这一点。 不过,我过去也遇到过类似的麻烦,当控件在您获取事件之前就消耗了它们。 您可以尝试以下一些操作:
除了 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:
Except for 1) all of these count as hacks, really, but good luck, I hope one of them works for you!
“使用 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.