Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add modifier flags for mouse events #217

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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