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

Support for a variety of keyboard layouts #44

Merged
merged 10 commits into from
Apr 30, 2020
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
6 changes: 3 additions & 3 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// ⌘ + Control + B
guard let keyCombo = KeyCombo(keyCode: 11, carbonModifiers: 4352) else { return }
guard let keyCombo = KeyCombo(key: .b, cocoaModifiers: [.command, .control]) else { return }
let hotKey = HotKey(identifier: "CommandControlB",
keyCombo: keyCombo,
target: self,
action: #selector(AppDelegate.tappedHotKey))
hotKey.register()

// Shift + Control + A
guard let keyCombo2 = KeyCombo(keyCode: 0, cocoaModifiers: [.shift, .control]) else { return }
guard let keyCombo2 = KeyCombo(key: .a, cocoaModifiers: [.shift, .control]) else { return }
let hotKey2 = HotKey(identifier: "ShiftControlA",
keyCombo: keyCombo2,
target: self,
Expand All @@ -43,7 +43,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
hotKey3.register()

// Shift Double Tap
guard let keyCombo4 = KeyCombo(doubledCocoaModifiers: .shift) else { return }
guard let keyCombo4 = KeyCombo(doubledCarbonModifiers: shiftKey) else { return }
let hotKey4 = HotKey(identifier: "ShiftDoubleTap",
keyCombo: keyCombo4,
target: self,
Expand Down
36 changes: 24 additions & 12 deletions Lib/Magnet.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
/* Begin PBXBuildFile section */
091D85D22459554600930473 /* Sauce.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 091D85CF2459553A00930473 /* Sauce.framework */; };
091D85D32459554600930473 /* Sauce.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 091D85CF2459553A00930473 /* Sauce.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
FA3AA2142315A51F007EAA1F /* CollectionExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3AA2132315A51F007EAA1F /* CollectionExtension.swift */; };
091D85EA245A917500930473 /* NSEventExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091D85E6245A917500930473 /* NSEventExtension.swift */; };
091D85EB245A917500930473 /* IntExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091D85E7245A917500930473 /* IntExtension.swift */; };
091D85EC245A917500930473 /* KeyExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091D85E8245A917500930473 /* KeyExtension.swift */; };
091D85ED245A917500930473 /* CollectionExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 091D85E9245A917500930473 /* CollectionExtension.swift */; };
FA3AA2162315A6A3007EAA1F /* CollectionExtensionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA3AA2152315A6A3007EAA1F /* CollectionExtensionTests.swift */; };
FA7145471D147F1B004A04D4 /* KeyTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FA7145461D147F1B004A04D4 /* KeyTransformer.swift */; };
FAEC34B31C9059DF004177E2 /* Magnet.h in Headers */ = {isa = PBXBuildFile; fileRef = FAEC34B21C9059DF004177E2 /* Magnet.h */; settings = {ATTRIBUTES = (Public, ); }; };
FAEC34BA1C9059DF004177E2 /* Magnet.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FAEC34AF1C9059DF004177E2 /* Magnet.framework */; };
FAEC34BF1C9059DF004177E2 /* MagnetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC34BE1C9059DF004177E2 /* MagnetTests.swift */; };
FAEC34D81C905B4D004177E2 /* HotKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC34D71C905B4D004177E2 /* HotKey.swift */; };
FAEC34DA1C905B5A004177E2 /* KeyCombo.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC34D91C905B5A004177E2 /* KeyCombo.swift */; };
FAEC34DE1C905B7C004177E2 /* HotKeyCenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAEC34DD1C905B7C004177E2 /* HotKeyCenter.swift */; };
FAFA5B711D1FD1FA003CB2DC /* KeyCodeTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = FAFA5B701D1FD1FA003CB2DC /* KeyCodeTransformer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -61,9 +62,11 @@

/* Begin PBXFileReference section */
091D85C92459553A00930473 /* Sauce.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = Sauce.xcodeproj; path = ../Carthage/Checkouts/Sauce/Lib/Sauce.xcodeproj; sourceTree = "<group>"; };
FA3AA2132315A51F007EAA1F /* CollectionExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionExtension.swift; sourceTree = "<group>"; };
091D85E6245A917500930473 /* NSEventExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSEventExtension.swift; sourceTree = "<group>"; };
091D85E7245A917500930473 /* IntExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IntExtension.swift; sourceTree = "<group>"; };
091D85E8245A917500930473 /* KeyExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyExtension.swift; sourceTree = "<group>"; };
091D85E9245A917500930473 /* CollectionExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionExtension.swift; sourceTree = "<group>"; };
FA3AA2152315A6A3007EAA1F /* CollectionExtensionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionExtensionTests.swift; sourceTree = "<group>"; };
FA7145461D147F1B004A04D4 /* KeyTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyTransformer.swift; sourceTree = "<group>"; };
FAEC34AF1C9059DF004177E2 /* Magnet.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Magnet.framework; sourceTree = BUILT_PRODUCTS_DIR; };
FAEC34B21C9059DF004177E2 /* Magnet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Magnet.h; sourceTree = "<group>"; };
FAEC34B41C9059DF004177E2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -73,7 +76,6 @@
FAEC34D71C905B4D004177E2 /* HotKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HotKey.swift; sourceTree = "<group>"; };
FAEC34D91C905B5A004177E2 /* KeyCombo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyCombo.swift; sourceTree = "<group>"; };
FAEC34DD1C905B7C004177E2 /* HotKeyCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HotKeyCenter.swift; sourceTree = "<group>"; };
FAFA5B701D1FD1FA003CB2DC /* KeyCodeTransformer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyCodeTransformer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -113,6 +115,17 @@
name = Products;
sourceTree = "<group>";
};
091D85E5245A915A00930473 /* Extensions */ = {
isa = PBXGroup;
children = (
091D85E9245A917500930473 /* CollectionExtension.swift */,
091D85E7245A917500930473 /* IntExtension.swift */,
091D85E8245A917500930473 /* KeyExtension.swift */,
091D85E6245A917500930473 /* NSEventExtension.swift */,
);
path = Extensions;
sourceTree = "<group>";
};
FAEC34A51C9059DF004177E2 = {
isa = PBXGroup;
children = (
Expand All @@ -135,12 +148,10 @@
FAEC34B11C9059DF004177E2 /* Magnet */ = {
isa = PBXGroup;
children = (
091D85E5245A915A00930473 /* Extensions */,
FAEC34D71C905B4D004177E2 /* HotKey.swift */,
FAEC34D91C905B5A004177E2 /* KeyCombo.swift */,
FAEC34DD1C905B7C004177E2 /* HotKeyCenter.swift */,
FA7145461D147F1B004A04D4 /* KeyTransformer.swift */,
FAFA5B701D1FD1FA003CB2DC /* KeyCodeTransformer.swift */,
FA3AA2132315A51F007EAA1F /* CollectionExtension.swift */,
FAEC34B21C9059DF004177E2 /* Magnet.h */,
FAEC34B41C9059DF004177E2 /* Info.plist */,
);
Expand Down Expand Up @@ -293,11 +304,12 @@
buildActionMask = 2147483647;
files = (
FAEC34DA1C905B5A004177E2 /* KeyCombo.swift in Sources */,
FA3AA2142315A51F007EAA1F /* CollectionExtension.swift in Sources */,
FAEC34DE1C905B7C004177E2 /* HotKeyCenter.swift in Sources */,
091D85EA245A917500930473 /* NSEventExtension.swift in Sources */,
FAEC34D81C905B4D004177E2 /* HotKey.swift in Sources */,
FAFA5B711D1FD1FA003CB2DC /* KeyCodeTransformer.swift in Sources */,
FA7145471D147F1B004A04D4 /* KeyTransformer.swift in Sources */,
091D85EC245A917500930473 /* KeyExtension.swift in Sources */,
091D85ED245A917500930473 /* CollectionExtension.swift in Sources */,
091D85EB245A917500930473 /* IntExtension.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import Foundation

extension Collection where Element == Bool {
public extension Collection where Element == Bool {
var trueCount: Int {
return filter { $0 }.count
}
Expand Down
31 changes: 31 additions & 0 deletions Lib/Magnet/Extensions/IntExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// IntExtension.swift
//
// Magnet
// GitHub: https://github.com/clipy
// HP: https://clipy-app.com
//
// Copyright © 2015-2020 Clipy Project.
//

import Cocoa
import Carbon

extension Int {
func convertSupportCococaModifiers() -> NSEvent.ModifierFlags {
var cocoaFlags: NSEvent.ModifierFlags = NSEvent.ModifierFlags(rawValue: 0)
if (self & cmdKey) != 0 {
cocoaFlags.insert(.command)
}
if (self & optionKey) != 0 {
cocoaFlags.insert(.option)
}
if (self & controlKey) != 0 {
cocoaFlags.insert(.control)
}
if (self & shiftKey) != 0 {
cocoaFlags.insert(.shift)
}
return cocoaFlags
}
}
42 changes: 42 additions & 0 deletions Lib/Magnet/Extensions/KeyExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// KeyExtension.swift
//
// Magnet
// GitHub: https://github.com/clipy
// HP: https://clipy-app.com
//
// Copyright © 2015-2020 Clipy Project.
//

import Foundation
import Sauce

public extension Key {
var isFunctionKey: Bool {
switch self {
case .f1,
.f2,
.f3,
.f4,
.f5,
.f6,
.f7,
.f8,
.f9,
.f10,
.f11,
.f12,
.f13,
.f14,
.f15,
.f16,
.f17,
.f18,
.f19,
.f20:
return true
default:
return false
}
}
}
62 changes: 62 additions & 0 deletions Lib/Magnet/Extensions/NSEventExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// NSEventExtension.swift
//
// Magnet
// GitHub: https://github.com/clipy
// HP: https://clipy-app.com
//
// Copyright © 2015-2020 Clipy Project.
//

import Cocoa
import Carbon

public extension NSEvent.ModifierFlags {
var containsSupportModifiers: Bool {
return contains(.command) || contains(.option) || contains(.control) || contains(.shift) || contains(.function)
}
var isSingleFlags: Bool {
let commandSelected = contains(.command)
let optionSelected = contains(.option)
let controlSelected = contains(.control)
let shiftSelected = contains(.shift)
return [commandSelected, optionSelected, controlSelected, shiftSelected].trueCount == 1
}

func filterUnsupportModifiers() -> NSEvent.ModifierFlags {
var filterdModifierFlags = NSEvent.ModifierFlags(rawValue: 0)
if contains(.command) {
filterdModifierFlags.insert(.command)
}
if contains(.option) {
filterdModifierFlags.insert(.option)
}
if contains(.control) {
filterdModifierFlags.insert(.control)
}
if contains(.shift) {
filterdModifierFlags.insert(.shift)
}
return filterdModifierFlags
}

func carbonModifiers(isSupportFunctionKey: Bool = false) -> Int {
var carbonModifiers: Int = 0
if contains(.command) {
carbonModifiers |= cmdKey
}
if contains(.option) {
carbonModifiers |= optionKey
}
if contains(.control) {
carbonModifiers |= controlKey
}
if contains(.shift) {
carbonModifiers |= shiftKey
}
if contains(.function) && isSupportFunctionKey {
carbonModifiers |= Int(NSEvent.ModifierFlags.function.rawValue)
}
return carbonModifiers
}
}
Loading