views:

455

answers:

2

Hello,

I'm developing a XAML system which at present involves an icon in the taskbar which the user has to hover over in order to open the main window. However I need to take accessibility into consideration due to legal issues and also making the system appeal to as broad a range of users as possible. In order to make the system more accessible I am in the process of trying to create a windows shortcut which will allow the main program window to be opened from anywhere else in windows.

The code I've been working on, has 2 faults, the first is that the event handler only fires if the application itself is the focus, where as the shortcuts I require must be able to be opended while other windows are in focus etc. The second is that in the event handler it doesn't detect Alt & Q being pressed at the same time. The code I've created can be found below, I'm just looking for a way to handle the shortcut Alt & Q from anywhere in the windows enviroment and detect them buttons have been pressed, so the window can open.

Any help or advice would be appreciated.

this.KeyDown += new System.Windows.Input.KeyEventHandler(this.Window_Keydown);

private void Window_Keydown(object sender, KeyEventArgs e)
    {
        if ((e.KeyboardDevice.Modifiers == ModifierKeys.Alt) && (e.Key == Key.Q)){}
    }
+1  A: 

Perhaps you should take a look at RoutedCommand objects, and the InputGestures property

Here is the MSDN of RoutedCommands and how to set it up:

http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.aspx

Basicly you have a command: ProcessDataCommand

public class MyCommands
{ 
    public static readonly RoutedCommand ProcessDataCommand = new RoutedCommand();
}

Set it to your menuitem or button as follows:

<Button Command="{x:Static NameSpace:MyCommands.ProcessDataCommand}" />

And add a CommandBinding to the control you wish to handle your command: http://msdn.microsoft.com/en-us/library/system.windows.input.commandbinding.aspx

<Window x:Class="WCSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CloseCommand"
    Name="RootWindow"
    >
  <Window.CommandBindings>
    <CommandBinding Command="{x:Static NameSpace:MyCommands.ProcessDataCommand}"
                    Executed="CloseCommandHandler"
                    CanExecute="CanExecuteHandler"
                    />
  </Window.CommandBindings>
  <StackPanel Name="MainStackPanel">
    <Button Command="{x:Static NameSpace:MyCommands.ProcessDataCommand}" 
            Content="Close File" />
  </StackPanel>
</Window>

Now for the shortkey magic ;)

Where you defined your command, add the following static constructor:

static MyCommands()
{
    ProcessDataCommand.InputGestures.Add(new KeyGesture(Key.Q, ModifierKeys.Alt));
}

And your command will respond to the shortkey as well!

Download a routed command sample here: http://www.codeplex.com/wpfroutedcommand

Hope this helps

Arcturus
A bit but im not doing anything within the application itself, just attempting to open the main window while the application is minimized.
manemawanna
Ahh you might want to take a look at a Keyboard hook invoked with P/Invoke! Someone wrote a simple keylogger in C# using this technique, perhaps you can extract the things you need: http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx .
Arcturus
Ah I see you already found that article.. You dont need Windows Form to make it work btw ;) if you can recognize the ints the keys throw, you can do without the forms dll. P/Invoke is located in the System.Runtime namespace
Arcturus
+2  A: 

Two comments:

Firstly, I would get rid of the "hover" requirement. If you use a standard method of adding your app to the taskbar, such as using System.Windows.Forms.NotifyIcon, you should have your application Window open when it is double clicked. Optionally, you could also add a right click context menu. This way a user can use winkey+B to go to the system tray, and left/right to find your icon, enter to double click or applicationkey to right click.

If, for some reason, this doesn't work in your particular scenario. I would recommend checking if your application is running in the Application_Start event (this only works if you want a single instance of your application at any time). So, if a user runs the application, and there's already an instance of the application open, just restore the window state and bring it to the foreground.

Then, during the installation you can set up a standard Windows shortcut on the desktop or start menu, and set a hotkey in the shortcut properties (this can all be done in code). Then, when the user types the shortcut key, the shortcut will launch the app, Application_Start will execute, it'll see there's a version already running (in the task bar) and bring it to the foreground. Thus achieving the same result in a more standard way.

I hope I correctly understood what you wish to achieve.

Saqib
Thank you very much, I didn't know that winkey + B did that and it achieves what I wanted really without the shortcut key, although I have also got that working.
manemawanna