Skip to content

Commit

Permalink
Merge pull request #5974 from js-john/auto-capture-mouse
Browse files Browse the repository at this point in the history
Automatically capture cursor and keyboard on window focus
  • Loading branch information
osy committed Apr 30, 2024
2 parents 9f8c1a8 + 7efea64 commit 3e0bdee
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Platform/macOS/Display/VMDisplayQemuMetalWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class VMDisplayQemuMetalWindowController: VMDisplayQemuWindowController {
@Setting("NoCursorCaptureAlert") private var isCursorCaptureAlertShown: Bool = false
@Setting("NoFullscreenCursorCaptureAlert") private var isFullscreenCursorCaptureAlertShown: Bool = false
@Setting("FullScreenAutoCapture") private var isFullScreenAutoCapture: Bool = false
@Setting("WindowFocusAutoCapture") private var isWindowFocusAutoCapture: Bool = false
@Setting("CtrlRightClick") private var isCtrlRightClick: Bool = false
@Setting("AlternativeCaptureKey") private var isAlternativeCaptureKey: Bool = false
@Setting("IsCapsLockKey") private var isCapsLockKey: Bool = false
Expand Down Expand Up @@ -414,6 +415,21 @@ extension VMDisplayQemuMetalWindowController {
}
}

func windowDidBecomeMain(_ notification: Notification) {
// Do not capture mouse if user did not clicked inside the metalView because the window will be draged if user hold the mouse button.
guard let window = window,
window.mouseLocationOutsideOfEventStream.y < metalView.frame.height,
captureMouseToolbarButton.state == .off,
isWindowFocusAutoCapture else {
return
}
captureMouse()
}

func windowDidResignMain(_ notification: Notification) {
releaseMouse()
}

override func windowDidBecomeKey(_ notification: Notification) {
if isFullScreen && isFullScreenAutoCapture {
captureMouse()
Expand Down
4 changes: 4 additions & 0 deletions Platform/macOS/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ struct SoundSettingsView: View {

struct InputSettingsView: View {
@AppStorage("FullScreenAutoCapture") var isFullScreenAutoCapture = false
@AppStorage("WindowFocusAutoCapture") var isWindowFocusAutoCapture = false
@AppStorage("OptionAsMetaKey") var isOptionAsMetaKey = false
@AppStorage("CtrlRightClick") var isCtrlRightClick = false
@AppStorage("AlternativeCaptureKey") var isAlternativeCaptureKey = false
Expand All @@ -143,6 +144,9 @@ struct InputSettingsView: View {
Toggle(isOn: $isFullScreenAutoCapture) {
Text("Capture input automatically when entering full screen")
}.help("If enabled, input capture will toggle automatically when entering and exiting full screen mode.")
Toggle(isOn: $isWindowFocusAutoCapture) {
Text("Capture input automatically when window is focused")
}.help("If enabled, input capture will toggle automatically when the VM's window is focused.")
}

Section(header: Text("Console")) {
Expand Down
6 changes: 6 additions & 0 deletions Platform/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,12 @@
/* No comment provided by engineer. */
"Capture input automatically when entering full screen" = "进入全屏时自动捕获输入";

"If enabled, input capture will toggle automatically when entering and exiting full screen mode." = "如果启用此开关,进入和退出全屏模式时将自动切换输入捕获。";

"Capture input automatically when window is focused" = "成为活动窗口时自动捕获输入";

"If enabled, input capture will toggle automatically when the VM's window is focused." = "如果启用此开关,成为/失去当前活动窗口时自动切换输入捕获。";

/* VMDisplayQemuMetalWindowController */
"Captured mouse" = "已捕获鼠标";

Expand Down

0 comments on commit 3e0bdee

Please sign in to comment.