Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Toggle the guest's software keyboard without restarting

Marvae8804d3f579fe
diff --git a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
index ae50bbc1..a12725a9 100644
--- a/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
+++ b/VPhoneExecutable/VPhoneVirtualization/Resources/Localizable.xcstrings
@@ -688,6 +688,111 @@
}
}
},
+ "Toggle Software Keyboard" : {
+ "extractionState" : "manual",
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Toggle Software Keyboard"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ソフトウェアキーボードを切り替え"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "소프트웨어 키보드 전환"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Bật/tắt bàn phím phần mềm"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "切换软件键盘"
+ }
+ }
+ }
+ },
+ "Shows or hides the guest's software keyboard while the hardware keyboard stays attached. Restarting SpringBoard returns to the hardware keyboard." : {
+ "extractionState" : "manual",
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Shows or hides the guest's software keyboard while the hardware keyboard stays attached. Restarting SpringBoard returns to the hardware keyboard."
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ハードウェアキーボードを接続したまま、ゲストのソフトウェアキーボードを表示または非表示にします。SpringBoard を再起動するとハードウェアキーボードに戻ります。"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "하드웨어 키보드를 연결한 채로 게스트의 소프트웨어 키보드를 표시하거나 숨깁니다. SpringBoard를 재시동하면 하드웨어 키보드로 돌아갑니다."
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Hiện hoặc ẩn bàn phím phần mềm của máy khách trong khi bàn phím phần cứng vẫn được kết nối. Khởi động lại SpringBoard sẽ quay về bàn phím phần cứng."
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "在硬件键盘保持连接的情况下显示或隐藏客户机的软件键盘。重启 SpringBoard 后会恢复为硬件键盘。"
+ }
+ }
+ }
+ },
+ "Unable to Toggle Software Keyboard" : {
+ "extractionState" : "manual",
+ "localizations" : {
+ "en" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Unable to Toggle Software Keyboard"
+ }
+ },
+ "ja" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "ソフトウェアキーボードを切り替えられません"
+ }
+ },
+ "ko" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "소프트웨어 키보드를 전환할 수 없음"
+ }
+ },
+ "vi" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "Không thể bật/tắt bàn phím phần mềm"
+ }
+ },
+ "zh-Hans" : {
+ "stringUnit" : {
+ "state" : "translated",
+ "value" : "无法切换软件键盘"
+ }
+ }
+ }
+ },
"Enable Hardware Keyboard?" : {
"extractionState" : "manual",
"localizations" : {
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuController.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuController.swift
index a69ef578..9b7d0a08 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuController.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuController.swift
@@ -22,6 +22,7 @@ class VPhoneMenuController {
}
var hardwareKeyboardItem: NSMenuItem?
+ var softwareKeyboardItem: NSMenuItem?
var tvControlCenterItem: NSMenuItem?
var tvRemoteItem: NSMenuItem?
lazy var tvRemoteWindowController = VPhoneTVRemoteWindowController(control: control)
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuDevice.swift b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuDevice.swift
index 2eb14d1a..e3a89a64 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuDevice.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/UserInterface/Menu/VPhoneMenuDevice.swift
@@ -97,6 +97,20 @@ extension VPhoneMenuController {
keyboardItem.toolTip = VPhoneLocalization.text("Changing the hardware keyboard requires restarting the virtual machine.")
hardwareKeyboardItem = keyboardItem
menu.addItem(keyboardItem)
+ // Simulator's I/O > Keyboard item, without its Command-K: a plain
+ // Command-letter is the guest's (Command-K is Add Link in Notes). It
+ // flips the guest's keyboard mode in place, so it needs no restart.
+ let softwareKeyboard = makeItem(
+ "Toggle Software Keyboard",
+ action: #selector(toggleSoftwareKeyboard),
+ symbol: "keyboard.chevron.compact.down",
+ )
+ softwareKeyboard.isEnabled = false
+ softwareKeyboard.toolTip = VPhoneLocalization.text(
+ "Shows or hides the guest's software keyboard while the hardware keyboard stays attached. Restarting SpringBoard returns to the hardware keyboard.",
+ )
+ softwareKeyboardItem = softwareKeyboard
+ menu.addItem(softwareKeyboard)
// Trackpad scroll and pinch arrive as ordinary NSEvents; the view turns
// them into guest touches. Off hands both back to AppKit untouched.
let trackpadItem = makeItem(
@@ -230,6 +244,47 @@ extension VPhoneMenuController {
}
}
+ // MARK: - Software Keyboard
+
+ /// Without a virtual USB keyboard the guest already uses its software
+ /// keyboard, so the item only applies while one is attached. Only iPhone and
+ /// iPad guests were measured: tvOS draws its own on-screen keyboard, and a
+ /// watch or HomePod guest was not tried, so those never get it.
+ func updateSoftwareKeyboardAvailability(available: Bool) {
+ let measured = !showsTVControls && !showsWatchControls && !control.isHomePodGuest
+ softwareKeyboardItem?.isEnabled = available && measured && vm?.usesHardwareKeyboard == true
+ }
+
+ /// GraphicsServices keeps the guest's hardware keyboard in the state of the
+ /// `GSEventHardwareKeyboardAttached` Darwin notification: bit 0 attached,
+ /// bits 8–15 the country code, bits 16 and up the keyboard type. Posting it
+ /// with bit 0 flipped is what the guest does when a keyboard comes or goes,
+ /// so the guest switches to or from its software keyboard while the USB
+ /// keyboard stays attached. SpringBoard sets bit 0 again when it restarts.
+ @objc func toggleSoftwareKeyboard() {
+ guard softwareKeyboardItem?.isEnabled == true else { return }
+ let name = "GSEventHardwareKeyboardAttached"
+ Task {
+ do {
+ let reply = try await control.call("notify.state", params: ["name": name])
+ guard let state = (reply["state"] as? NSNumber)?.uint64Value else {
+ throw VPhoneGuestControl.ControlError.protocolError("notify.state returned no state for \(name)")
+ }
+ let toggled = state ^ 1
+ // A decimal string keeps all 64 bits, which a JSON double does not.
+ _ = try await control.call("notify.post", params: ["name": name, "state": String(toggled)])
+ print("[keyboard] Software keyboard \(toggled & 1 == 0 ? "shown" : "hidden"), state 0x\(String(toggled, radix: 16))")
+ } catch {
+ print("[keyboard] Software keyboard toggle failed: \(error)")
+ VPhoneAlert.present(
+ title: "Unable to Toggle Software Keyboard",
+ message: "Check that the guest agent is connected, then try again.",
+ style: .warning,
+ )
+ }
+ }
+ }
+
// MARK: - Rotate
/// The four interface orientations, checked by the one the window last
diff --git a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
index ea1b6f8c..8a1d2753 100644
--- a/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
+++ b/VPhoneExecutable/VPhoneVirtualization/UI/VPhoneVirtualMachineAppDelegate.swift
@@ -328,6 +328,7 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
mc?.updateRestartAvailability(available: caps.contains("system_control"))
mc?.updateShutDownAvailability(available: caps.contains("system_shutdown"))
mc?.updateUDIDAvailability(available: caps.contains("udid_override"))
+ mc?.updateSoftwareKeyboardAvailability(available: true)
mc?.updatePanelAvailability(capabilities: caps)
if caps.contains("location") {
mc?.updateLocationCapability(available: true)
@@ -355,6 +356,7 @@ class VPhoneVirtualMachineAppDelegate: NSObject, NSApplicationDelegate {
}
control.onDisconnect = { [weak mc, weak provider = locationProvider, weak timeZoneSync, weak clockSync, weak hostAudioLatencySync] in
mc?.updateConnectAvailability(available: false)
+ mc?.updateSoftwareKeyboardAvailability(available: false)
mc?.updateInstallAvailability(available: false)
mc?.updateBootstrapAvailability(available: false)
mc?.updateBootstrapUninstallAvailability(available: false)