Question

I was planning to add support for the Back and Forward buttons, found on many keyboards, to my WPF app, but I'm struggling to get them to work.

I've tried using a standard KeyBinding to BrowserBack and BrowserForward, no joy. I tested the code with the ESC key to make sure it was working in principal, and that key was fine.

Nextup I handled the KeyUp event, but the key that gets sent is "System", which is useless, and if I use KeyInterop.VirtualKeyFromKey I just get 0 returned.

I'm starting to think that PInvoke/trapping real Window Messages are going to be the only option, but I'd rather avoid that if anyone has any bright ideas?

Oh, and the keys themselves definately work, and my keyboard is plugged in ;-)

Update: They suggest to use SystemKey got me to a point that I can get it working with:

new KeyBinding(TestCommand, new KeyGesture(Key.Left, ModifierKeys.Alt));

And that seems to work for the keyboard button, but it doesn't work for the corresponding touch "flick" (which simulates next and back). Those flicks work fine in the browsers, but according to my KeyUp event all they're sending is "LeftAlt" and not much else!

** Update Again ** : Rich's comment got me to this:

this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseBack, BrowseBack_Executed));
this.CommandBindings.Add(new CommandBinding(NavigationCommands.BrowseForward, BrowseForward_Executed));

Which seems to work a treat.. flicks too!

Was it helpful?

Solution

The buttons you refer to are handled as MediaCommands, NavigationCommands, ApplicationCommands, EditingCommands or ComponentCommands in WPF - you'll need to add a CommandBinding for each of the buttons you want to intercept, for example:-

<Window.CommandBindings>
<CommandBinding Command="MediaCommands.PreviousTrack" 
                Executed="PreviousTrackCommandBinding_Executed"/>
<CommandBinding Command="MediaCommands.NextTrack"             
                Executed="NextTrackCommandBinding_Executed"/>

And add relevant events in code behind:-

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

I would say in your case it's probably NavigationCommands.BrowseForward and NavigationCommands.BrowseBack. Check out... http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands.aspx and http://msdn.microsoft.com/en-us/library/system.windows.input.navigationcommands_members.aspx

Check out my blog post for more info and more code samples.

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

OTHER TIPS

In PreviewKeyUp event, you should be able to do this -

private void Window_PreviewKeyUp(object sender, KeyEventArgs e){
  if (e.SystemKey == Key.BrowserBack)
    // Do something ...

I hate to say it but this worked fine for me:

<RichTextBox>   
       <RichTextBox.InputBindings>
           <KeyBinding Key="BrowserForward" Command="Paste"/>
       </RichTextBox.InputBindings>
       <FlowDocument>
            <Paragraph>
                Some text here to cut and paste...
                            </Paragraph>
            </FlowDocument>
        </RichTextBox>

When I press the Forward key on my keyboard it does a paste.

Is it possible something else is intercepting the key press?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top