├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ ├── IDETemplateMacros.plist │ └── IDEWorkspaceChecks.plist ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── VisionKeyboardKit │ ├── KeyboardButtonStyle.swift │ ├── KeyboardClickSound.swift │ ├── KeyboardEvent.swift │ ├── KeyboardEventPS2.swift │ ├── KeyboardIdentifier.swift │ ├── KeyboardKeyView.swift │ ├── KeyboardLayoutANSI.swift │ ├── KeyboardLayoutTenkeys.swift │ ├── KeyboardLayoutType.swift │ ├── KeyboardSettingsViewModifier.swift │ ├── KeyboardState.swift │ ├── KeyboardView.swift │ └── KeyboardWindowGroup.swift ├── Tests └── VisionKeyboardKitTests │ └── VisionKeyboardKitTests.swift └── screenshot.png /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | 92 | .DS_Store 93 | 94 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDETemplateMacros.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | FILEHEADER 6 | 7 | // Copyright © ___YEAR___ osy. All rights reserved. 8 | // 9 | // Licensed under the Apache License, Version 2.0 (the "License"); 10 | // you may not use this file except in compliance with the License. 11 | // You may obtain a copy of the License at 12 | // 13 | // http://www.apache.org/licenses/LICENSE-2.0 14 | // 15 | // Unless required by applicable law or agreed to in writing, software 16 | // distributed under the License is distributed on an "AS IS" BASIS, 17 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 | // See the License for the specific language governing permissions and 19 | // limitations under the License. 20 | // 21 | 22 | 23 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.9 2 | 3 | import PackageDescription 4 | 5 | let package = Package( 6 | name: "VisionKeyboardKit", 7 | platforms: [ 8 | .visionOS(.v1) 9 | ], 10 | products: [ 11 | .library( 12 | name: "VisionKeyboardKit", 13 | targets: ["VisionKeyboardKit"]), 14 | ], 15 | targets: [ 16 | .target( 17 | name: "VisionKeyboardKit"), 18 | .testTarget( 19 | name: "VisionKeyboardKitTests", 20 | dependencies: ["VisionKeyboardKit"]), 21 | ] 22 | ) 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Vision Keyboard Kit 2 | =================== 3 | ![Screenshot](screenshot.png) 4 | 5 | ## Features 6 | * Full sized (104-keys) or 60% (no arrow/num keys) mode 7 | * Support for multiple keyboard layouts 8 | * PS/2 Set 1 scan code generation 9 | * Multiple instances with independent event handlers 10 | * Key down and key up events 11 | 12 | ## Usage 13 | Declare `KeyboardWindowGroup` in your `Scene`: 14 | ```swift 15 | import SwiftUI 16 | import VisionKeyboardKit 17 | 18 | @main 19 | struct YourApp: App { 20 | var body: some Scene { 21 | WindowGroup { 22 | ContentView() 23 | } 24 | KeyboardWindowGroup() 25 | } 26 | } 27 | ``` 28 | 29 | Open/close the keyboard window using `OpenWindowAction`/`DismissWindowAction` and receive events through `KeyboardEvent.publisher(for:)`: 30 | ```swift 31 | import SwiftUI 32 | import VisionKeyboardKit 33 | 34 | struct ContentView: View { 35 | @Environment(\.openWindow) private var openWindow 36 | @Environment(\.dismissWindow) private var dismissWindow 37 | @State private var isKeyboardShown: Bool = false 38 | 39 | var body: some View { 40 | Button { 41 | if !isKeyboardShown { 42 | openWindow(keyboardFor: .global) 43 | } else { 44 | dismissWindow(keyboardFor: .global) 45 | } 46 | } label: { 47 | Text("Toggle Keyboard") 48 | }.onReceive(KeyboardEvent.publisher(for: .global)) { event in 49 | if case .keyboardDidAppear = event { 50 | isKeyboardShown = true 51 | } else if case .keyboardDidDisappear = event { 52 | isKeyboardShown = false 53 | } else { 54 | print("Got event: \(event)") 55 | } 56 | } 57 | } 58 | } 59 | ``` 60 | 61 | If you need more than one keyboard with independent event publishers, just pass in any `Hashable` which serves as an identifier for the keyboard. 62 | 63 | The returned `Publisher` can be used in the Combine framework for chained processing (such as debounce). 64 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardButtonStyle.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | import RealityKit 19 | 20 | private let kDebounceTime: Duration = .milliseconds(25) 21 | 22 | struct KeyboardButtonStyle: PrimitiveButtonStyle { 23 | let isDark: Bool 24 | let isToggled: Bool 25 | let fontSize: CGFloat 26 | let width: CGFloat 27 | let height: CGFloat 28 | let clickSound: KeyboardClickSound.File 29 | let onTouchDown: () -> Void 30 | let onTouchUp: () -> Void 31 | 32 | @State private var isPressed: Bool = false 33 | @State private var debounceTask: Task? 34 | @Environment(KeyboardState.self) private var state 35 | 36 | private func spatialGesture(configuration: Configuration) -> some Gesture { 37 | SpatialEventGesture() 38 | .onChanged { events in 39 | for event in events { 40 | switch event.kind { 41 | case .indirectPinch, .pointer, .touch: 42 | if !isPressed { 43 | isPressed = true 44 | onTouchDown() 45 | } 46 | // touch events require debouncing 47 | if event.kind == .touch { 48 | debounceTask?.cancel() 49 | debounceTask = nil 50 | } 51 | default: break 52 | } 53 | } 54 | } 55 | .onEnded { events in 56 | for event in events { 57 | switch event.kind { 58 | case .indirectPinch, .pointer: 59 | isPressed = false 60 | onTouchUp() 61 | configuration.trigger() 62 | case .touch: 63 | // ignore any .ended phase followed immediately by an .active phase 64 | let trigger = configuration.trigger 65 | debounceTask = Task { @MainActor in 66 | try? await Task.sleep(for: kDebounceTime) 67 | if !Task.isCancelled { 68 | isPressed = false 69 | onTouchUp() 70 | trigger() 71 | } 72 | } 73 | default: break 74 | } 75 | } 76 | } 77 | } 78 | 79 | func makeBody(configuration: Configuration) -> some View { 80 | ZStack { 81 | if isPressed { 82 | // we only create the RealityView when the button is activated 83 | // this is where the click sound will orginate from 84 | RealityView { content in 85 | content.add(state.clicker.entity) 86 | Task { 87 | state.clicker.play(sound: clickSound) 88 | } 89 | } 90 | } 91 | if isToggled { 92 | Capsule() 93 | .frame(width: width, height: height) 94 | .foregroundStyle(.white) 95 | .background(in: Capsule()) 96 | .backgroundStyle(.shadow(.drop(radius: 2, x: 2, y: 2))) 97 | } else if isDark { 98 | Capsule() 99 | .frame(width: width, height: height) 100 | .foregroundStyle(.thickMaterial.shadow(.inner(radius: 1, x: -1, y: -1))) 101 | .background(in: Capsule()) 102 | .backgroundStyle(.shadow(.drop(radius: 2, x: 2, y: 2))) 103 | } else { 104 | Capsule() 105 | .frame(width: width, height: height) 106 | .foregroundStyle(.shadow(.inner(radius: 1, x: -1, y: -1)).tertiary) 107 | .background(in: Capsule()) 108 | .backgroundStyle(.shadow(.drop(radius: 2, x: 2, y: 2))) 109 | } 110 | if isPressed { 111 | Capsule() 112 | .frame(width: width+8, height: height+8) 113 | .foregroundStyle(.white.opacity(0.25)) 114 | } 115 | configuration.label 116 | .font(.system(size: fontSize)) 117 | .foregroundStyle(isToggled ? .black : .white) 118 | } 119 | .frame(width: width + 4, height: height + 4) 120 | .hoverEffect() 121 | .frame(depth: isPressed ? 0 : 12) 122 | .gesture(spatialGesture(configuration: configuration)) 123 | } 124 | } 125 | 126 | extension PrimitiveButtonStyle where Self == KeyboardButtonStyle { 127 | static func keyboardButton(isDark: Bool = false, isToggled: Bool = false, fontSize: CGFloat = 24, width: CGFloat = 50, height: CGFloat = 50, clickSound: KeyboardClickSound.File = .normal, onTouchDown: @escaping () -> Void = {}, onTouchUp: @escaping () -> Void = {}) -> KeyboardButtonStyle { 128 | KeyboardButtonStyle(isDark: isDark, isToggled: isToggled, fontSize: fontSize, width: width, height: height, clickSound: clickSound, onTouchDown: onTouchDown, onTouchUp: onTouchUp) 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardClickSound.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import AVFAudio 18 | import RealityKit 19 | 20 | /// Plays the system keyboard click sound 21 | class KeyboardClickSound { 22 | enum File { 23 | case normal 24 | case delete 25 | case modifier 26 | } 27 | 28 | /// The RK entity where the sound will originate 29 | let entity = Entity() 30 | 31 | private var pressNormalPlayer: AudioPlaybackController? 32 | private var pressDeletePlayer: AudioPlaybackController? 33 | private var pressModifierPlayer: AudioPlaybackController? 34 | 35 | private func loadAudioResource(for url: URL) -> AudioFileResource? { 36 | let configuration = AudioFileResource.Configuration(calibration: .absolute(dBSPL: 58.0)) 37 | return try? .load(contentsOf: url, configuration: configuration) 38 | } 39 | 40 | #if targetEnvironment(simulator) 41 | /// This is a very hacky way to get the keyboard sound files in a simulator 42 | private lazy var uiSoundsUrl: URL = { 43 | let fm = FileManager.default 44 | let library = fm.urls(for: .libraryDirectory, in: .localDomainMask)[0] 45 | let simulators = library.appending(components: "Developer", "CoreSimulator", "Volumes") 46 | let visionos = try? fm.contentsOfDirectory(at: simulators, includingPropertiesForKeys: nil).first(where: { $0.lastPathComponent.starts(with: "xrOS") || $0.lastPathComponent.starts(with: "visionOS") }) 47 | guard let visionos = visionos else { 48 | return Bundle.main.resourceURL! 49 | } 50 | let runtimes = visionos.appending(components: "Library", "Developer", "CoreSimulator", "Profiles", "Runtimes") 51 | let runtime = try? fm.contentsOfDirectory(at: runtimes, includingPropertiesForKeys: nil).first 52 | guard let runtime = runtime else { 53 | return Bundle.main.resourceURL! 54 | } 55 | return runtime.appending(components: "Contents", "Resources", "RuntimeRoot", "System", "Library", "Audio", "UISounds") 56 | }() 57 | #else 58 | private lazy var uiSoundsUrl: URL = { 59 | let fm = FileManager.default 60 | let library = fm.urls(for: .libraryDirectory, in: .systemDomainMask)[0] 61 | return library.appending(components: "Audio", "UISounds") 62 | }() 63 | #endif 64 | 65 | private lazy var isEnabled: Bool = { 66 | var valid: DarwinBoolean = false 67 | CFPreferencesAppSynchronize("com.apple.preferences.sounds" as CFString) 68 | let enabled = CFPreferencesGetAppBooleanValue("keyboard-audio" as CFString, "com.apple.preferences.sounds" as CFString, &valid) 69 | return !valid.boolValue || enabled 70 | }() 71 | 72 | init() { 73 | // if we disabled system sounds then do not create the players 74 | guard isEnabled else { 75 | return 76 | } 77 | let pressNormalUrl = uiSoundsUrl.appending(component: "key_press_click.caf") 78 | let pressDeleteUrl = uiSoundsUrl.appending(component: "key_press_delete.caf") 79 | let pressModifierUrl = uiSoundsUrl.appending(component: "key_press_modifier.caf") 80 | if let resource = loadAudioResource(for: pressNormalUrl) { 81 | pressNormalPlayer = entity.prepareAudio(resource) 82 | } 83 | if let resource = loadAudioResource(for: pressDeleteUrl) { 84 | pressDeletePlayer = entity.prepareAudio(resource) 85 | } 86 | if let resource = loadAudioResource(for: pressModifierUrl) { 87 | pressModifierPlayer = entity.prepareAudio(resource) 88 | } 89 | } 90 | 91 | private func withAmbientSession(_ callback: () -> Void) { 92 | let session = AVAudioSession.sharedInstance() 93 | let oldCategory = session.category 94 | let oldMode = session.mode 95 | let oldPolicy = session.routeSharingPolicy 96 | let oldOptions = session.categoryOptions 97 | do { 98 | try session.setCategory(.ambient, mode: .default, policy: .default, options: .mixWithOthers) 99 | callback() 100 | try session.setCategory(oldCategory, mode: oldMode, policy: oldPolicy, options: oldOptions) 101 | } catch { 102 | print(error) 103 | } 104 | } 105 | 106 | @MainActor 107 | private func play(_ player: AudioPlaybackController?) { 108 | guard let player = player else { 109 | return 110 | } 111 | withAmbientSession { 112 | if player.isPlaying { 113 | player.seek(to: .zero) 114 | } 115 | player.play() 116 | } 117 | } 118 | 119 | /// Play the click sound 120 | /// - Parameter sound: Which sound to play 121 | @MainActor 122 | func play(sound: File = .normal) { 123 | switch sound { 124 | case .normal: play(pressNormalPlayer) 125 | case .delete: play(pressDeletePlayer) 126 | case .modifier: play(pressModifierPlayer) 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardEvent.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License") 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import Combine 18 | import os 19 | 20 | /// Events posted by the keyboard 21 | public enum KeyboardEvent { 22 | /// Sent from the keyboard view's `onAppear()` 23 | case keyboardDidAppear 24 | /// Sent from the keyboard view's `onDisappear()` 25 | case keyboardDidDisappear 26 | /// Sent when a keyboard key is held down 27 | case keyDown(KeyboardKeyCode, modifier: KeyboardModifier) 28 | /// Sent when a keyboard key is released 29 | case keyUp(KeyboardKeyCode, modifier: KeyboardModifier) 30 | 31 | static func subject(for id: KeyboardIdentifier) -> KeyboardEventSubject { 32 | KeyboardEventSubject.for(id) 33 | } 34 | 35 | /// Returns a publisher that emits events from the keyboard. 36 | /// - Parameter id: A `KeyboardIdentifier` that uniquely identifies this keyboard instance. 37 | /// - Returns: A `Publisher` that emits events from the keyboard. 38 | public static func publisher(for id: KeyboardIdentifier) -> AnyPublisher { 39 | KeyboardEventSubject.for(id).eraseToAnyPublisher() 40 | } 41 | 42 | /// Returns a publisher that emits events from the keyboard. 43 | /// - Parameter id: Unique identifier for the keyboard to handle events from. 44 | /// - Returns: A `Publisher` that emits events from the keyboard. 45 | public static func publisher(for id: H) -> AnyPublisher { 46 | publisher(for: KeyboardIdentifier(for: id)) 47 | } 48 | } 49 | 50 | /// Modifier key state 51 | public struct KeyboardModifier: OptionSet { 52 | public let rawValue: Int 53 | 54 | public static var none = KeyboardModifier(rawValue: 0) 55 | public static var leftShift = KeyboardModifier(rawValue: 1 << 0) 56 | public static var rightShift = KeyboardModifier(rawValue: 1 << 1) 57 | public static var leftControl = KeyboardModifier(rawValue: 1 << 2) 58 | public static var rightControl = KeyboardModifier(rawValue: 1 << 3) 59 | public static var leftOption = KeyboardModifier(rawValue: 1 << 4) 60 | public static var rightOption = KeyboardModifier(rawValue: 1 << 5) 61 | public static var leftCommand = KeyboardModifier(rawValue: 1 << 6) 62 | public static var rightCommand = KeyboardModifier(rawValue: 1 << 7) 63 | public static var capsLock = KeyboardModifier(rawValue: 1 << 8) 64 | public static var scrollLock = KeyboardModifier(rawValue: 1 << 9) 65 | public static var numLock = KeyboardModifier(rawValue: 1 << 10) 66 | 67 | public init(rawValue: Int) { 68 | self.rawValue = rawValue 69 | } 70 | 71 | mutating func removeNontoggle() { 72 | subtract([.leftShift, .rightShift, .leftControl, .rightControl, .leftCommand, .rightCommand, .leftOption, .rightOption]) 73 | } 74 | } 75 | 76 | public enum KeyboardKeyCode { 77 | case key0 78 | case key1 79 | case key2 80 | case key3 81 | case key4 82 | case key5 83 | case key6 84 | case key7 85 | case key8 86 | case key9 87 | case keyA 88 | case keyACPIPower 89 | case keyACPISleep 90 | case keyACPIWake 91 | case keyApplication 92 | case keyB 93 | case keyBackslash 94 | case keyBackspace 95 | case keyC 96 | case keyCapsLock 97 | case keyComma 98 | case keyCommand 99 | case keyControl 100 | case keyD 101 | case keyDelete 102 | case keyDownArrow 103 | case keyE 104 | case keyEnd 105 | case keyEqual 106 | case keyEscape 107 | case keyF 108 | case keyF1 109 | case keyF10 110 | case keyF11 111 | case keyF12 112 | case keyF2 113 | case keyF3 114 | case keyF4 115 | case keyF5 116 | case keyF6 117 | case keyF7 118 | case keyF8 119 | case keyF9 120 | case keyG 121 | case keyGrave 122 | case keyH 123 | case keyHome 124 | case keyI 125 | case keyInsert 126 | case keyInternational107 127 | case keyInternational42 128 | case keyInternational45 129 | case keyJ 130 | case keyJISEisu 131 | case keyJISKana 132 | case keyJISUnderscore 133 | case keyJISYen 134 | case keyK 135 | case keyKeypad0 136 | case keyKeypad1 137 | case keyKeypad2 138 | case keyKeypad3 139 | case keyKeypad4 140 | case keyKeypad5 141 | case keyKeypad6 142 | case keyKeypad7 143 | case keyKeypad8 144 | case keyKeypad9 145 | case keyKeypadDecimal 146 | case keyKeypadDivide 147 | case keyKeypadEnter 148 | case keyKeypadMinus 149 | case keyKeypadMultiply 150 | case keyKeypadPlus 151 | case keyL 152 | case keyLeftArrow 153 | case keyLeftBracket 154 | case keyM 155 | case keyMinus 156 | case keyN 157 | case keyNumLock 158 | case keyO 159 | case keyOption 160 | case keyP 161 | case keyPageDown 162 | case keyPageUp 163 | case keyPause 164 | case keyPeriod 165 | case keyPrintScreen 166 | case keyQ 167 | case keyQuote 168 | case keyR 169 | case keyReturn 170 | case keyRightArrow 171 | case keyRightBracket 172 | case keyRightCommand 173 | case keyRightControl 174 | case keyRightOption 175 | case keyRightShift 176 | case keyS 177 | case keyScrollLock 178 | case keySemicolon 179 | case keyShift 180 | case keySlash 181 | case keySpace 182 | case keyT 183 | case keyTab 184 | case keyU 185 | case keyUpArrow 186 | case keyV 187 | case keyW 188 | case keyX 189 | case keyY 190 | case keyZ 191 | 192 | var isModifier: Bool { 193 | switch self { 194 | case .keyShift, .keyRightShift, .keyControl, .keyRightControl, .keyCommand, .keyRightCommand, .keyOption, .keyRightOption: 195 | return true 196 | case .keyCapsLock, .keyNumLock, .keyScrollLock: 197 | return true 198 | default: 199 | return false 200 | } 201 | } 202 | } 203 | 204 | /// Box for holding a weak reference 205 | private struct Weak { 206 | weak var ref: S? 207 | } 208 | 209 | /// We maintain a map of `KeyboardIdentifier` to `KeyboardEventSubject` in order to maintain that 210 | /// each identifier gets a unique instance of the `Subject`. When they keyboard view posts events to the `Subject`, 211 | /// any `Publisher` who is listening to it shall receive the event. 212 | /// 213 | /// Once all references are dropped, that means that the keyboard window is closed and all listeners have released 214 | /// their reference and so it is safe to clean up the map entry (which should be holding a box with a nil reference). 215 | /// We make sure all this is done in a thread safe manner by the use of `subjectsLock`. 216 | final class KeyboardEventSubject: Subject { 217 | typealias Output = KeyboardEvent 218 | typealias Failure = Never 219 | 220 | private static var subjectsLock = OSAllocatedUnfairLock() 221 | private static var allSubjects: [KeyboardIdentifier: Weak] = [:] 222 | private let id: KeyboardIdentifier 223 | private let underlyingSubject = PassthroughSubject() 224 | 225 | private init(for id: KeyboardIdentifier) { 226 | self.id = id 227 | } 228 | 229 | static func `for`(_ id: KeyboardIdentifier) -> KeyboardEventSubject { 230 | subjectsLock.withLock { 231 | if let w = allSubjects[id], let s = w.ref { 232 | return s 233 | } else { 234 | let s = KeyboardEventSubject(for: id) 235 | allSubjects[id] = Weak(ref: s) 236 | return s 237 | } 238 | } 239 | } 240 | 241 | deinit { 242 | Self.subjectsLock.withLock { 243 | _ = Self.allSubjects.removeValue(forKey: id) 244 | } 245 | } 246 | 247 | func send(subscription: Subscription) { 248 | underlyingSubject.send(subscription: subscription) 249 | } 250 | 251 | func receive(subscriber: S) where KeyboardEvent == S.Input, Never == S.Failure, S : Subscriber { 252 | underlyingSubject.receive(subscriber: subscriber) 253 | } 254 | 255 | func send(_ input: KeyboardEvent) { 256 | underlyingSubject.send(input) 257 | } 258 | 259 | func send(completion: Subscribers.Completion) { 260 | underlyingSubject.send(completion: completion) 261 | } 262 | } 263 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardEventPS2.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License") 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | /// Reference: https://babbage.cs.qc.cuny.edu/courses/cs345/Manuals/ms_scancode.pdf 18 | public extension KeyboardKeyCode { 19 | private var ps2Set1ScanBase: [UInt8] { 20 | switch self { 21 | case .keyGrave: return [0x29] 22 | case .key1: return [0x02] 23 | case .key2: return [0x03] 24 | case .key3: return [0x04] 25 | case .key4: return [0x05] 26 | case .key5: return [0x06] 27 | case .key6: return [0x07] 28 | case .key7: return [0x08] 29 | case .key8: return [0x09] 30 | case .key9: return [0x0a] 31 | case .key0: return [0x0b] 32 | case .keyMinus: return [0x0c] 33 | case .keyEqual: return [0x0d] 34 | case .keyBackspace: return [0x0e] 35 | case .keyTab: return [0x0f] 36 | case .keyQ: return [0x10] 37 | case .keyW: return [0x11] 38 | case .keyE: return [0x12] 39 | case .keyR: return [0x13] 40 | case .keyT: return [0x14] 41 | case .keyY: return [0x15] 42 | case .keyU: return [0x16] 43 | case .keyI: return [0x17] 44 | case .keyO: return [0x18] 45 | case .keyP: return [0x19] 46 | case .keyRightBracket: return [0x1a] 47 | case .keyLeftBracket: return [0x1b] 48 | case .keyBackslash: return [0x1c] 49 | case .keyCapsLock: return [0x3a] 50 | case .keyA: return [0x1e] 51 | case .keyS: return [0x1f] 52 | case .keyD: return [0x20] 53 | case .keyF: return [0x21] 54 | case .keyG: return [0x22] 55 | case .keyH: return [0x23] 56 | case .keyJ: return [0x24] 57 | case .keyK: return [0x25] 58 | case .keyL: return [0x26] 59 | case .keySemicolon: return [0x27] 60 | case .keyQuote: return [0x28] 61 | case .keyInternational42: return [0x2b] 62 | case .keyReturn: return [0x1c] 63 | case .keyShift: return [0x2a] 64 | case .keyInternational45: return [0x56] 65 | case .keyZ: return [0x2c] 66 | case .keyX: return [0x2d] 67 | case .keyC: return [0x2e] 68 | case .keyV: return [0x2f] 69 | case .keyB: return [0x30] 70 | case .keyN: return [0x31] 71 | case .keyM: return [0x32] 72 | case .keyComma: return [0x33] 73 | case .keyPeriod: return [0x34] 74 | case .keySlash: return [0x35] 75 | case .keyRightShift: return [0x36] 76 | case .keyControl: return [0x1d] 77 | case .keyOption: return [0x38] 78 | case .keySpace: return [0x39] 79 | case .keyRightOption: return [0xe0, 0x38] 80 | case .keyRightControl: return [0xe0, 0x1d] 81 | case .keyInsert: return [0xe0, 0x52] 82 | case .keyDelete: return [0xe0, 0x53] 83 | case .keyLeftArrow: return [0xe0, 0x4b] 84 | case .keyHome: return [0xe0, 0x47] 85 | case .keyEnd: return [0xe0, 0x4f] 86 | case .keyUpArrow: return [0xe0, 0x48] 87 | case .keyDownArrow: return [0xe0, 0x50] 88 | case .keyPageUp: return [0xe0, 0x49] 89 | case .keyPageDown: return [0xe0, 0x51] 90 | case .keyRightArrow: return [0xe0, 0x4d] 91 | case .keyNumLock: return [0x45] 92 | case .keyKeypad7: return [0x47] 93 | case .keyKeypad4: return [0x4b] 94 | case .keyKeypad1: return [0x4f] 95 | case .keyKeypadDivide: return [0xe0, 0x35] 96 | case .keyKeypad8: return [0x48] 97 | case .keyKeypad5: return [0x4c] 98 | case .keyKeypad2: return [0x50] 99 | case .keyKeypad0: return [0x52] 100 | case .keyKeypadMultiply: return [0x37] 101 | case .keyKeypad9: return [0x49] 102 | case .keyKeypad6: return [0x4d] 103 | case .keyKeypad3: return [0x51] 104 | case .keyKeypadDecimal: return [0x53] 105 | case .keyKeypadMinus: return [0x4a] 106 | case .keyKeypadPlus: return [0x4e] 107 | case .keyInternational107: return [0x7e] 108 | case .keyKeypadEnter: return [0xe0, 0x1c] 109 | case .keyEscape: return [0x01] 110 | case .keyF1: return [0x3b] 111 | case .keyF2: return [0x3c] 112 | case .keyF3: return [0x3d] 113 | case .keyF4: return [0x3e] 114 | case .keyF5: return [0x3f] 115 | case .keyF6: return [0x40] 116 | case .keyF7: return [0x41] 117 | case .keyF8: return [0x42] 118 | case .keyF9: return [0x43] 119 | case .keyF10: return [0x44] 120 | case .keyF11: return [0x57] 121 | case .keyF12: return [0x58] 122 | case .keyPrintScreen: return [0xe0, 0x2a, 0xe0, 0x37] 123 | case .keyScrollLock: return [0x46] 124 | case .keyPause: return [0xe1, 0x1d, 0x45, 0xe1, 0x9d, 0xc5] 125 | case .keyCommand: return [0xe0, 0x5b] 126 | case .keyRightCommand: return [0xe0, 0x5c] 127 | case .keyApplication: return [0xe0, 0x5d] 128 | case .keyACPIPower: return [0xe0, 0x5e] 129 | case .keyACPISleep: return [0xe0, 0x5f] 130 | case .keyACPIWake: return [0xe0, 0x63] 131 | case .keyJISYen: return [0x7d] 132 | case .keyJISUnderscore: return [0x73] 133 | case .keyJISEisu: return [0x7b] 134 | case .keyJISKana: return [0x79] 135 | } 136 | } 137 | 138 | /// Generate a PS/2 compatible set 1 scan code (make) 139 | /// - Parameter modifiers: Optional modifiers which may affect scan code output. 140 | /// - Returns: A byte array of scan codes. 141 | func ps2Set1ScanMake(_ modifiers: KeyboardModifier = .none) -> [UInt8] { 142 | let base = ps2Set1ScanBase 143 | let shiftModifier = { (base: [UInt8]) -> [UInt8] in 144 | var prefix = [UInt8]() 145 | if modifiers.contains(.leftShift) { 146 | prefix = [0xe0, 0xaa] 147 | } 148 | if modifiers.contains(.rightShift) { 149 | prefix = prefix + [0xe0, 0xb6] 150 | } 151 | return prefix + base 152 | } 153 | switch self { 154 | // note 1 155 | case .keyInsert, .keyDelete, .keyLeftArrow, .keyHome, .keyEnd, .keyUpArrow, .keyDownArrow, .keyPageUp, .keyPageDown, .keyRightArrow: 156 | if modifiers.contains(.numLock) { 157 | return [0xe0, 0x2a] + base 158 | } else { 159 | return shiftModifier(base) 160 | } 161 | // note 3 162 | case .keyKeypadDivide: 163 | return shiftModifier(base) 164 | // note 4 165 | case .keyPrintScreen: 166 | if modifiers.contains(.leftControl) { 167 | return [0xe0, 0x37] 168 | } else if modifiers.contains(.rightControl) && !modifiers.isDisjoint(with: [.leftShift, .rightShift]) { 169 | return [0xe0, 0x37] 170 | } else if !modifiers.isDisjoint(with: [.leftOption, .rightOption]) { 171 | return [0x54] 172 | } else { 173 | return base 174 | } 175 | // note 5 176 | case .keyPause: 177 | if !modifiers.isDisjoint(with: [.leftControl, .rightControl]) { 178 | return [0xe0, 0x46, 0xe0, 0xc6] 179 | } else { 180 | return base 181 | } 182 | default: 183 | return base 184 | } 185 | } 186 | 187 | /// Generate a PS/2 compatible set 1 scan code (break) 188 | /// - Parameter modifiers: Optional modifiers which may affect scan code output. 189 | /// - Returns: A byte array of scan codes. 190 | func ps2Set1ScanBreak(_ modifiers: KeyboardModifier = .none) -> [UInt8] { 191 | let base = ps2Set1ScanBase.map({ $0 | 0x80 }) 192 | let shiftModifier = { (base: [UInt8]) -> [UInt8] in 193 | var suffix = [UInt8]() 194 | if modifiers.contains(.rightShift) { 195 | suffix = [0xe0, 0x36] 196 | } 197 | if modifiers.contains(.leftShift) { 198 | suffix = suffix + [0xe0, 0x2a] 199 | } 200 | return base + suffix 201 | } 202 | switch self { 203 | // note 1 204 | case .keyInsert, .keyDelete, .keyLeftArrow, .keyHome, .keyEnd, .keyUpArrow, .keyDownArrow, .keyPageUp, .keyPageDown, .keyRightArrow: 205 | if modifiers.contains(.numLock) { 206 | return base + [0xe0, 0xaa] 207 | } else { 208 | return shiftModifier(base) 209 | } 210 | // note 3 211 | case .keyKeypadDivide: 212 | return shiftModifier(base) 213 | // note 4 214 | case .keyPrintScreen: 215 | if modifiers.contains(.leftControl) { 216 | return [0xe0, 0xb7] 217 | } else if modifiers.contains(.rightControl) && !modifiers.isDisjoint(with: [.leftShift, .rightShift]) { 218 | return [0xe0, 0xb7] 219 | } else if !modifiers.isDisjoint(with: [.leftOption, .rightOption]) { 220 | return [0xd4] 221 | } else { 222 | return base 223 | } 224 | // note 5 225 | case .keyPause: 226 | return [] 227 | default: 228 | return base 229 | } 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardIdentifier.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | /// Represents a unique instance of the keyboard. 20 | public struct KeyboardIdentifier: Codable, Hashable { 21 | private let id: Int 22 | 23 | /// Use this if your app only needs one instance of the keyboard. 24 | public static let global = KeyboardIdentifier(for: 0) 25 | 26 | init(for id: any Hashable) { 27 | self.id = id.hashValue 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardKeyView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | struct KeyboardKeyView: View { 20 | let key: String 21 | var shift: String? 22 | let code: KeyboardKeyCode 23 | var width: CGFloat = 50 24 | var height: CGFloat = 50 25 | var isDark: Bool = false 26 | var isToggled: Bool = false 27 | var clickSound: KeyboardClickSound.File = .normal 28 | var action: () -> Void = {} 29 | 30 | @Environment(KeyboardState.self) private var state 31 | 32 | private var isShifted: Bool { 33 | !state.modifier.isDisjoint(with: [.leftShift, .rightShift]) 34 | } 35 | 36 | var body: some View { 37 | Button(action: action) { 38 | Text(isShifted ? shift ?? key : key) 39 | }.buttonStyle(.keyboardButton(isDark: isDark, isToggled: isToggled, fontSize: width > 50 && key.count > 1 ? 20 : key.count > 1 ? 16 : 24, width: width, height: height, clickSound: clickSound, onTouchDown: { 40 | state.keyDown(code) 41 | }, onTouchUp: { 42 | state.keyUp(code) 43 | })) 44 | } 45 | } 46 | 47 | struct KeyboardToggleKeyView: View { 48 | let key: String 49 | let code: KeyboardKeyCode 50 | let modifier: KeyboardModifier 51 | var otherModifier: KeyboardModifier? 52 | var width: CGFloat = 50 53 | var height: CGFloat = 50 54 | var isDark: Bool = false 55 | 56 | @Environment(KeyboardState.self) private var state 57 | 58 | private var isOn: Bool { 59 | if let otherModifier = otherModifier { 60 | return !state.modifier.isDisjoint(with: [modifier, otherModifier]) 61 | } else { 62 | return state.modifier.contains(modifier) 63 | } 64 | } 65 | 66 | var body: some View { 67 | KeyboardKeyView(key: key, code: code, width: width, height: height, isDark: isDark, isToggled: isOn, clickSound: .modifier) { 68 | if isOn { 69 | state.modifier.remove(modifier) 70 | if let otherModifier = otherModifier { 71 | state.modifier.remove(otherModifier) 72 | } 73 | } else { 74 | state.modifier.insert(modifier) 75 | if let otherModifier = otherModifier { 76 | state.modifier.insert(otherModifier) 77 | } 78 | } 79 | } 80 | } 81 | } 82 | 83 | #Preview { 84 | VStack { 85 | KeyboardKeyView(key: "a", code: .keyA) 86 | KeyboardKeyView(key: "a", code: .keyA, isDark: true) 87 | KeyboardKeyView(key: "a", code: .keyA, isToggled: true) 88 | KeyboardKeyView(key: "abcd", code: .keySpace) 89 | KeyboardKeyView(key: "abcd", code: .keySpace, width: 100) 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardLayoutANSI.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | struct KeyboardLayoutANSI: View { 20 | var body: some View { 21 | VStack { 22 | HStack { 23 | KeyboardKeyView(key: "⎋", code: .keyEscape, isDark: true, clickSound: .delete) 24 | KeyboardKeyView(key: "F1", code: .keyF1) 25 | KeyboardKeyView(key: "F2", code: .keyF2) 26 | KeyboardKeyView(key: "F3", code: .keyF3) 27 | KeyboardKeyView(key: "F4", code: .keyF4) 28 | KeyboardKeyView(key: "F5", code: .keyF5) 29 | KeyboardKeyView(key: "F6", code: .keyF6) 30 | KeyboardKeyView(key: "F7", code: .keyF7) 31 | KeyboardKeyView(key: "F8", code: .keyF8) 32 | KeyboardKeyView(key: "F9", code: .keyF9) 33 | KeyboardKeyView(key: "F10", code: .keyF10) 34 | KeyboardKeyView(key: "F11", code: .keyF11) 35 | KeyboardKeyView(key: "F12", code: .keyF12) 36 | } 37 | HStack { 38 | KeyboardKeyView(key: "`", shift: "~", code: .keyGrave) 39 | KeyboardKeyView(key: "1", shift: "!", code: .key1) 40 | KeyboardKeyView(key: "2", shift: "@", code: .key2) 41 | KeyboardKeyView(key: "3", shift: "#", code: .key3) 42 | KeyboardKeyView(key: "4", shift: "$", code: .key4) 43 | KeyboardKeyView(key: "5", shift: "%", code: .key5) 44 | KeyboardKeyView(key: "6", shift: "^", code: .key6) 45 | KeyboardKeyView(key: "7", shift: "&", code: .key7) 46 | KeyboardKeyView(key: "8", shift: "*", code: .key8) 47 | KeyboardKeyView(key: "9", shift: "(", code: .key9) 48 | KeyboardKeyView(key: "0", shift: ")", code: .key0) 49 | KeyboardKeyView(key: "-", shift: "_", code: .keyMinus) 50 | KeyboardKeyView(key: "=", shift: "+", code: .keyEqual) 51 | KeyboardKeyView(key: "⌫", code: .keyBackspace, width: 65, isDark: true, clickSound: .delete) 52 | } 53 | HStack { 54 | KeyboardKeyView(key: "⇥", code: .keyTab, width: 65, isDark: true) 55 | KeyboardKeyView(key: "q", shift: "Q", code: .keyQ) 56 | KeyboardKeyView(key: "w", shift: "W", code: .keyW) 57 | KeyboardKeyView(key: "e", shift: "E", code: .keyE) 58 | KeyboardKeyView(key: "r", shift: "R", code: .keyR) 59 | KeyboardKeyView(key: "t", shift: "T", code: .keyT) 60 | KeyboardKeyView(key: "y", shift: "Y", code: .keyY) 61 | KeyboardKeyView(key: "u", shift: "U", code: .keyU) 62 | KeyboardKeyView(key: "i", shift: "I", code: .keyI) 63 | KeyboardKeyView(key: "o", shift: "O", code: .keyO) 64 | KeyboardKeyView(key: "p", shift: "P", code: .keyP) 65 | KeyboardKeyView(key: "[", shift: "{", code: .keyLeftBracket) 66 | KeyboardKeyView(key: "]", shift: "}", code: .keyRightBracket) 67 | KeyboardKeyView(key: "\\", shift: "|", code: .keyBackslash) 68 | } 69 | HStack { 70 | KeyboardToggleKeyView(key: "⇪", code: .keyCapsLock, modifier: .capsLock, width: 65, isDark: true) 71 | KeyboardKeyView(key: "a", shift: "A", code: .keyA) 72 | KeyboardKeyView(key: "s", shift: "S", code: .keyS) 73 | KeyboardKeyView(key: "d", shift: "D", code: .keyD) 74 | KeyboardKeyView(key: "f", shift: "F", code: .keyF) 75 | KeyboardKeyView(key: "g", shift: "G", code: .keyG) 76 | KeyboardKeyView(key: "h", shift: "H", code: .keyH) 77 | KeyboardKeyView(key: "j", shift: "J", code: .keyJ) 78 | KeyboardKeyView(key: "k", shift: "K", code: .keyK) 79 | KeyboardKeyView(key: "l", shift: "L", code: .keyL) 80 | KeyboardKeyView(key: ";", shift: ":", code: .keySemicolon) 81 | KeyboardKeyView(key: "'", shift: "\"", code: .keyQuote) 82 | KeyboardKeyView(key: "return", code: .keyReturn, width: 85, isDark: true) 83 | } 84 | HStack { 85 | KeyboardToggleKeyView(key: "⇧", code: .keyShift, modifier: .leftShift, otherModifier: .rightShift, width: 85, isDark: true) 86 | KeyboardKeyView(key: "z", shift: "Z", code: .keyZ) 87 | KeyboardKeyView(key: "x", shift: "X", code: .keyX) 88 | KeyboardKeyView(key: "c", shift: "C", code: .keyC) 89 | KeyboardKeyView(key: "v", shift: "V", code: .keyV) 90 | KeyboardKeyView(key: "b", shift: "B", code: .keyB) 91 | KeyboardKeyView(key: "n", shift: "N", code: .keyN) 92 | KeyboardKeyView(key: "m", shift: "M", code: .keyM) 93 | KeyboardKeyView(key: ",", shift: "<", code: .keyComma) 94 | KeyboardKeyView(key: ".", shift: ">", code: .keyPeriod) 95 | KeyboardKeyView(key: "/", shift: "?", code: .keySlash) 96 | KeyboardToggleKeyView(key: "⇧", code: .keyRightShift, modifier: .rightShift, otherModifier: .leftShift, width: 85, isDark: true) 97 | } 98 | HStack { 99 | KeyboardToggleKeyView(key: "⌃", code: .keyControl, modifier: .leftControl, otherModifier: .rightControl, width: 65, isDark: true) 100 | KeyboardToggleKeyView(key: "⌥", code: .keyOption, modifier: .leftOption, otherModifier: .rightOption, width: 65, isDark: true) 101 | KeyboardToggleKeyView(key: "⌘", code: .keyCommand, modifier: .leftCommand, otherModifier: .rightCommand, width: 65, isDark: true) 102 | KeyboardKeyView(key: "space", code: .keySpace, width: 360) 103 | KeyboardToggleKeyView(key: "⌘", code: .keyRightCommand, modifier: .rightCommand, otherModifier: .leftCommand, width: 65, isDark: true) 104 | KeyboardToggleKeyView(key: "⌥", code: .keyRightOption, modifier: .rightOption, otherModifier: .leftOption, width: 65, isDark: true) 105 | KeyboardToggleKeyView(key: "⌃", code: .keyRightControl, modifier: .rightControl, otherModifier: .leftControl, width: 65, isDark: true) 106 | } 107 | } 108 | } 109 | 110 | } 111 | 112 | #Preview { 113 | KeyboardLayoutANSI() 114 | } 115 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardLayoutTenkeys.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | struct KeyboardLayoutTenKeys: View { 20 | var body: some View { 21 | Grid { 22 | GridRow { 23 | KeyboardKeyView(key: "PrSn", code: .keyPrintScreen) 24 | KeyboardToggleKeyView(key: "ScLk", code: .keyScrollLock, modifier: .scrollLock) 25 | KeyboardKeyView(key: "Pause", code: .keyPause) 26 | Color.clear.frame(width: 20, height: 0) 27 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 28 | .gridCellColumns(4) 29 | } 30 | GridRow { 31 | KeyboardKeyView(key: "Ins", code: .keyInsert) 32 | KeyboardKeyView(key: "Home", code: .keyHome) 33 | KeyboardKeyView(key: "PgUp", code: .keyPageUp) 34 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 35 | KeyboardToggleKeyView(key: "NmLk", code: .keyNumLock, modifier: .numLock) 36 | KeyboardKeyView(key: "/", code: .keyKeypadDivide) 37 | KeyboardKeyView(key: "*", code: .keyKeypadMultiply) 38 | KeyboardKeyView(key: "-", code: .keyKeypadMinus) 39 | } 40 | GridRow { 41 | KeyboardKeyView(key: "Del", code: .keyDelete, clickSound: .delete) 42 | KeyboardKeyView(key: "End", code: .keyEnd) 43 | KeyboardKeyView(key: "PgDn", code: .keyPageDown) 44 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 45 | KeyboardKeyView(key: "7", code: .keyKeypad7) 46 | KeyboardKeyView(key: "8", code: .keyKeypad8) 47 | KeyboardKeyView(key: "9", code: .keyKeypad9) 48 | GeometryReader { geo in 49 | KeyboardKeyView(key: "+", code: .keyKeypadPlus, height: 110) 50 | }.gridCellUnsizedAxes([.horizontal, .vertical]) 51 | } 52 | GridRow { 53 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 54 | .gridCellColumns(4) 55 | KeyboardKeyView(key: "4", code: .keyKeypad4) 56 | KeyboardKeyView(key: "5", code: .keyKeypad5) 57 | KeyboardKeyView(key: "6", code: .keyKeypad6) 58 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 59 | } 60 | GridRow { 61 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 62 | KeyboardKeyView(key: "↑", code: .keyUpArrow, isDark: true) 63 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 64 | .gridCellColumns(2) 65 | KeyboardKeyView(key: "1", code: .keyKeypad1) 66 | KeyboardKeyView(key: "2", code: .keyKeypad2) 67 | KeyboardKeyView(key: "3", code: .keyKeypad3) 68 | GeometryReader { geo in 69 | KeyboardKeyView(key: "↵", code: .keyKeypadEnter, height: 110, isDark: true) 70 | }.gridCellUnsizedAxes([.horizontal, .vertical]) 71 | } 72 | GridRow { 73 | KeyboardKeyView(key: "←", code: .keyLeftArrow, isDark: true) 74 | KeyboardKeyView(key: "↓", code: .keyDownArrow, isDark: true) 75 | KeyboardKeyView(key: "→", code: .keyRightArrow, isDark: true) 76 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 77 | KeyboardKeyView(key: "0", code: .keyKeypad0, width: 110) 78 | .gridCellColumns(2) 79 | KeyboardKeyView(key: ".", code: .keyKeypadDecimal) 80 | Color.clear.gridCellUnsizedAxes([.horizontal, .vertical]) 81 | } 82 | } 83 | } 84 | } 85 | 86 | #Preview { 87 | KeyboardLayoutTenKeys() 88 | } 89 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardLayoutType.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import Foundation 18 | 19 | enum KeyboardLayoutType: String, CaseIterable { 20 | case ansi = "ANSI" 21 | 22 | var label: String { 23 | switch self { 24 | case .ansi: return NSLocalizedString("ANSI", comment: "KeyboardLayoutType") 25 | } 26 | } 27 | } 28 | 29 | extension KeyboardLayoutType: Identifiable { 30 | var id: Self { 31 | self 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardSettingsViewModifier.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | struct KeyboardSettingsViewModifier: ViewModifier { 20 | @AppStorage("IsExpanded") private var isExpanded: Bool = false 21 | @AppStorage("Layout") private var layout: KeyboardLayoutType = .ansi 22 | 23 | func body(content: Content) -> some View { 24 | content.ornament(attachmentAnchor: .scene(.topLeading)) { 25 | Menu { 26 | ForEach(KeyboardLayoutType.allCases) { option in 27 | Toggle(option.label, isOn: Binding { 28 | layout == option 29 | } set: { _ in 30 | layout = option 31 | }) 32 | } 33 | } label: { 34 | Label("Layout…", systemImage: "globe") 35 | }.labelStyle(.iconOnly) 36 | } 37 | .ornament(attachmentAnchor: .scene(.topTrailing)) { 38 | if isExpanded { 39 | Button { 40 | isExpanded = false 41 | } label: { 42 | Label("Contract", systemImage: "chevron.left") 43 | }.labelStyle(.iconOnly) 44 | } else { 45 | Button { 46 | isExpanded = true 47 | } label: { 48 | Label("Expand", systemImage: "chevron.right") 49 | }.labelStyle(.iconOnly) 50 | } 51 | } 52 | } 53 | } 54 | 55 | extension View { 56 | func keyboardOrnament() -> some View { 57 | self.modifier(KeyboardSettingsViewModifier()) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardState.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | @Observable 20 | class KeyboardState { 21 | private let id: KeyboardIdentifier 22 | private let subject: KeyboardEventSubject 23 | 24 | let clicker = KeyboardClickSound() 25 | 26 | var modifier: KeyboardModifier = .none 27 | 28 | init(for id: KeyboardIdentifier) { 29 | self.id = id 30 | self.subject = KeyboardEvent.subject(for: id) 31 | } 32 | 33 | func keyboardDidAppear() { 34 | subject.send(.keyboardDidAppear) 35 | } 36 | 37 | func keyboardDidDisappear() { 38 | subject.send(.keyboardDidDisappear) 39 | } 40 | 41 | func keyDown(_ code: KeyboardKeyCode) { 42 | subject.send(.keyDown(code, modifier: modifier)) 43 | } 44 | 45 | func keyUp(_ code: KeyboardKeyCode) { 46 | subject.send(.keyUp(code, modifier: modifier)) 47 | if !code.isModifier { 48 | modifier.removeNontoggle() 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | struct KeyboardView: View { 20 | @AppStorage("IsExpanded") private var isExpanded: Bool = false 21 | @AppStorage("Layout") private var layout: KeyboardLayoutType = .ansi 22 | @State private var state: KeyboardState 23 | 24 | init(for id: KeyboardIdentifier) { 25 | state = KeyboardState(for: id) 26 | } 27 | 28 | var body: some View { 29 | HStack { 30 | KeyboardLayoutANSI() 31 | if isExpanded { 32 | Color.clear.frame(width: 20, height: 0) 33 | KeyboardLayoutTenKeys() 34 | } 35 | } 36 | .onAppear { 37 | state.keyboardDidAppear() 38 | } 39 | .onDisappear { 40 | state.keyboardDidDisappear() 41 | } 42 | .keyboardOrnament() 43 | .fixedSize() 44 | .padding([.top, .bottom]) 45 | .padding([.leading, .trailing], 20) 46 | .background(.black.opacity(0.25)) 47 | .environment(state) 48 | } 49 | } 50 | 51 | #Preview { 52 | KeyboardView(for: .global) 53 | } 54 | -------------------------------------------------------------------------------- /Sources/VisionKeyboardKit/KeyboardWindowGroup.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright © 2023 osy. All rights reserved. 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | 17 | import SwiftUI 18 | 19 | /// Instantiate this in your `Scene` to support keyboard windows. 20 | /// 21 | /// Use `OpenWindowAction.callAsFunction(keyboardFor:)` to show the keyboard. 22 | public struct KeyboardWindowGroup: Scene { 23 | public init(){} 24 | 25 | public var body: some Scene { 26 | WindowGroup(for: KeyboardIdentifier.self) { $id in 27 | KeyboardView(for: id ?? .global) 28 | } 29 | .windowResizability(.contentSize) 30 | } 31 | } 32 | 33 | public extension OpenWindowAction { 34 | /// Opens a keyboard window 35 | /// - Parameter id: Any `Hashable` object that uniquely identifies this keyboard instance. 36 | func callAsFunction(keyboardFor id: ID) where ID: Hashable { 37 | callAsFunction(value: KeyboardIdentifier(for: id)) 38 | } 39 | 40 | /// Opens a keyboard window 41 | /// - Parameter id: A `KeyboardIdentifier` that uniquely identifies this keyboard instance. 42 | func callAsFunction(keyboardFor id: KeyboardIdentifier) { 43 | callAsFunction(value: id) 44 | } 45 | } 46 | 47 | public extension DismissWindowAction { 48 | /// Closes a keyboard window 49 | /// - Parameter id: Any `Hashable` object that uniquely identifies this keyboard instance. 50 | func callAsFunction(keyboardFor id: ID) where ID: Hashable { 51 | callAsFunction(value: KeyboardIdentifier(for: id)) 52 | } 53 | 54 | /// Closes a keyboard window 55 | /// - Parameter id: A `KeyboardIdentifier` that uniquely identifies this keyboard instance. 56 | func callAsFunction(keyboardFor id: KeyboardIdentifier) { 57 | callAsFunction(value: id) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Tests/VisionKeyboardKitTests/VisionKeyboardKitTests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | @testable import VisionKeyboardKit 3 | 4 | final class VisionKeyboardKitTests: XCTestCase { 5 | func testExample() throws { 6 | // XCTest Documentation 7 | // https://developer.apple.com/documentation/xctest 8 | 9 | // Defining Test Cases and Test Methods 10 | // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/utmapp/VisionKeyboardKit/0804e4d64267acc8d08fb23160f5b6ac6134414f/screenshot.png --------------------------------------------------------------------------------