Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to assign ShortCut Keys to open a window in a WPF application(MVVM Pattern).The shortcut key should work from any window in my application. These keys should work in application level and shouldn't be global. Could anyone please help me with this?

What I have tried:

I've tried Importing User32.dll and RegisterHotKey method. But, this assigns key globally. Even If I've minimized my application and press any shortcut key, the assigned window gets open.

[DllImport("user32.dll")]
static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers,int vk);
Posted
Updated 5-Jun-17 0:18am

1 solution

What you need is command key binding.

KeyBinding Class (System.Windows.Input)[^] (scroll down for example)
 
Share this answer
 
Comments
Manoj_Baddi 5-Jun-17 8:31am    
KeyBinding works only when my focus is on that window. I need the shortcut to work no matter where my focus is in the whole application.
jimmson 5-Jun-17 8:37am    
Well, you can just add key binding to the each window. It can point to the same command, you don't need to duplicate it.
Manoj_Baddi 7-Jun-17 2:57am    
Thank you. But, I've more than 40 windows in my application. Isn't there any other way to achieve this?
jimmson 7-Jun-17 3:12am    
Yes, it is. Do these windows have some common parent? You can add command binding to the parent (in code, not xaml).
Manoj_Baddi 8-Jun-17 2:30am    
Ya, there is a window which is the owner of rest of the windows.Could you please help me with an example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900