OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/automation/ui_controls.h" | 5 #include "chrome/browser/automation/ui_controls.h" |
6 | 6 |
7 #include "base/keyboard_codes.h" | 7 #include "app/keyboard_code_conversion_win.h" |
| 8 #include "app/keyboard_codes.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/win_util.h" | 11 #include "base/win_util.h" |
11 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
12 #include "base/task.h" | 13 #include "base/task.h" |
13 #include "views/view.h" | 14 #include "views/view.h" |
14 | 15 |
15 namespace ui_controls { | 16 namespace ui_controls { |
16 | 17 |
17 namespace { | 18 namespace { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 131 |
131 void InputDispatcher::NotifyTask() { | 132 void InputDispatcher::NotifyTask() { |
132 task_->Run(); | 133 task_->Run(); |
133 Release(); | 134 Release(); |
134 } | 135 } |
135 | 136 |
136 // Private functions ---------------------------------------------------------- | 137 // Private functions ---------------------------------------------------------- |
137 | 138 |
138 // Populate the INPUT structure with the appropriate keyboard event | 139 // Populate the INPUT structure with the appropriate keyboard event |
139 // parameters required by SendInput | 140 // parameters required by SendInput |
140 bool FillKeyboardInput(base::KeyboardCode key, INPUT* input, bool key_up) { | 141 bool FillKeyboardInput(app::KeyboardCode key, INPUT* input, bool key_up) { |
141 memset(input, 0, sizeof(INPUT)); | 142 memset(input, 0, sizeof(INPUT)); |
142 input->type = INPUT_KEYBOARD; | 143 input->type = INPUT_KEYBOARD; |
143 input->ki.wVk = win_util::KeyboardCodeToWin(key); | 144 input->ki.wVk = app::WindowsKeyCodeForKeyboardCode(key); |
144 input->ki.dwFlags = key_up ? KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP : | 145 input->ki.dwFlags = key_up ? KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP : |
145 KEYEVENTF_EXTENDEDKEY; | 146 KEYEVENTF_EXTENDEDKEY; |
146 | 147 |
147 return true; | 148 return true; |
148 } | 149 } |
149 | 150 |
150 // Send a key event (up/down) | 151 // Send a key event (up/down) |
151 bool SendKeyEvent(base::KeyboardCode key, bool up) { | 152 bool SendKeyEvent(app::KeyboardCode key, bool up) { |
152 INPUT input = { 0 }; | 153 INPUT input = { 0 }; |
153 | 154 |
154 if (!FillKeyboardInput(key, &input, up)) | 155 if (!FillKeyboardInput(key, &input, up)) |
155 return false; | 156 return false; |
156 | 157 |
157 if (!::SendInput(1, &input, sizeof(INPUT))) | 158 if (!::SendInput(1, &input, sizeof(INPUT))) |
158 return false; | 159 return false; |
159 | 160 |
160 return true; | 161 return true; |
161 } | 162 } |
162 | 163 |
163 bool SendKeyPressImpl(base::KeyboardCode key, | 164 bool SendKeyPressImpl(app::KeyboardCode key, |
164 bool control, bool shift, bool alt, | 165 bool control, bool shift, bool alt, |
165 Task* task) { | 166 Task* task) { |
166 scoped_refptr<InputDispatcher> dispatcher( | 167 scoped_refptr<InputDispatcher> dispatcher( |
167 task ? new InputDispatcher(task, WM_KEYUP) : NULL); | 168 task ? new InputDispatcher(task, WM_KEYUP) : NULL); |
168 | 169 |
169 // If a pop-up menu is open, it won't receive events sent using SendInput. | 170 // If a pop-up menu is open, it won't receive events sent using SendInput. |
170 // Check for a pop-up menu using its window class (#32768) and if one | 171 // Check for a pop-up menu using its window class (#32768) and if one |
171 // exists, send the key event directly there. | 172 // exists, send the key event directly there. |
172 HWND popup_menu = ::FindWindow(L"#32768", 0); | 173 HWND popup_menu = ::FindWindow(L"#32768", 0); |
173 if (popup_menu != NULL && popup_menu == ::GetTopWindow(NULL)) { | 174 if (popup_menu != NULL && popup_menu == ::GetTopWindow(NULL)) { |
174 WPARAM w_param = win_util::KeyboardCodeToWin(key); | 175 WPARAM w_param = app::WindowsKeyCodeForKeyboardCode(key); |
175 LPARAM l_param = 0; | 176 LPARAM l_param = 0; |
176 ::SendMessage(popup_menu, WM_KEYDOWN, w_param, l_param); | 177 ::SendMessage(popup_menu, WM_KEYDOWN, w_param, l_param); |
177 ::SendMessage(popup_menu, WM_KEYUP, w_param, l_param); | 178 ::SendMessage(popup_menu, WM_KEYUP, w_param, l_param); |
178 | 179 |
179 if (dispatcher.get()) | 180 if (dispatcher.get()) |
180 dispatcher->AddRef(); | 181 dispatcher->AddRef(); |
181 return true; | 182 return true; |
182 } | 183 } |
183 | 184 |
184 INPUT input[8] = { 0 }; // 8, assuming all the modifiers are activated | 185 INPUT input[8] = { 0 }; // 8, assuming all the modifiers are activated |
185 | 186 |
186 UINT i = 0; | 187 UINT i = 0; |
187 if (control) { | 188 if (control) { |
188 if (!FillKeyboardInput(base::VKEY_CONTROL, &input[i], false)) | 189 if (!FillKeyboardInput(app::VKEY_CONTROL, &input[i], false)) |
189 return false; | 190 return false; |
190 i++; | 191 i++; |
191 } | 192 } |
192 | 193 |
193 if (shift) { | 194 if (shift) { |
194 if (!FillKeyboardInput(base::VKEY_SHIFT, &input[i], false)) | 195 if (!FillKeyboardInput(app::VKEY_SHIFT, &input[i], false)) |
195 return false; | 196 return false; |
196 i++; | 197 i++; |
197 } | 198 } |
198 | 199 |
199 if (alt) { | 200 if (alt) { |
200 if (!FillKeyboardInput(base::VKEY_MENU, &input[i], false)) | 201 if (!FillKeyboardInput(app::VKEY_MENU, &input[i], false)) |
201 return false; | 202 return false; |
202 i++; | 203 i++; |
203 } | 204 } |
204 | 205 |
205 if (!FillKeyboardInput(key, &input[i], false)) | 206 if (!FillKeyboardInput(key, &input[i], false)) |
206 return false; | 207 return false; |
207 i++; | 208 i++; |
208 | 209 |
209 if (!FillKeyboardInput(key, &input[i], true)) | 210 if (!FillKeyboardInput(key, &input[i], true)) |
210 return false; | 211 return false; |
211 i++; | 212 i++; |
212 | 213 |
213 if (alt) { | 214 if (alt) { |
214 if (!FillKeyboardInput(base::VKEY_MENU, &input[i], true)) | 215 if (!FillKeyboardInput(app::VKEY_MENU, &input[i], true)) |
215 return false; | 216 return false; |
216 i++; | 217 i++; |
217 } | 218 } |
218 | 219 |
219 if (shift) { | 220 if (shift) { |
220 if (!FillKeyboardInput(base::VKEY_SHIFT, &input[i], true)) | 221 if (!FillKeyboardInput(app::VKEY_SHIFT, &input[i], true)) |
221 return false; | 222 return false; |
222 i++; | 223 i++; |
223 } | 224 } |
224 | 225 |
225 if (control) { | 226 if (control) { |
226 if (!FillKeyboardInput(base::VKEY_CONTROL, &input[i], true)) | 227 if (!FillKeyboardInput(app::VKEY_CONTROL, &input[i], true)) |
227 return false; | 228 return false; |
228 i++; | 229 i++; |
229 } | 230 } |
230 | 231 |
231 if (::SendInput(i, input, sizeof(INPUT)) != i) | 232 if (::SendInput(i, input, sizeof(INPUT)) != i) |
232 return false; | 233 return false; |
233 | 234 |
234 if (dispatcher.get()) | 235 if (dispatcher.get()) |
235 dispatcher->AddRef(); | 236 dispatcher->AddRef(); |
236 return true; | 237 return true; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 if (dispatcher.get()) | 316 if (dispatcher.get()) |
316 dispatcher->AddRef(); | 317 dispatcher->AddRef(); |
317 | 318 |
318 return true; | 319 return true; |
319 } | 320 } |
320 | 321 |
321 } // namespace | 322 } // namespace |
322 | 323 |
323 // public functions ----------------------------------------------------------- | 324 // public functions ----------------------------------------------------------- |
324 | 325 |
325 bool SendKeyPress(gfx::NativeWindow window, base::KeyboardCode key, | 326 bool SendKeyPress(gfx::NativeWindow window, app::KeyboardCode key, |
326 bool control, bool shift, bool alt, bool command) { | 327 bool control, bool shift, bool alt, bool command) { |
327 DCHECK(command == false); // No command key on Windows | 328 DCHECK(command == false); // No command key on Windows |
328 return SendKeyPressImpl(key, control, shift, alt, NULL); | 329 return SendKeyPressImpl(key, control, shift, alt, NULL); |
329 } | 330 } |
330 | 331 |
331 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, | 332 bool SendKeyPressNotifyWhenDone(gfx::NativeWindow window, |
332 base::KeyboardCode key, | 333 app::KeyboardCode key, |
333 bool control, bool shift, bool alt, | 334 bool control, bool shift, bool alt, |
334 bool command, | 335 bool command, |
335 Task* task) { | 336 Task* task) { |
336 DCHECK(command == false); // No command key on Windows | 337 DCHECK(command == false); // No command key on Windows |
337 return SendKeyPressImpl(key, control, shift, alt, task); | 338 return SendKeyPressImpl(key, control, shift, alt, task); |
338 } | 339 } |
339 | 340 |
340 bool SendMouseMove(long x, long y) { | 341 bool SendMouseMove(long x, long y) { |
341 return SendMouseMoveImpl(x, y, NULL); | 342 return SendMouseMoveImpl(x, y, NULL); |
342 } | 343 } |
(...skipping 18 matching lines...) Expand all Loading... |
361 int state, Task* task) { | 362 int state, Task* task) { |
362 DCHECK(view); | 363 DCHECK(view); |
363 DCHECK(view->GetWidget()); | 364 DCHECK(view->GetWidget()); |
364 gfx::Point view_center(view->width() / 2, view->height() / 2); | 365 gfx::Point view_center(view->width() / 2, view->height() / 2); |
365 views::View::ConvertPointToScreen(view, &view_center); | 366 views::View::ConvertPointToScreen(view, &view_center); |
366 SendMouseMove(view_center.x(), view_center.y()); | 367 SendMouseMove(view_center.x(), view_center.y()); |
367 SendMouseEventsNotifyWhenDone(button, state, task); | 368 SendMouseEventsNotifyWhenDone(button, state, task); |
368 } | 369 } |
369 | 370 |
370 } // ui_controls | 371 } // ui_controls |
OLD | NEW |