响应WPF中的"后退"和"前进"按钮

Ste*_*ins 4 keyboard wpf key-bindings back next

我计划在我的WPF应用程序中添加对许多键盘上的后退和前进按钮的支持,但我很难让它们工作.

我尝试过使用标准的KeyBinding来浏览BrowserBack和BrowserForward,没有任何乐趣.我用ESC键测试了代码,以确保它在原理上工作,并且该密钥很好.

Nextup我处理了KeyUp事件,但是发送的密钥是"System",这是没用的,如果我使用KeyInterop.VirtualKeyFromKey,我只返回0.

我开始认为PInvoke /陷阱真正的窗口消息将是唯一的选择,但我宁愿避免,如果有人有任何好主意?

哦,钥匙本身肯定会工作,我的键盘插入;-)

更新:他们建议使用SystemKey让我明白我可以使用它:

new KeyBinding(TestCommand, new KeyGesture(Key.Left, ModifierKeys.Alt));
Run Code Online (Sandbox Code Playgroud)

这似乎适用于键盘按钮,但它不适用于相应的触摸"轻弹"(模拟下一个和后面).这些电影在浏览器中运行良好,但根据我的KeyUp事件,他们发送的所有内容都是"LeftAlt"而不是其他内容!

**再次更新**:Rich的评论让我这样:

this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseBack, BrowseBack_Executed));
this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseForward, BrowseForward_Executed));
Run Code Online (Sandbox Code Playgroud)

这似乎是一种享受...也轻弹!

Ric*_*ton 5

您引用的按钮在WPF中作为MediaCommands,NavigationCommands,ApplicationCommands,EditingCommands或ComponentCommands处理 - 您需要为要拦截的每个按钮添加一个CommandBinding,例如: -

<Window.CommandBindings>
<CommandBinding Command="MediaCommands.PreviousTrack" 
                Executed="PreviousTrackCommandBinding_Executed"/>
<CommandBinding Command="MediaCommands.NextTrack"             
                Executed="NextTrackCommandBinding_Executed"/>
Run Code Online (Sandbox Code Playgroud)

并在代码中添加相关事件: -

private void PreviousTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Previous track");
}
private void NextTrackCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    MessageBox.Show("Next track");
}
Run Code Online (Sandbox Code Playgroud)

我会说在你的情况下它可能是NavigationCommands.BrowseForward和NavigationCommands.BrowseBack.查看... http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands.aspxhttp://msdn.microsoft.com/en-us/library/system.windows .input.navigationcommands_members.aspx

查看我的博客文章,了解更多信息和更多代码示例.

http://richardhopton.blogspot.com/2009/08/responding-to-mediapresentation-buttons.html