views:

455

answers:

3

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!

+1  A: 

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 ...
Edward
That didn't work, but it did let me see what it was actually doing which was sending LeftAlt followed by Left (or Right).
Steven Robbins
Lol... so it is a dodgy keyboard. That short cut combo is back in most windows apps... I am pretty sure its distinct from the Back key though... what keyboard is it?
Schneider
A Microsoft Comfort Keyboard, so I'd hope they'd implement the keys correctly :-)
Steven Robbins
From your description I would say its 100% certain they are not implemented correctly despite it being MS. Have u tried windows update for keyboard 'drivers' (absurd I know but maybe)?
Schneider
+1  A: 

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?

Schneider
Doesn't work for me. I've just pasted that code into a new WPF project and nada. I'm using Win7, I wonder if that is making a difference?
Steven Robbins
I am also on Windows 7... I am wondering if you could have a "non standard" keyboard... but presumably if the back/forward work in your web browser then Windows does recognise the keys correctly.
Schneider
Anyway, now you know that my code snippet works for me, you can be satisfied that any code you write is probably only required on your machine ;)
Schneider
I've tried it on a few machines, same result. I have it partially working with the code in my edit, but it would be nice if the touch flicks worked too. They're supposed to emulate the same command, but it appears not :-)
Steven Robbins
Tried the keyboard or tried the code?
Schneider
+1  A: 

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

irtimaled
Thanks, but this is the back/next buttons that generally control the browser, not the next/previous track. Your code works fine with the next/previous track buttons though :-)
Steven Robbins
It seems NavigationCommands.BrowseBack was the missing piece of the puzzle! thanks.
Steven Robbins