Skip to content

Commit

Permalink
Merge pull request #217 from DanielGibbsNZ/feature/modifier-flags-for…
Browse files Browse the repository at this point in the history
…-mouse-events

Add modifier flags for mouse events
  • Loading branch information
kasper authored Dec 27, 2018
2 parents f8d45e4 + 60678c0 commit 5c2e57d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ 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
8 changes: 7 additions & 1 deletion Phoenix/PHGlobalEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#import "PHEventConstants.h"
#import "PHGlobalEventMonitor.h"
#import "PHKeyTranslator.h"
#import "PHMouse.h"

@interface PHGlobalEventMonitor ()
Expand Down Expand Up @@ -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<NSString *> *modifiers = [PHKeyTranslator modifiersForModifierFlags:event.modifierFlags];
userInfo[PHGlobalEventMonitorMouseKey] = @{
@"x": @(location.x),
@"y": @(location.y),
@"modifiers": modifiers,
};
}

[[NSNotificationCenter defaultCenter] postNotificationName:notification object:nil userInfo:userInfo];
Expand Down
1 change: 1 addition & 0 deletions Phoenix/PHKeyTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma mark - Translating

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

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

@import Carbon;
@import Cocoa;

#import "PHKeyTranslator.h"

Expand All @@ -29,6 +30,23 @@ + (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
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.
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
Expand Down

0 comments on commit 5c2e57d

Please sign in to comment.