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