-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from sieren/add-customizable-hotkey
Add Customizable Hotkeys
- Loading branch information
Showing
6 changed files
with
155 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...Toggler/WidgetToggler.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+767 Bytes
(100%)
...roj/project.xcworkspace/xcuserdata/sierenmusic.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) 2023 Matthias Frick. All rights reserved. | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
import KeyboardShortcuts | ||
|
||
struct SettingsScreen: View { | ||
@Environment(\.dismiss) var dismiss | ||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
Spacer() | ||
Text("Set a global HotKey to toggle Widgets.") | ||
Form { | ||
KeyboardShortcuts.Recorder("HotKey:", name: .toggleWidgetMode) | ||
} | ||
Text("Requires restarting WidgetToggler to show in the UI.") | ||
Spacer() | ||
} | ||
.frame(minWidth: 200) | ||
.padding(50) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright (c) 2023 Matthias Frick. All rights reserved. | ||
|
||
import Carbon | ||
import Foundation | ||
import SwiftUI | ||
|
||
import KeyboardShortcuts | ||
|
||
extension KeyboardShortcuts.Name { | ||
static let toggleWidgetMode = Self( | ||
"ToggleWidgetMode", | ||
default: .init( | ||
.w, | ||
modifiers: [.command, .control] | ||
) | ||
) | ||
} | ||
|
||
extension View { | ||
public func keyboardShortcut(_ shortcut: KeyboardShortcuts.Name) -> some View { | ||
if let shortcut = shortcut.shortcut { | ||
if let keyEquivalent = shortcut.toKeyEquivalent() { | ||
return AnyView(self.keyboardShortcut(keyEquivalent, modifiers: shortcut.toEventModifiers())) | ||
} | ||
} | ||
|
||
return AnyView(self) | ||
} | ||
} | ||
|
||
extension KeyboardShortcuts.Shortcut { | ||
|
||
func toKeyEquivalent() -> KeyEquivalent? { | ||
let carbonKeyCode = UInt16(self.carbonKeyCode) | ||
let maxNameLength = 4 | ||
var nameBuffer = [UniChar](repeating: 0, count : maxNameLength) | ||
var nameLength = 0 | ||
|
||
let modifierKeys = UInt32(alphaLock >> 8) & 0xFF // Caps Lock | ||
var deadKeys: UInt32 = 0 | ||
let keyboardType = UInt32(LMGetKbdType()) | ||
|
||
let source = TISCopyCurrentKeyboardLayoutInputSource().takeRetainedValue() | ||
guard let ptr = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData) else { | ||
NSLog("Could not get keyboard layout data") | ||
return nil | ||
} | ||
let layoutData = Unmanaged<CFData>.fromOpaque(ptr).takeUnretainedValue() as Data | ||
let osStatus = layoutData.withUnsafeBytes { | ||
UCKeyTranslate($0.bindMemory(to: UCKeyboardLayout.self).baseAddress, carbonKeyCode, UInt16(kUCKeyActionDown), | ||
modifierKeys, keyboardType, UInt32(kUCKeyTranslateNoDeadKeysMask), | ||
&deadKeys, maxNameLength, &nameLength, &nameBuffer) | ||
} | ||
guard osStatus == noErr else { | ||
NSLog("Code: 0x%04X Status: %+i", carbonKeyCode, osStatus); | ||
return nil | ||
} | ||
|
||
return KeyEquivalent(Character(String(utf16CodeUnits: nameBuffer, count: nameLength))) | ||
} | ||
|
||
func toEventModifiers() -> SwiftUI.EventModifiers { | ||
var modifiers: SwiftUI.EventModifiers = [] | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.command) { | ||
modifiers.update(with: EventModifiers.command) | ||
} | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.control) { | ||
modifiers.update(with: EventModifiers.control) | ||
} | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.option) { | ||
modifiers.update(with: EventModifiers.option) | ||
} | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.shift) { | ||
modifiers.update(with: EventModifiers.shift) | ||
} | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.capsLock) { | ||
modifiers.update(with: EventModifiers.capsLock) | ||
} | ||
|
||
if self.modifiers.contains(NSEvent.ModifierFlags.numericPad) { | ||
modifiers.update(with: EventModifiers.numericPad) | ||
} | ||
|
||
return modifiers | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters