How can I set evenHandler in WPF for all windows (whole application)?
5 answers
Register a global event handler in your application class (App.cs), for example:
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new RoutedEventHandler(Window_KeyDown));
}
void Window_KeyDown(object sender, RoutedEventArgs e)
{
// your code here
}
}
This will handle the event KeyDown
for anyone Window
in your application. You can dump e
on KeyEventArgs
to get information about the pressed key.
+9
source to share
How about this:
public partial class App : Application {
protected override void OnStartup(StartupEventArgs e) {
EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new RoutedEventHandler(KeyDown));
base.OnStartup(e);
}
void KeyDown(object sender, RoutedEventArgs e) {
}
}
+3
source to share