Chromium Code Reviews| Index: media/base/user_input_monitor.cc |
| diff --git a/media/base/user_input_monitor.cc b/media/base/user_input_monitor.cc |
| index 18b4c8060f64e34437d4e3e9222fde4d18af520a..68552eb78ca3cc21f2cb5062ecf2be573069159e 100644 |
| --- a/media/base/user_input_monitor.cc |
| +++ b/media/base/user_input_monitor.cc |
| @@ -6,6 +6,14 @@ |
| #include "third_party/skia/include/core/SkPoint.h" |
| +#if defined(OS_MACOSX) |
| +#include <ApplicationServices/ApplicationServices.h> |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/time/time.h" |
| +#endif // defined(OS_MACOSX) |
| + |
| namespace media { |
| #ifdef DISABLE_USER_INPUT_MONITOR |
| @@ -40,19 +48,33 @@ void UserInputMonitor::RemoveMouseListener(MouseEventListener* listener) { |
| } |
| } |
| void UserInputMonitor::AddKeyStrokeListener(KeyStrokeListener* listener) { |
| - base::AutoLock auto_lock(lock_); |
| - key_stroke_listeners_.AddObserver(listener); |
| - if (!monitoring_keyboard_) { |
| - StartKeyboardMonitoring(); |
| - monitoring_keyboard_ = true; |
| - DVLOG(2) << "Started keyboard monitoring."; |
| + bool start_polling = false; |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + key_stroke_listeners_.AddObserver(listener); |
| + if (!monitoring_keyboard_) { |
| + #if !defined(OS_MACOSX) |
| + StartKeyboardMonitoring(); |
| + #else |
| + start_polling = true; |
| + #endif // defined(OS_MACOSX) |
| + monitoring_keyboard_ = true; |
| + DVLOG(2) << "Started keyboard monitoring."; |
| + } |
| + } |
| + |
| + if (start_polling) { |
| + PollKeyState(); |
| } |
| } |
| void UserInputMonitor::RemoveKeyStrokeListener(KeyStrokeListener* listener) { |
| base::AutoLock auto_lock(lock_); |
| key_stroke_listeners_.RemoveObserver(listener); |
| if (key_stroke_listeners_.size() == 0) { |
| +#if !defined(OS_MACOSX) |
| StopKeyboardMonitoring(); |
| +#endif // !defined(OS_MACOSX) |
| + |
| monitoring_keyboard_ = false; |
| DVLOG(2) << "Stopped keyboard monitoring."; |
| } |
| @@ -84,4 +106,40 @@ void UserInputMonitor::OnKeyboardEvent(ui::EventType event, |
| } |
| } |
| +void UserInputMonitor::PollKeyState() { |
|
DaleCurtis
2013/08/19 23:49:00
Seems like this should go in UserInputMonitorMac,
jiayl
2013/08/20 00:21:15
I would need another pressed_key_map in UserInputM
DaleCurtis
2013/08/20 00:35:54
If you allow OnKeyboardEvent() to be called w/ ET_
jiayl
2013/08/20 00:42:59
So you mean firing Key_release for all keys not in
DaleCurtis
2013/08/20 00:52:18
OnKeyboardEvent() doesn't fire anything for ET_KEY
|
| +#if defined(OS_MACOSX) |
| + { |
| + base::AutoLock auto_lock(lock_); |
| + if (!monitoring_keyboard_) |
| + return; |
| + } |
| + bool key_pressed = false; |
| + |
| + // 0x5d is key "9", after that comes function keys. |
| + for (int key_index = 0; key_index <= 0x5d; ++key_index) { |
| + bool key_state = CGEventSourceKeyState(kCGEventSourceStateHIDSystemState, |
| + key_index); |
|
DaleCurtis
2013/08/19 23:49:00
Indent is off.
jiayl
2013/08/20 00:21:15
Done.
|
| + |
| + if (key_state && pressed_keys_.find(key_index) == pressed_keys_.end()) { |
|
DaleCurtis
2013/08/19 23:49:00
Save result so you're not calling find() twice.
jiayl
2013/08/20 00:21:15
Done.
|
| + key_pressed = true;; |
| + pressed_keys_.insert(key_index); |
| + } else if (!key_state && |
| + pressed_keys_.find(key_index) != pressed_keys_.end()) { |
|
DaleCurtis
2013/08/19 23:49:00
Ditto.
jiayl
2013/08/20 00:21:15
Done.
|
| + pressed_keys_.erase(key_index); |
| + } |
| + } |
| + |
| + if (key_pressed) { |
| + base::AutoLock auto_lock(lock_); |
| + DVLOG(6) << "Key stroke detected."; |
| + FOR_EACH_OBSERVER(KeyStrokeListener, key_stroke_listeners_, OnKeyStroke()); |
| + } |
| + |
| + base::MessageLoop::current()->PostDelayedTask( |
|
DaleCurtis
2013/08/19 23:49:00
Which loop is this running on? It looks like it'll
jiayl
2013/08/20 00:21:15
Yeh, this is gross. But I don't want to run it on
DaleCurtis
2013/08/20 00:35:54
Well not only is it gross, it doesn't work: consid
DaleCurtis
2013/08/20 00:40:00
Ah, nevermind those require Assistive as well :/
jiayl
2013/08/20 00:42:59
Right, the document says
A global event monitor lo
DaleCurtis
2013/08/20 00:52:18
Can we ask users for assistive access? FYI, on OS
|
| + FROM_HERE, |
| + base::Bind(&UserInputMonitor::PollKeyState, base::Unretained(this)), |
| + base::TimeDelta::FromMilliseconds(10)); |
| +#endif // defined(OS_MACOSX) |
| +} |
| + |
| } // namespace media |