1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/event_monitor_mac.h"
7 #include "base/logging.h"
8 #include "ui/events/event.h"
9 #include "ui/events/event_handler.h"
10 #include "ui/events/event_utils.h"
15 EventMonitor* EventMonitor::Create(ui::EventHandler* event_handler) {
16 return new EventMonitorMac(event_handler);
20 gfx::Point EventMonitor::GetLastMouseLocation() {
21 NSPoint mouseLocation = [NSEvent mouseLocation];
22 // Flip coordinates to gfx (0,0 in top-left corner) using primary screen.
23 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
24 mouseLocation.y = NSMaxY([screen frame]) - mouseLocation.y;
25 return gfx::Point(mouseLocation.x, mouseLocation.y);
28 EventMonitorMac::EventMonitorMac(ui::EventHandler* event_handler) {
29 DCHECK(event_handler);
30 monitor_ = [NSEvent addLocalMonitorForEventsMatchingMask:NSAnyEventMask
31 handler:^NSEvent*(NSEvent* event){
32 scoped_ptr<ui::Event> ui_event = ui::EventFromNative(event);
33 event_handler->OnEvent(ui_event.get());
38 EventMonitorMac::~EventMonitorMac() {
39 [NSEvent removeMonitor:monitor_];