From 98319f796575ed4ac98f4edd8b06d36ef9fbb1dc Mon Sep 17 00:00:00 2001 From: Daniel Gibbs Date: Thu, 7 Jun 2018 22:06:17 +1200 Subject: [PATCH] Add modifier flags for mouse events --- CHANGELOG.md | 1 + Phoenix/PHGlobalEventMonitor.m | 8 +++++++- Phoenix/PHKeyTranslator.h | 1 + Phoenix/PHKeyTranslator.m | 18 ++++++++++++++++++ docs/API.md | 2 +- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 47ab540c..ad0518ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ Release: dd.mm.yyyy #### App - Change: Function `launch(...)` now supports a focus option to focus the app automatically on launch ([#211](https://github.com/kasper/phoenix/issues/211), [#212](https://github.com/kasper/phoenix/pull/212)). +- New: Add modifier flags to mouse events ([#216](https://github.com/kasper/phoenix/issues/216)). 2.6.1 ----- diff --git a/Phoenix/PHGlobalEventMonitor.m b/Phoenix/PHGlobalEventMonitor.m index 18537824..283875a9 100644 --- a/Phoenix/PHGlobalEventMonitor.m +++ b/Phoenix/PHGlobalEventMonitor.m @@ -6,6 +6,7 @@ #import "PHEventConstants.h" #import "PHGlobalEventMonitor.h" +#import "PHKeyTranslator.h" #import "PHMouse.h" @interface PHGlobalEventMonitor () @@ -98,7 +99,12 @@ - (void) setup { // Event for mouse if ([notification hasPrefix:NSStringFromClass([PHMouse class])]) { CGPoint location = [PHMouse location]; - userInfo[PHGlobalEventMonitorMouseKey] = @{ @"x": @(location.x), @"y": @(location.y) }; + NSArray *modifiers = [PHKeyTranslator modifiersForModifierFlags:event.modifierFlags]; + userInfo[PHGlobalEventMonitorMouseKey] = @{ + @"x": @(location.x), + @"y": @(location.y), + @"modifiers": modifiers, + }; } [[NSNotificationCenter defaultCenter] postNotificationName:notification object:nil userInfo:userInfo]; diff --git a/Phoenix/PHKeyTranslator.h b/Phoenix/PHKeyTranslator.h index 33499857..95578470 100644 --- a/Phoenix/PHKeyTranslator.h +++ b/Phoenix/PHKeyTranslator.h @@ -12,6 +12,7 @@ #pragma mark - Translating + (UInt32) modifierFlagsForModifiers:(NSArray *)modifiers; ++ (NSArray *) modifiersForModifierFlags:(UInt32)modifierFlags; + (UInt32) keyCodeForKey:(NSString *)key; @end diff --git a/Phoenix/PHKeyTranslator.m b/Phoenix/PHKeyTranslator.m index 632d9ceb..bc05d444 100644 --- a/Phoenix/PHKeyTranslator.m +++ b/Phoenix/PHKeyTranslator.m @@ -3,6 +3,7 @@ */ @import Carbon; +@import Cocoa; #import "PHKeyTranslator.h" @@ -26,6 +27,23 @@ + (NSNumber *) flagForModifier:(NSString *)modifier { return modifierToFlag[modifier]; } ++ (NSArray *) modifiersForModifierFlags:(UInt32)modifierFlags { + NSMutableArray *modifiers = [NSMutableArray array]; + if (modifierFlags & NSEventModifierFlagCommand) { + [modifiers addObject:@"cmd"]; + } + if (modifierFlags & NSEventModifierFlagOption) { + [modifiers addObject:@"alt"]; + } + if (modifierFlags & NSEventModifierFlagControl) { + [modifiers addObject:@"ctrl"]; + } + if (modifierFlags & NSEventModifierFlagShift) { + [modifiers addObject:@"shift"]; + } + return modifiers; +} + #pragma mark - Local Key + (NSString *) characterForKeyCode:(unsigned short)keyCode { diff --git a/docs/API.md b/docs/API.md index 29f43e52..57b6a0a1 100644 --- a/docs/API.md +++ b/docs/API.md @@ -160,7 +160,7 @@ Phoenix supports the following (case sensitive) events: ### Mouse -All of the following mouse events receive the corresponding `Point`-object as the first argument for the callback function. +All of the following mouse events receive the corresponding `Point`-object as the first argument for the callback function. The object will also contain a `modifiers` arrays which will contain the modifier keys pressed when the mouse event occurred. - `mouseDidMove` triggered when the mouse has moved - `mouseDidLeftClick` triggered when the mouse did left click