-1

I`ve been trying to figure this out, I`m using WPF to build my application. I want to create a shortcut key for the application (CTRL+ALT+3, for example). I`ve tried everything I found over the internet, but with no success.

I want to use a hot key to minimize the application to the tray icon that I will create it later on.

The problem now I can`t figure out how can I do this using the C# WPF on Visual Studio 2010.

sikas
  • 5,435
  • 28
  • 75
  • 120
  • Possible duplicate of http://stackoverflow.com/questions/48935/c-using-wpf-and-net-3-5-how-can-i-register-a-global-hot-key-to-say-ctrlshift – CodeNaked Apr 08 '11 at 22:15
  • @CodeNaked: The solution in the provided link doesn`t work with me – sikas Apr 08 '11 at 22:23
  • @sikas - Which part doesn't work? Seems like the only thing missing from there would be an appropriate routed command. Or are you also asking how to put an icon in the notification area? – CodeNaked Apr 08 '11 at 22:25
  • @CodeNaked: This is the error I get from the code over there "The name 'MyAppCommands' does not exist in the current context" ... any ideas about it? I can post my code if it helps – sikas Apr 08 '11 at 22:28
  • you asked the same question yesterday. Why didn't you do what was suggested then. Did you try? If so tell us what happened and how you failed. – David Heffernan Apr 08 '11 at 22:30
  • @David Heffernan: none of them worked with the WPF. The below answer works but with only one key, CTRL+{Letter|Digit} or ALT+{Letter|Digit} or SHIFT+{Letter|Digit} ... I want to use CTRL+ALT+{Letter|Digit} – sikas Apr 08 '11 at 22:50
  • What about [RegisterHotkey](http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx)? – Nick Spreitzer Apr 08 '11 at 23:18
  • Exact duplicate of http://stackoverflow.com/questions/5587425/shortcuts-keys-and-tray-icon – Hans Passant Apr 08 '11 at 23:45
  • @Nick: how can I use RegisterHotKey with WPF? I can`t seem to get it work!! and the link u provided is for C++ – sikas Apr 09 '11 at 08:52

2 Answers2

1

I have done this in a WPF application. I used some code-behind for the main window.

The creator is:

    public MainWindow()
    {
        InitializeComponent();

        this.AddHandler(Control.PreviewKeyDownEvent, new RoutedEventHandler(MyPreviewKeyDown), true);
    }

The handler begins with:

    private void MyPreviewKeyDown(object sender, RoutedEventArgs e)
    {
        KeyEventArgs ke = e as KeyEventArgs;
        if (ke.Key == Key.Oem5 && ke.KeyboardDevice.Modifiers == ModifierKeys.Control)
        {

If you need a combination of modifier keys, you can use an expression such as:

(int)ke.KeyboardDevice.Modifiers == (int)ModifierKeys.Control + (int)ModifierKeys.Alt
Guge
  • 4,569
  • 4
  • 35
  • 47
  • How can I set more than one modifier? ex: CTRL+ALT? and what is Oem5 key? – sikas Apr 08 '11 at 22:46
  • I have update my answer with code for combinations of modifiers. Oem5 is pipe (|) on my Norwegian keyboard, to the left of the 1-key and above the TAB. Makes it easy to find for my blind users. Might be different in other keyboards. – Guge Apr 08 '11 at 22:55
  • 1
    Thanks, that solved a small part. The shortcut works when the application is focused. I want to use it when the application is not focused, and I can use RegisterHotKey()!! – sikas Apr 09 '11 at 08:49
  • Oh, I thought you just wanted a shortcut key while inside the application. I have never tried to create a shortcut key that works while in other apps. Now I wonder if I should do that for my application.... – Guge Apr 09 '11 at 09:20
0

Use the HookManager from codeproject, then use it's KeyDown event.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69