From c439895208945d3536c1410c242ed3e908d6612c Mon Sep 17 00:00:00 2001 From: Kasper Hirvikoski Date: Thu, 27 Dec 2018 15:48:40 +0200 Subject: [PATCH] Cleanup for #217 --- CHANGELOG.md | 5 ++++- Phoenix/PHKeyTranslator.h | 4 ++-- Phoenix/PHKeyTranslator.m | 41 ++++++++++++++++++++++----------------- docs/API.md | 2 +- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea569086..c76f31d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ Release: dd.mm.yyyy - New: Add support for `command`, `option` and `control` as additional key modifiers. This is purely syntactic sugar representing newer Apple keyboard layouts and they alias the existing `cmd`, `alt` and `ctrl` modifiers in this order ([#173](https://github.com/kasper/phoenix/issues/173)). +#### Events + +- Change: Add key modifiers to mouse events ([#216](https://github.com/kasper/phoenix/issues/216), [#217](https://github.com/kasper/phoenix/pull/217)). + 2.6.2 ----- @@ -38,7 +42,6 @@ Release: 7.6.2018 #### 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/PHKeyTranslator.h b/Phoenix/PHKeyTranslator.h index 95578470..f1d5ecd3 100644 --- a/Phoenix/PHKeyTranslator.h +++ b/Phoenix/PHKeyTranslator.h @@ -2,7 +2,7 @@ * Phoenix is released under the MIT License. Refer to https://github.com/kasper/phoenix/blob/master/LICENSE.md */ -@import Foundation; +@import Cocoa; @interface PHKeyTranslator : NSObject @@ -12,7 +12,7 @@ #pragma mark - Translating + (UInt32) modifierFlagsForModifiers:(NSArray *)modifiers; -+ (NSArray *) modifiersForModifierFlags:(UInt32)modifierFlags; ++ (NSArray *) modifiersForModifierFlags:(NSEventModifierFlags)modifierFlags; + (UInt32) keyCodeForKey:(NSString *)key; @end diff --git a/Phoenix/PHKeyTranslator.m b/Phoenix/PHKeyTranslator.m index 47242cf2..dcc94ff8 100644 --- a/Phoenix/PHKeyTranslator.m +++ b/Phoenix/PHKeyTranslator.m @@ -3,7 +3,6 @@ */ @import Carbon; -@import Cocoa; #import "PHKeyTranslator.h" @@ -30,23 +29,6 @@ + (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 { @@ -248,6 +230,29 @@ + (UInt32) modifierFlagsForModifiers:(NSArray *)modifiers { return flags; } ++ (NSArray *) modifiersForModifierFlags:(NSEventModifierFlags)modifierFlags { + + NSMutableArray *modifiers = [NSMutableArray array]; + + if (modifierFlags & NSEventModifierFlagCommand) { + [modifiers addObjectsFromArray:@[ @"command", @"cmd" ]]; + } + + if (modifierFlags & NSEventModifierFlagOption) { + [modifiers addObjectsFromArray:@[ @"option", @"alt" ]]; + } + + if (modifierFlags & NSEventModifierFlagControl) { + [modifiers addObjectsFromArray:@[ @"control", @"ctrl" ]]; + } + + if (modifierFlags & NSEventModifierFlagShift) { + [modifiers addObject:@"shift"]; + } + + return [modifiers copy]; +} + + (UInt32) keyCodeForKey:(NSString *)key { // Local key diff --git a/docs/API.md b/docs/API.md index bff69a0c..39419e3f 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. The object will also contain a `modifiers` arrays which will contain the modifier keys pressed when the mouse event occurred. +All of the following mouse events receive the corresponding `Point`-object as the first argument for the callback function. This object is also enhanced with a `modifiers` array which contains the key modifiers pressed when the mouse event is triggered. - `mouseDidMove` triggered when the mouse has moved - `mouseDidLeftClick` triggered when the mouse did left click