Skip to content

Commit

Permalink
Cleanup for #217
Browse files Browse the repository at this point in the history
  • Loading branch information
kasper committed Dec 27, 2018
1 parent 5c2e57d commit c439895
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand All @@ -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
-----
Expand Down
4 changes: 2 additions & 2 deletions Phoenix/PHKeyTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -12,7 +12,7 @@
#pragma mark - Translating

+ (UInt32) modifierFlagsForModifiers:(NSArray<NSString *> *)modifiers;
+ (NSArray<NSString *> *) modifiersForModifierFlags:(UInt32)modifierFlags;
+ (NSArray<NSString *> *) modifiersForModifierFlags:(NSEventModifierFlags)modifierFlags;
+ (UInt32) keyCodeForKey:(NSString *)key;

@end
41 changes: 23 additions & 18 deletions Phoenix/PHKeyTranslator.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

@import Carbon;
@import Cocoa;

#import "PHKeyTranslator.h"

Expand All @@ -30,23 +29,6 @@ + (NSNumber *) flagForModifier:(NSString *)modifier {
return modifierToFlag[modifier];
}

+ (NSArray<NSString *> *) modifiersForModifierFlags:(UInt32)modifierFlags {
NSMutableArray<NSString *> *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 {
Expand Down Expand Up @@ -248,6 +230,29 @@ + (UInt32) modifierFlagsForModifiers:(NSArray<NSString *> *)modifiers {
return flags;
}

+ (NSArray<NSString *> *) modifiersForModifierFlags:(NSEventModifierFlags)modifierFlags {

NSMutableArray<NSString *> *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
Expand Down
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c439895

Please sign in to comment.