diff --git a/Core/Handling/KeyboardHandlerBase.cs b/Core/Handling/KeyboardHandlerBase.cs new file mode 100644 index 00000000..e3803791 --- /dev/null +++ b/Core/Handling/KeyboardHandlerBase.cs @@ -0,0 +1,22 @@ +using System.Windows.Forms; +using CefSharp; + +namespace TweetDuck.Core.Handling{ + class KeyboardHandlerBase : IKeyboardHandler{ + protected virtual bool HandleRawKey(IWebBrowser browserControl, IBrowser browser, Keys key, CefEventFlags modifiers){ + return false; + } + + bool IKeyboardHandler.OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut){ + if (type == KeyType.RawKeyDown && !browser.FocusedFrame.Url.StartsWith("chrome-devtools://")){ + return HandleRawKey(browserControl, browser, (Keys)windowsKeyCode, modifiers); + } + + return false; + } + + bool IKeyboardHandler.OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey){ + return false; + } + } +} diff --git a/Core/Handling/KeyboardHandlerBrowser.cs b/Core/Handling/KeyboardHandlerBrowser.cs index 338ced49..65f84472 100644 --- a/Core/Handling/KeyboardHandlerBrowser.cs +++ b/Core/Handling/KeyboardHandlerBrowser.cs @@ -2,19 +2,19 @@ using CefSharp; namespace TweetDuck.Core.Handling{ - sealed class KeyboardHandlerBrowser : IKeyboardHandler{ + sealed class KeyboardHandlerBrowser : KeyboardHandlerBase{ private readonly FormBrowser form; public KeyboardHandlerBrowser(FormBrowser form){ this.form = form; } - bool IKeyboardHandler.OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut){ - return type == KeyType.RawKeyDown && form.ProcessBrowserKey((Keys)windowsKeyCode); - } + protected override bool HandleRawKey(IWebBrowser browserControl, IBrowser browser, Keys key, CefEventFlags modifiers){ + if (base.HandleRawKey(browserControl, browser, key, modifiers)){ + return true; + } - bool IKeyboardHandler.OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey){ - return false; + return form.ProcessBrowserKey(key); } } } diff --git a/Core/Handling/KeyboardHandlerNotification.cs b/Core/Handling/KeyboardHandlerNotification.cs index c1be9f07..06c08fbf 100644 --- a/Core/Handling/KeyboardHandlerNotification.cs +++ b/Core/Handling/KeyboardHandlerNotification.cs @@ -4,7 +4,7 @@ using TweetDuck.Core.Notification; namespace TweetDuck.Core.Handling { - sealed class KeyboardHandlerNotification : IKeyboardHandler{ + sealed class KeyboardHandlerNotification : KeyboardHandlerBase{ private readonly FormNotificationBase notification; public KeyboardHandlerNotification(FormNotificationBase notification){ @@ -15,31 +15,30 @@ private void TriggerKeyboardShortcutAnalytics(){ notification.InvokeAsyncSafe(notification.AnalyticsFile.NotificationKeyboardShortcuts.Trigger); } - bool IKeyboardHandler.OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut){ - if (type == KeyType.RawKeyDown && !browser.FocusedFrame.Url.StartsWith("chrome-devtools://")){ - switch((Keys)windowsKeyCode){ - case Keys.Enter: - notification.InvokeAsyncSafe(notification.FinishCurrentNotification); - TriggerKeyboardShortcutAnalytics(); - return true; - - case Keys.Escape: - notification.InvokeAsyncSafe(notification.HideNotification); - TriggerKeyboardShortcutAnalytics(); - return true; - - case Keys.Space: - notification.InvokeAsyncSafe(() => notification.FreezeTimer = !notification.FreezeTimer); - TriggerKeyboardShortcutAnalytics(); - return true; - } + protected override bool HandleRawKey(IWebBrowser browserControl, IBrowser browser, Keys key, CefEventFlags modifiers){ + if (base.HandleRawKey(browserControl, browser, key, modifiers)){ + return true; } - return false; - } + switch(key){ + case Keys.Enter: + notification.InvokeAsyncSafe(notification.FinishCurrentNotification); + TriggerKeyboardShortcutAnalytics(); + return true; - bool IKeyboardHandler.OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey){ - return false; + case Keys.Escape: + notification.InvokeAsyncSafe(notification.HideNotification); + TriggerKeyboardShortcutAnalytics(); + return true; + + case Keys.Space: + notification.InvokeAsyncSafe(() => notification.FreezeTimer = !notification.FreezeTimer); + TriggerKeyboardShortcutAnalytics(); + return true; + + default: + return false; + } } } } diff --git a/Core/Other/FormGuide.cs b/Core/Other/FormGuide.cs index 67aa2051..b2e6e5c6 100644 --- a/Core/Other/FormGuide.cs +++ b/Core/Other/FormGuide.cs @@ -70,6 +70,7 @@ private FormGuide(string url, FormBrowser owner){ this.browser = new ChromiumWebBrowser(url){ MenuHandler = new ContextMenuGuide(owner), JsDialogHandler = new JavaScriptDialogHandler(), + KeyboardHandler = new KeyboardHandlerBase(), LifeSpanHandler = new LifeSpanHandler(), RequestHandler = new RequestHandlerBase(true), ResourceHandlerFactory = resourceHandlerFactory diff --git a/TweetDuck.csproj b/TweetDuck.csproj index b77ce0d0..f7822d6a 100644 --- a/TweetDuck.csproj +++ b/TweetDuck.csproj @@ -92,6 +92,7 @@ <DependentUpon>FormBrowser.cs</DependentUpon> </Compile> <Compile Include="Core\Handling\General\FileDialogHandler.cs" /> + <Compile Include="Core\Handling\KeyboardHandlerBase.cs" /> <Compile Include="Core\Handling\KeyboardHandlerBrowser.cs" /> <Compile Include="Core\Handling\KeyboardHandlerNotification.cs" /> <Compile Include="Core\Handling\RequestHandlerBase.cs" />