-
Notifications
You must be signed in to change notification settings - Fork 493
/
Copy pathScenarioAcceleratorKeyPressed.cpp
145 lines (132 loc) · 6.3 KB
/
ScenarioAcceleratorKeyPressed.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) Microsoft Corporation. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "stdafx.h"
#include "ScenarioAcceleratorKeyPressed.h"
#include "AppWindow.h"
#include "CheckFailure.h"
using namespace Microsoft::WRL;
static constexpr WCHAR c_samplePath[] = L"ScenarioAcceleratorKeyPressed.html";
ScenarioAcceleratorKeyPressed::ScenarioAcceleratorKeyPressed(AppWindow* appWindow)
: m_appWindow(appWindow), m_controller(appWindow->GetWebViewController()),
m_webView(appWindow->GetWebView())
{
m_sampleUri = m_appWindow->GetLocalUri(c_samplePath);
wil::com_ptr<ICoreWebView2Settings> settings;
CHECK_FAILURE(m_webView->get_Settings(&settings));
m_settings3 = settings.try_query<ICoreWebView2Settings3>();
// Setup the web message received event handler before navigating to
// ensure we don't miss any messages.
CHECK_FAILURE(m_webView->add_WebMessageReceived(
Microsoft::WRL::Callback<ICoreWebView2WebMessageReceivedEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
{
wil::unique_cotaskmem_string uri;
CHECK_FAILURE(args->get_Source(&uri));
// Always validate that the origin of the message is what you expect.
if (uri.get() != m_sampleUri)
{
return S_OK;
}
wil::unique_cotaskmem_string messageRaw;
CHECK_FAILURE(args->TryGetWebMessageAsString(&messageRaw));
std::wstring message = messageRaw.get();
std::wstring reply;
if (message.compare(0, 26, L"DisableBrowserAccelerators") == 0)
{
CHECK_FAILURE(m_settings3->put_AreBrowserAcceleratorKeysEnabled(FALSE));
MessageBox(
nullptr,
L"Browser-specific accelerator keys, for example: \n"
L"Ctrl+F and F3 for Find on Page\n"
L"Ctrl+P for Print\n"
L"Ctrl+R and F5 for Reload\n"
L"Ctrl+Plus and Ctrl+Minus for zooming\n"
L"Ctrl+Shift-C and F12 for DevTools\n"
L"Special keys for browser functions, such as Back, Forward, and "
L"Search \n"
L"will be disabled after the next navigation except for F7.",
L"Settings change", MB_OK);
}
else if (message.compare(0, 25, L"EnableBrowserAccelerators") == 0)
{
CHECK_FAILURE(m_settings3->put_AreBrowserAcceleratorKeysEnabled(TRUE));
MessageBox(
nullptr,
L"Browser-specific accelerator keys, for example: \n"
L"Ctrl+F and F3 for Find on Page\n"
L"Ctrl+R and F5 for Reload\n"
L"Ctrl+Plus and Ctrl+Minus for zooming\n"
L"Ctrl+Shift-C and F12 for DevTools\n"
L"Special keys for browser functions, such as Back, Forward, and "
L"Search \n"
L"will be enabled after the next navigation except for Ctr + P.",
L"Settings change", MB_OK);
}
return S_OK;
})
.Get(),
&m_webMessageReceivedToken));
//! [IsBrowserAcceleratorKeyEnabled]
if (m_settings3)
{
// Register a handler for the AcceleratorKeyPressed event.
CHECK_FAILURE(m_controller->add_AcceleratorKeyPressed(
Callback<ICoreWebView2AcceleratorKeyPressedEventHandler>(
[this](
ICoreWebView2Controller* sender,
ICoreWebView2AcceleratorKeyPressedEventArgs* args) -> HRESULT
{
COREWEBVIEW2_KEY_EVENT_KIND kind;
CHECK_FAILURE(args->get_KeyEventKind(&kind));
// We only care about key down events.
if (kind == COREWEBVIEW2_KEY_EVENT_KIND_KEY_DOWN ||
kind == COREWEBVIEW2_KEY_EVENT_KIND_SYSTEM_KEY_DOWN)
{
UINT key;
CHECK_FAILURE(args->get_VirtualKey(&key));
wil::com_ptr<ICoreWebView2AcceleratorKeyPressedEventArgs2> args2;
args->QueryInterface(IID_PPV_ARGS(&args2));
if (args2)
{
if (key == 'P' && (GetKeyState(VK_CONTROL) < 0))
{
// tell the browser to skip the key
CHECK_FAILURE(args2->put_IsBrowserAcceleratorKeyEnabled(FALSE));
}
if (key == VK_F7)
{
// tell the browser to process the key
CHECK_FAILURE(args2->put_IsBrowserAcceleratorKeyEnabled(TRUE));
}
}
}
return S_OK;
})
.Get(),
&m_acceleratorKeyPressedToken));
}
//! [IsBrowserAcceleratorKeyEnabled]
// Turn off this scenario if we navigate away from the sample page
CHECK_FAILURE(m_webView->add_ContentLoading(
Callback<ICoreWebView2ContentLoadingEventHandler>(
[this](ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args) -> HRESULT
{
wil::unique_cotaskmem_string uri;
sender->get_Source(&uri);
if (uri.get() != m_sampleUri)
{
m_appWindow->DeleteComponent(this);
}
return S_OK;
})
.Get(),
&m_contentLoadingToken));
CHECK_FAILURE(m_webView->Navigate(m_sampleUri.c_str()));
}
ScenarioAcceleratorKeyPressed::~ScenarioAcceleratorKeyPressed()
{
m_webView->remove_WebMessageReceived(m_webMessageReceivedToken);
m_controller->remove_AcceleratorKeyPressed(m_acceleratorKeyPressedToken);
m_webView->remove_ContentLoading(m_contentLoadingToken);
}