|
| 1 | +# AreBrowserAcceleratorKeysEnabled |
| 2 | + |
| 3 | +## Background |
| 4 | +In general, CoreWebView2 tries to behave as much like the browser as possible. |
| 5 | +This includes allowing accelerator key access to features such as printing and |
| 6 | +navigation. However, in many apps these features are unnecessary or even |
| 7 | +intrusive. These accelerator keys can be disabled by handling the |
| 8 | +`AcceleratorKeyPressed` event on `ICoreWebView2Controller`, but then the burden |
| 9 | +is on the developer to correctly block the keys that trigger browser commands, |
| 10 | +such as Ctrl-P, and not basic editing and movement keys such as Ctrl-C and the |
| 11 | +arrow keys. After that, if the underlying browser ever changes its set of |
| 12 | +accelerator keys, apps may not be able to respond to those changes. |
| 13 | + |
| 14 | +This illustrates the need for a single setting to disable all keyboard shortcuts |
| 15 | +that correspond to browser-specific functionality, without disabling things like |
| 16 | +copy and paste and movement. |
| 17 | + |
| 18 | +In this document we describe the new setting. We'd appreciate your feedback. |
| 19 | + |
| 20 | + |
| 21 | +## Description |
| 22 | +When this setting is set to false, it disables all accelerator keys that access |
| 23 | +features specific to the browser, including but not limited to: |
| 24 | +- Ctrl-F and F3 for Find on Page |
| 25 | +- Ctrl-P for Print |
| 26 | +- Ctrl-R and F5 for Reload |
| 27 | +- Ctrl-Plus and Ctrl-Minus for zooming |
| 28 | +- Ctrl-Shift-C and F12 for DevTools |
| 29 | +- Special keys for browser functions, such as Back, Forward, and Search |
| 30 | + |
| 31 | +It does not disable accelerator keys related to movement and text editing, such |
| 32 | +as: |
| 33 | +- Home, End, Page Up, and Page Down |
| 34 | +- Ctrl-X, Ctrl-C, Ctrl-V |
| 35 | +- Ctrl-A for Select All |
| 36 | +- Ctrl-Z for Undo |
| 37 | + |
| 38 | +This setting has no effect on the `AcceleratorKeyPressed` event. The event will |
| 39 | +be fired for all accelerator keys, whether they are enabled or not. |
| 40 | + |
| 41 | +Those accelerator keys will always be enabled unless they are handled in the |
| 42 | +`AcceleratorKeyPressed` event. |
| 43 | + |
| 44 | +The default value for AreBrowserAcceleratorKeysEnabled is true. |
| 45 | + |
| 46 | +## Examples |
| 47 | +```c# |
| 48 | +privagte WebView2 _webView; |
| 49 | +void ToggleBrowserKeysEnabled() |
| 50 | +{ |
| 51 | + var settings = _webView.CoreWebView2.Settings; |
| 52 | + settings.AreBrowserAcceleratorKeysEnabled = !settings.AreBrowserAcceleratorKeysEnabled; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +```cpp |
| 57 | +void SettingsComponent::ToggleBrowserKeysEnabled() |
| 58 | +{ |
| 59 | + BOOL enabled; |
| 60 | + CHECK_FAILURE(_coreWebView2Settings->get_AreBrowserAcceleratorKeysEnabled(&enabled)); |
| 61 | + CHECK_FAILURE(_coreWebView2Settings->put_AreBrowserAcceleratorKeysEnabled(enabled ? FALSE : TRUE)); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +## Remarks |
| 67 | +Some accelerator keys that don't make sense for WebViews are always disabled |
| 68 | +regardless of this setting. This includes things like opening and closing tabs, |
| 69 | +viewing bookmarks and history, and selecting the location bar. |
| 70 | + |
| 71 | +This setting will not prevent commands from being run through the context menu. |
| 72 | +To disable the context menu, use `AreDefaultContextMenusEnabled`. |
| 73 | + |
| 74 | + |
| 75 | +## API Details |
| 76 | +``` |
| 77 | +[uuid(9aab8652-d89f-408d-8b2c-1ade3ab51d6d), object, pointer_default(unique)] |
| 78 | +interface ICoreWebView2Settings2 : ICoreWebView2Settings { |
| 79 | + /// When this setting is set to FALSE, it disables all accelerator keys |
| 80 | + /// that access features specific to the browser, including but not limited to: |
| 81 | + /// - Ctrl-F and F3 for Find on Page |
| 82 | + /// - Ctrl-P for Print |
| 83 | + /// - Ctrl-R and F5 for Reload |
| 84 | + /// - Ctrl-Plus and Ctrl-Minus for zooming |
| 85 | + /// - Ctrl-Shift-C and F12 for DevTools |
| 86 | + /// - Special keys for browser functions, such as Back, Forward, and Search |
| 87 | + /// |
| 88 | + /// It does not disable accelerator keys related to movement and text editing, |
| 89 | + /// such as: |
| 90 | + /// - Home, End, Page Up, and Page Down |
| 91 | + /// - Ctrl-X, Ctrl-C, Ctrl-V |
| 92 | + /// - Ctrl-A for Select All |
| 93 | + /// - Ctrl-Z for Undo |
| 94 | + /// |
| 95 | + /// |
| 96 | + /// This setting has no effect on the `AcceleratorKeyPressed` event. The event |
| 97 | + /// will be fired for all accelerator keys, whether they are enabled or not. |
| 98 | + /// |
| 99 | + /// Those accelerator keys will always be enabled unless they are handled in the |
| 100 | + /// `AcceleratorKeyPressed` event. |
| 101 | + /// |
| 102 | + /// The default value for AreBrowserAcceleratorKeysEnabled is TRUE. |
| 103 | + [propget] HRESULT AreBrowserAcceleratorKeysEnabled( |
| 104 | + [out, retval] BOOL* areBrowserAcceleratorKeysEnabled); |
| 105 | +
|
| 106 | + /// Sets the `AreBrowserAcceleratorKeysEnabled` property. |
| 107 | + [propput] HRESULT AreBrowserAcceleratorKeysEnabled( |
| 108 | + [in] BOOL areBrowserAcceleratorKeysEnabled); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +```c# (but actually midl3) |
| 113 | +runtimeclass CoreWebView2Settings { |
| 114 | + // ... |
| 115 | + |
| 116 | + Boolean AreBrowserAcceleratorKeysEnabled { get; set; }; |
| 117 | +} |
| 118 | +``` |
0 commit comments